-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
145 lines (131 loc) · 3.25 KB
/
main.go
File metadata and controls
145 lines (131 loc) · 3.25 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"comma/pkg/model"
"embed"
"io/fs"
"log"
"net/http"
"time"
adminHttpServer "comma/pkg/server/http/admin"
gatewayHttpServer "comma/pkg/server/http/gateway"
"comma/pkg/server/http/gateway/middleware"
gatewayJsonRpc "comma/pkg/server/jsonrpc/gateway"
segmentJsonRpc "comma/pkg/server/jsonrpc/segment"
httpServer "github.com/caicaispace/gohelper/server/http"
jsonrpcServer "github.com/caicaispace/gohelper/server/jsonrpc"
//bannedJsonRpc `comma/pkg/service/banned/server/jsonrpc`
bannedService "comma/pkg/service/banned"
"github.com/caicaispace/gohelper/config"
"github.com/caicaispace/gohelper/metric"
"github.com/caicaispace/gohelper/orm/gorm"
"github.com/caicaispace/gohelper/server"
"github.com/caicaispace/gohelper/setting"
"github.com/caicaispace/gohelper/task"
"golang.org/x/sync/errgroup"
)
const (
TRACE_URL = ""
)
type Service struct {
AdminIsOpen bool
JsonrpcIsOpen bool
GrpcIsOpen bool
}
var (
//go:embed static/*
static embed.FS
group errgroup.Group
services = &Service{
AdminIsOpen: true,
JsonrpcIsOpen: true,
GrpcIsOpen: false,
}
)
func beforeStart() {
if config.GetInstance().GetEnv() == "dev" {
gorm.GetInstance().AddConnWithDns(config.GetInstance().GetDbDns(), "")
}
if setting.Database.AutoMigrate {
gorm.GetInstance().GetDB("").AutoMigrate(
&model.DictBanned{},
&model.DictFestival{},
&model.DictHighFrequency{},
&model.DictHyponym{},
&model.DictPinyin{},
&model.DictProject{},
&model.DictRedirect{},
&model.DictStop{},
&model.DictSynonyms{},
&model.DictVersion{},
&model.DictWeight{},
&model.DictWord{},
)
}
if config.GetInstance().GetMetricIsEnable() {
conf := config.GetInstance().GetMetric()
metric.StartMetricsPush(task.NewRunner(), metric.NewMetricCfg(
conf.Job,
conf.Instance,
conf.Address,
time.Second*time.Duration(conf.IntervalSync),
))
}
loadService()
}
func loadService() {
bannedService.GetInstance()
}
func adminServerStart(serverAddr string) error {
s := httpServer.NewServer()
s.AddServerAddr(serverAddr)
adminHttpServer.NewServer(s.Engine)
fe, _ := fs.Sub(static, "static")
s.Engine.StaticFS("ui", http.FS(fe))
s.Start()
return nil
}
func gatewayServerStart(serverAddr string) error {
s := httpServer.NewServer()
s.AddServerAddr(serverAddr)
s.AddMiddlewares(
middleware.NewStats(),
// middleware.NewPyroscope(),
)
gatewayHttpServer.NewServer(s)
s.Start()
return nil
}
func jsonRpcServerStart(serverAddr string) error {
s := jsonrpcServer.NewServer()
s.SetServerAddr(serverAddr)
// s.RegisterService(bannedJsonRpc.GetInstance())
s.RegisterService(segmentJsonRpc.GetInstance())
s.RegisterService(gatewayJsonRpc.GetInstance())
s.Start()
return nil
}
func main() {
server.New()
beforeStart()
group.Go(func() error {
// gateway server
return gatewayServerStart(config.GetInstance().GetServerHost() + ":9400")
})
group.Go(func() error {
// jsonrpc service
if services.JsonrpcIsOpen {
return jsonRpcServerStart(config.GetInstance().GetServerHost() + ":9401")
}
return nil
})
group.Go(func() error {
// admin server
if services.AdminIsOpen {
return adminServerStart(config.GetInstance().GetServerHost() + ":9402")
}
return nil
})
if err := group.Wait(); err != nil {
log.Fatal(err)
}
}