forked from chengshiwen/influx-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (66 loc) · 1.68 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"runtime"
"time"
"github.com/chengshiwen/influx-proxy/backend"
"github.com/chengshiwen/influx-proxy/service"
"github.com/chengshiwen/influx-proxy/util"
)
var (
ConfigFile string
Version bool
GitCommit = "not build"
BuildTime = "not build"
)
func init() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
log.SetOutput(os.Stdout)
flag.StringVar(&ConfigFile, "config", "proxy.json", "proxy config file")
flag.BoolVar(&Version, "version", false, "proxy version")
flag.Parse()
}
func main() {
if Version {
fmt.Printf("Version: %s\n", backend.Version)
fmt.Printf("Git commit: %s\n", GitCommit)
fmt.Printf("Build time: %s\n", BuildTime)
fmt.Printf("Go version: %s\n", runtime.Version())
fmt.Printf("OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
return
}
cfg, err := backend.NewFileConfig(ConfigFile)
if err != nil {
fmt.Printf("illegal config file: %s\n", err)
return
}
log.Printf("version: %s, commit: %s, build: %s", backend.Version, GitCommit, BuildTime)
cfg.PrintSummary()
err = util.MakeDir(cfg.DataDir)
if err != nil {
log.Fatalln("create data dir error")
return
}
mux := http.NewServeMux()
service.NewHttpService(cfg).Register(mux)
server := &http.Server{
Addr: cfg.ListenAddr,
Handler: mux,
IdleTimeout: time.Duration(cfg.IdleTimeout) * time.Second,
}
if cfg.HTTPSEnabled {
log.Printf("https service start, listen on %s", server.Addr)
err = server.ListenAndServeTLS(cfg.HTTPSCert, cfg.HTTPSKey)
} else {
log.Printf("http service start, listen on %s", server.Addr)
err = server.ListenAndServe()
}
if err != nil {
log.Print(err)
return
}
}