forked from shell909090/influx-proxy
-
Notifications
You must be signed in to change notification settings - Fork 112
/
main.go
77 lines (67 loc) · 1.8 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
// Copyright 2021 Shiwen Cheng. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"runtime"
"time"
"github.com/chengshiwen/influx-proxy/backend"
"github.com/chengshiwen/influx-proxy/service"
)
var (
configFile string
version bool
)
func init() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
log.SetOutput(os.Stdout)
flag.StringVar(&configFile, "config", "proxy.json", "proxy config file with json/yaml/toml format")
flag.BoolVar(&version, "version", false, "proxy version")
flag.Parse()
}
func printVersion() {
fmt.Printf("Version: %s\n", backend.Version)
fmt.Printf("Git commit: %s\n", backend.GitCommit)
fmt.Printf("Build time: %s\n", backend.BuildTime)
fmt.Printf("Go version: %s\n", runtime.Version())
fmt.Printf("OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
}
func main() {
if version {
printVersion()
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, backend.GitCommit, backend.BuildTime)
cfg.PrintSummary()
mux := service.NewServeMux()
service.NewHttpService(cfg).Register(mux)
server := &http.Server{
Addr: cfg.ListenAddr,
Handler: mux,
IdleTimeout: time.Duration(cfg.IdleTimeout) * time.Second,
}
if cfg.HTTPSEnabled {
if cfg.TLS != nil {
server.TLSConfig, _ = cfg.TLS.Parse()
}
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
}
}