-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (72 loc) · 2.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"flag"
"fmt"
"github.com/go-chi/chi/v5/middleware"
"github.com/golang-io/requests"
"io"
"log"
"net/http"
"os"
)
func Token(token string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if token == "" || r.Header.Get("Token") != token {
http.Error(w, "token header is must!", http.StatusBadRequest)
return
}
next.ServeHTTP(w, r)
})
}
}
func Method(method string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if method != "" && r.Method != method {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
next.ServeHTTP(w, r)
})
}
}
func Echo(w http.ResponseWriter, r *http.Request) { _, _ = io.Copy(w, r.Body) }
func RequestLog(output string) func(http.Handler) http.Handler {
out := os.Stdout
if output != "stdout" {
var err error
if out, err = os.OpenFile(output, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644); err != nil {
out = os.Stdout
fmt.Println(err)
}
}
return middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: log.New(out, "", log.LstdFlags)})
}
func H(r *requests.ServeMux, path string) {
r.Route(path, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(r.URL.Path))
})
}
// curl http://127.0.0.1:8080 -F '[email protected]' -F '[email protected]' -vvvv
func main() {
var token = flag.String("token", "byFjRL3cr4v656AojKjW", "上传使用的TOKEN")
var prefix = flag.String("prefix", "/tmp", "上传文件的前缀路径")
var listen = flag.String("listen", "0.0.0.0:8080", "监听端口")
var output = flag.String("output", "stdout", "日志输出")
flag.Parse()
r := requests.NewServeMux(requests.URL(*listen), requests.Use(RequestLog(*output), middleware.Recoverer))
r.Route("/echo", Echo)
r.Route("/ping", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("pong")) })
r.Route("/_upload", requests.ServeUpload(*prefix), requests.Use(Token(*token), Method("POST")))
r.Route("/backup", func(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/backup", http.FileServer(http.Dir(*prefix))).ServeHTTP(w, r)
}, requests.Use(Method("GET")))
r.Route("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/backup/", http.StatusFound)
})
r.Pprof()
err := requests.ListenAndServe(context.Background(), r)
fmt.Println(err)
}