forked from kentimes/etcd-manage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·72 lines (57 loc) · 1.55 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/shiguanghuxian/etcd-manage/conf"
"github.com/shiguanghuxian/etcd-manage/routers"
"github.com/shiguanghuxian/etcd-manage/e3ch"
"github.com/gin-gonic/gin"
"go.etcd.io/etcd/version"
)
const (
PROGRAM_NAME = "e3w"
PROGRAM_VERSION = "0.0.2"
)
var configFilepath string
func init() {
flag.StringVar(&configFilepath, "conf", "conf/config.default.ini", "config file path")
rev := flag.Bool("rev", false, "print rev")
flag.Parse()
if *rev {
fmt.Printf("[%s v%s]\n[etcd %s]\n",
PROGRAM_NAME, PROGRAM_VERSION,
version.Version,
)
os.Exit(0)
}
}
func main() {
config, err := conf.Init(configFilepath)
if err != nil {
panic(err)
}
client, err := e3ch.NewE3chClient(config)
if err != nil {
panic(err)
}
router := gin.Default()
router.Use(MiddleWare())
router.UseRawPath = true
routers.InitRouters(router, config, client)
router.Run(":" + config.Port)
}
func MiddleWare() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
c.Header("Access-Control-Allow-Headers", "Origin,Content-Type, Authorization, Access-Control-Allow-Origin, X-Etcd-Username, X-Etcd-Password")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
//放行所有OPTIONS方法
if c.Request.Method == "OPTIONS" {
c.Status(http.StatusOK)
}
}
}