-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (79 loc) · 1.7 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
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/urfave/cli/v2"
"os"
"sync"
"time"
)
var (
ctxServer, cancel = context.WithCancel(context.Background())
wgServer = sync.WaitGroup{}
)
func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Load configuration from `FILE`",
},
&cli.StringFlag{
Name: "mode",
Aliases: []string{"m"},
Usage: "Server Type, ``(default): api and timer server, `api`: api server, `timer`: timer server",
},
},
Action: runServer,
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
}
wgServer.Wait()
}
func runServer(ctx *cli.Context) error {
configFilePath := ctx.String("config")
fmt.Println("config path: ", configFilePath)
//service mode
mode := ctx.String("mode")
if mode == "api" {
fmt.Println("init api server")
initApiServer()
} else if mode == "timer" {
fmt.Println("init timer server")
initTimerServer()
} else {
fmt.Println("init api server and timer server")
initTimerServer()
initApiServer()
}
return nil
}
func initApiServer() {
r := gin.Default()
r.GET("/api/test", func(c *gin.Context) {
c.JSON(200, "main hello world v12.0.0")
})
r.Run("0.0.0.0:8899") // 监听并在 0.0.0.0:8899 上启动服务
}
func initTimerServer() {
ticker := time.NewTicker(time.Second * 2)
wgServer.Add(1)
go func() {
for {
select {
case <-ticker.C:
fmt.Println("timer start ...")
case <-ctxServer.Done():
fmt.Println("timer done")
wgServer.Done()
return
}
}
}()
}
//CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
//docker build -f ./docker/Dockerfile -t go-test:v13 .
//111111