/
command.go
87 lines (78 loc) 路 1.91 KB
/
command.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
package web
import (
"context"
"github.com/duxweb/go-fast/global"
"github.com/duxweb/go-fast/monitor"
"github.com/duxweb/go-fast/route"
"github.com/duxweb/go-fast/task"
"github.com/gookit/color"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/urfave/cli/v2"
"os"
"os/signal"
"syscall"
"time"
)
func Command() []*cli.Command {
cmd := &cli.Command{
Category: "service",
Name: "web",
Usage: "starting the web service",
Action: func(cCtx *cli.Context) error {
ctx, stop := signal.NotifyContext(context.Background(),
os.Interrupt,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTERM)
defer stop()
task.ListenerTask("dux.monitor", monitor.Control)
task.ListenerScheduler("*/1 * * * *", "dux.monitor", map[string]any{}, task.PRIORITY_LOW)
// 鍚姩浠诲姟鏈嶅姟
go func() {
task.StartScheduler()
}()
// 鍚姩闃熷垪鏈嶅姟
go func() {
task.Add("ping", &map[string]any{})
task.StartQueue()
}()
// 鍚姩 web 鏈嶅姟
Start()
<-ctx.Done()
err := global.Injector.Shutdown()
if err != nil {
color.Errorln("Stop service")
}
ctx, cancel := context.WithTimeout(global.CtxBackground, 10*time.Second)
defer cancel()
if err = global.App.Shutdown(ctx); err != nil {
color.Errorln(err.Error())
}
return nil
},
}
routeList := &cli.Command{
Name: "route:list",
Usage: "viewing the route list",
Category: "dev",
Action: func(ctx *cli.Context) error {
for name, list := range route.Routes {
color.Println(name)
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Name", "Method", "Path"})
rows := make([]table.Row, 0)
for _, item := range list.ParseData(list.Prefix) {
rows = append(rows, table.Row{item["name"], item["method"], item["path"]})
}
t.AppendRows(rows)
t.Render()
}
return nil
},
}
return []*cli.Command{
cmd,
routeList,
}
}