-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (87 loc) · 1.98 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
101
102
103
104
105
106
107
108
package main
import (
"log"
"os"
"time"
"github.com/changqings/gin-web/handle"
"github.com/changqings/gin-web/router"
"github.com/changqings/gin-web/db"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func init() {
// // first check as master or wait
// startCh := make(chan int, 1)
// ticker := time.NewTicker(time.Second * 10)
// if db.ShouldRunAsMaster {
// close(startCh)
// } else {
// slog.Info("waitting to be master...")
// for range ticker.C {
// // slog.Info("main debug", "shouldRunAsMaster", db.ShouldRunAsMaster)
// if db.ShouldRunAsMaster {
// close(startCh)
// // 执行ticker.Stop()并不会关闭通信,只会不继续发送, 要手动退出循环
// ticker.Stop()
// break
// }
// }
// }
// <-startCh
// slog.Info("running as master...")
//// lock task
etcd := db.NewEtcd()
go db.LockTask01(etcd)
go db.LockTask02(etcd)
time.Sleep(time.Second * 10)
os.Exit(0)
}
//
func main() {
main_func := func() {
// gin config
gin.SetMode(gin.DebugMode)
app := gin.Default()
// middlewares write or find from offical
// and you can find some offical on `https://github.com/gin-gonic/contrib`
app.Use(cors.Default())
// middle.Limiter(1*time.Second),
// middle.Middle_01(),
// middle.Middle_02(),
// middle.Middle_03())
// middle.QuerySpendTime())
// simple mothed usage
app.GET("/getname", handle.GetName("scq"))
app.GET("/json", handle.P_list())
// security usage
{
sec_group := app.Group("/sec")
sec_group.Use(gin.BasicAuth(gin.Accounts{
"user01": "PasSw0rd!",
}))
sec_group.GET("/info", handle.Some_sec_info())
}
// metrics usage
// {
// if err := router.TxMetrics(app); err != nil {
// log.Fatal(err)
// }
// }
// pgsql usage
{
router.PgRouters(app)
}
// cicd
{
router.CICDRouter(app)
}
// main run
err := app.Run(":8080")
if err != nil {
log.Fatal(err)
}
}
// master election
etcd := db.NewEtcd()
etcd.Campaign(main_func)
}