-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (90 loc) · 2.38 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
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"k8s_test/global"
"k8s_test/internal/model"
"k8s_test/pkg/logger"
"k8s_test/pkg/setting"
"log"
"time"
"k8s_test/internal/controller"
"github.com/gin-gonic/gin"
"gopkg.in/natefinch/lumberjack.v2"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
router := gin.Default()
router.Static("/static", "./pkg/static")
router.LoadHTMLGlob("pkg/templates/*")
// todo 参数校验
mainC := controller.NewMainController()
router.GET("/index", mainC.GetIndex)
deployC := controller.NewDeploymentController()
router.POST("/deployment", deployC.CreateDeployment)
router.GET("/deployment", deployC.ListDeployment)
router.DELETE("/deployment", deployC.DeleteDeployment)
router.Run(":" + global.ServerSetting.HttpPort)
}
func init() {
err := setupSettings()
if err != nil {
log.Fatalf("init.setupSetting failed.err:%v", err)
}
err = setupLogger()
if err != nil {
log.Fatalf("init.setupLogger failed.err: %v", err)
}
err = setupKubernetes()
if err != nil {
log.Fatalf("init setupKubernetes failed.err:%v", err)
}
}
func setupSettings() error {
s, err := setting.NewSetting()
if err != nil {
return err
}
err = s.ReadSection("Server", &global.ServerSetting)
if err != nil {
return err
}
err = s.ReadSection("App", &global.AppSetting)
if err != nil {
return err
}
err = s.ReadSection("Database", &global.DatabaseSetting)
if err != nil {
return err
}
err = s.ReadSection("Kubernete", &global.KuberneteSetting)
global.ServerSetting.ReadTimeout *= time.Second
global.ServerSetting.WriteTimeout *= time.Second
fmt.Println(global.ServerSetting, global.AppSetting, global.KuberneteSetting)
return nil
}
func setupLogger() error {
// todo if LogSavePath not exist should create
global.Logger = logger.NewLogger(&lumberjack.Logger{
Filename: global.AppSetting.LogSavePath + "/" + global.AppSetting.LogFileName + global.AppSetting.LogFileExt,
MaxSize: 600,
MaxAge: 10,
LocalTime: true,
}, "", log.LstdFlags).WithCaller(2)
return nil
}
func setupDBEngine() error {
var err error
global.DBEngine, err = model.NewDBEngine(global.DatabaseSetting)
if err != nil {
return err
}
return nil
}
func setupKubernetes() error {
var err error
global.KubeConfig, err = clientcmd.BuildConfigFromFlags("", global.KuberneteSetting.KubeConfig)
if err != nil {
return fmt.Errorf("client cmd build config from flags failed.err:%v", err)
}
return nil
}