-
Notifications
You must be signed in to change notification settings - Fork 32
/
init.go
149 lines (123 loc) · 3.81 KB
/
init.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"github.com/7sDream/rikka/common/logger"
"github.com/7sDream/rikka/plugins"
"github.com/7sDream/rikka/plugins/fs"
"github.com/7sDream/rikka/plugins/qiniu"
"github.com/7sDream/rikka/plugins/tencent/ci"
"github.com/7sDream/rikka/plugins/tencent/cos"
"github.com/7sDream/rikka/plugins/upai"
"github.com/7sDream/rikka/plugins/weibo"
)
var (
// Map from plugin name to object
pluginMap = make(map[string]plugins.RikkaPlugin)
// Command line arguments var
argBindIPAddress *string
argPort *int
argPassword *string
argMaxSizeByMB *float64
argPluginStr *string
argLogLevel *int
argHTTPS *bool
argCertDir *string
argAllowOrigin *string
// concat socket from ip address and port
socket string
// The used plugin
thePlugin plugins.RikkaPlugin
)
// --- Init and check ---
func createSignalHandler(handlerFunc func()) (func(), chan os.Signal) {
signalChain := make(chan os.Signal, 1)
return func() {
for range signalChain {
handlerFunc()
}
}, signalChain
}
// registerSignalHandler register a handler for process Ctrl + C
func registerSignalHandler(handlerFunc func()) {
signalHandler, channel := createSignalHandler(handlerFunc)
signal.Notify(channel, os.Interrupt)
go signalHandler()
}
func init() {
registerSignalHandler(func() {
l.Info("Receive interrupt signal")
l.Info("Rikka have to go to sleep, see you tomorrow")
os.Exit(0)
})
initPluginList()
initArgVars()
flag.Parse()
l.Info("Args bindIP =", *argBindIPAddress)
l.Info("Args port =", *argPort)
l.Info("Args password =", *argPassword)
l.Info("Args maxFileSize =", *argMaxSizeByMB, "MB")
l.Info("Args loggerLevel =", *argLogLevel)
l.Info("Args https =", *argHTTPS)
l.Info("Args cert dir =", *argCertDir)
l.Info("Args plugin =", *argPluginStr)
if *argPort == 0 {
if *argHTTPS {
*argPort = 443
} else {
*argPort = 80
}
}
if *argBindIPAddress == ":" {
socket = *argBindIPAddress + strconv.Itoa(*argPort)
} else {
socket = *argBindIPAddress + ":" + strconv.Itoa(*argPort)
}
logger.SetLevel(*argLogLevel)
runtimeEnvCheck()
}
func initPluginList() {
pluginMap["fs"] = fs.Plugin
pluginMap["qiniu"] = qiniu.Plugin
pluginMap["upai"] = upai.Plugin
pluginMap["weibo"] = weibo.Plugin
pluginMap["tccos"] = cos.Plugin
pluginMap["tcci"] = ci.Plugin
}
func initArgVars() {
argBindIPAddress = flag.String("bind", ":", "Bind ip address, use : for all address")
argPort = flag.Int("port", 0, "Server port, 0 means use 80 when disable HTTPS, 443 when enable")
argPassword = flag.String("pwd", "rikka", "The password need provided when upload")
argMaxSizeByMB = flag.Float64("size", 5, "Max file size by MB")
argLogLevel = flag.Int(
"level", logger.LevelInfo,
fmt.Sprintf("Log level, from %d to %d", logger.LevelDebug, logger.LevelError),
)
argHTTPS = flag.Bool("https", false, "Use HTTPS")
argCertDir = flag.String("certDir", ".", "Where to find HTTPS cert files(cert.pem, key.pem)")
argAllowOrigin = flag.String("corsAllowOrigin", "", "Enable upload api CORS support, default is empty(disable). Set this to a origin, or * to enable for all origin")
// Get name array of all available plugins, show in `rikka -h`
pluginNames := make([]string, 0, len(pluginMap))
for k := range pluginMap {
pluginNames = append(pluginNames, k)
}
argPluginStr = flag.String(
"plugin", "fs",
"What plugin use to save file, selected from "+fmt.Sprintf("%v", pluginNames),
)
}
func runtimeEnvCheck() {
l.Info("Check runtime environment")
l.Debug("Try to find plugin", *argPluginStr)
// Make sure plugin be selected exist
if plugin, ok := pluginMap[*argPluginStr]; ok {
thePlugin = plugin
l.Debug("Plugin", *argPluginStr, "found")
} else {
l.Fatal("Plugin", *argPluginStr, "not exist")
}
l.Info("All runtime environment check passed")
}