/
main.go
68 lines (56 loc) · 1.53 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
package main
import (
"context"
"fmt"
"github.com/julienschmidt/httprouter"
up "github.com/rolandhe/sizedupload"
"github.com/rolandhe/sizedupload/example/file"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"log"
"net/http"
"os"
"time"
)
// confSizeUpload 初始化相关依赖
func confSizeUpload() {
var err error
// 可以调用NewConfSizeProvider方法,它从./conf/sizeconfig.yml读取
up.UploadConfig.SizeProvider, err = up.NewConfSizeProviderByConfFile("./example/conf/sizeconfig.yml")
if err != nil {
log.Println(err)
panic(err)
}
up.UploadConfig.AuthFunc = func(req *http.Request, ctx context.Context) int64 {
return 1004
}
up.UploadConfig.ResultOutput = up.NewJsonResultOutput()
// 配置日志输出,如果不配置,会使用缺省日志输出
//sizedupload.UploadConfig.Logger =
up.UploadConfig.MemoryLimit = 1024 * 1024
}
func init() {
confSizeUpload()
}
func main() {
router := httprouter.New()
router.POST("/fgw/upload/*path", func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
up.SizeUploadHandler(req, params.ByName("path"), w, file.DoProcessFile)
})
h2s := &http2.Server{}
host := fmt.Sprintf("0.0.0.0:%d", 8080)
server := &http.Server{
Addr: host,
Handler: h2c.NewHandler(router, h2s),
IdleTimeout: time.Minute * 30,
}
log.Printf("Listening [0.0.0.0:%d]...\n", 8080)
checkErr(server.ListenAndServe(), "while listening")
}
func checkErr(err error, msg string) {
if err == nil {
return
}
log.Printf("ERROR: %s: %s\n", msg, err)
os.Exit(1)
}