-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
92 lines (77 loc) · 2 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
package main
import (
"context"
_ "embed"
"github.com/samber/lo"
"strings"
"github.com/glennliao/apijson-go"
_ "github.com/glennliao/apijson-go/drivers/goframe"
"github.com/glennliao/apijson-go/drivers/goframe/web"
"github.com/glennliao/bookmark/app"
_ "github.com/glennliao/bookmark/packed"
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/util/grand"
)
//go:embed config.yaml.example
var configExample string
var (
Main = &gcmd.Command{
Name: "server",
Brief: "start http server",
Description: "",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
// 数据库同步前判断
tables, err := g.DB().Tables(ctx)
if err != nil {
g.Log().Fatal(ctx, err)
}
a := apijson.Load(app.App)
w := web.New(a)
s := g.Server()
s.Group("/api", func(group *ghttp.RouterGroup) {
group.Middleware(app.Cors, app.Response, app.Auth)
group.Bind(app.Api{
A: a,
})
})
s.Group("/api/data", func(group *ghttp.RouterGroup) {
group.Middleware(app.Cors, app.Auth)
w.Bind(group)
})
if g.Res().Contains("dist") {
s.AddStaticPath("/", "dist")
s.AddStaticPath("/assets", "dist/assets")
}
if !lo.Contains(tables, "config") && lo.Contains(tables, "group_bookmark") {
app.ToV0Dot11(ctx)
}
s.Run()
return nil
},
}
Init = &gcmd.Command{
Name: "init",
Brief: "init config file",
Description: "",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
if !gfile.Exists("config.yaml") {
configExample = strings.Replace(configExample, "{{secret}}", grand.S(32), 1)
gfile.PutContents("config.yaml", configExample)
}
return
},
}
)
func main() {
err := Main.AddCommand(Init)
if err != nil {
panic(err)
}
Main.Run(gctx.New())
}