/
main.go
88 lines (72 loc) · 1.86 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
82
83
84
85
86
87
88
package main
import (
"bmw"
"flag"
"fmt"
"github.com/golang/glog"
"github.com/spf13/viper"
"gopkg.in/fsnotify.v1"
)
var listenPort = ":8085"
func main() {
flag.Parse()
loadConfig("./", "config")
handler := &handler{}
batchHandler := &batchHandler{}
routes := []bmw.SingleRoute{
bmw.SingleRoute{
Path: "service/resource/upload",
Topic: "upload",
Handler: handler,
},
bmw.SingleRoute{
Path: "service/resource/upload/batch",
Topic: "upload-batch",
Handler: batchHandler,
},
}
handlerRoute := new(bmw.MultiHandlerRoutes)
handlerRoute.Host = listenPort
handlerRoute.Routes = routes
goChan := bmw.GoChannelConfig{}
bmw := bmw.NewMultiBMW(goChan, handlerRoute)
engine := bmw.GetAPI().GetEngine()
engine.GET("service/resource/key", findKeyQuery)
bmw.Start()
}
func loadConfig(path, name string) {
viper.SetConfigName(name)
viper.AddConfigPath(path)
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
viper.WatchConfig()
viper.OnConfigChange(func(event fsnotify.Event) {
glog.Info(event.String())
setVar()
initMysql()
})
setVar()
initMysql()
}
func setVar() {
listenPort = viper.GetString("listenPort")
QiniuAccessToken = viper.GetString("qiniuAccessToken")
QiniuSecretToken = viper.GetString("qiniuSecretToken")
S3Region = viper.GetString("s3Region")
S3Credentials = viper.GetString("s3Credentials")
bucketDomainMap = viper.GetStringMapString("bucketDomainMap")
allBuckets = viper.GetStringSlice("allBuckets")
mysqlConn = viper.GetString("mysqlConn")
}
type handler struct {
}
func (h *handler) Handle(job bmw.RetryJob) error {
return handleRequest(&job)
}
type batchHandler struct {
}
func (h *batchHandler) Handle(job bmw.RetryJob) error {
return handleBatchRequest(&job)
}