-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
104 lines (94 loc) · 2.71 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
package main
import (
"entgo.io/contrib/entgql"
"github.com/99designs/gqlgen/graphql/handler"
_ "github.com/go-sql-driver/mysql"
"github.com/tsingsun/woocoo"
"github.com/tsingsun/woocoo/contrib/gql"
"github.com/tsingsun/woocoo/pkg/conf"
"github.com/tsingsun/woocoo/pkg/log"
"github.com/tsingsun/woocoo/web"
"github.com/tsingsun/woocoo/web/handler/authz"
"github.com/woocoos/entco/ecx"
"github.com/woocoos/entco/ecx/oteldriver"
"github.com/woocoos/entco/gqlx"
"github.com/woocoos/entco/pkg/identity"
"github.com/woocoos/entco/pkg/snowflake"
"github.com/woocoos/workflow/api/graphql"
"github.com/woocoos/workflow/ent"
_ "github.com/woocoos/workflow/ent/runtime"
"github.com/woocoos/workflow/service/deployment"
"github.com/woocoos/workflow/service/workflow"
"net/http"
)
var (
dbClient *ent.Client
)
func main() {
app := woocoo.New()
cnf := app.AppConfiguration()
dbClient = buildEntClient(cnf)
defer dbClient.Close()
err := snowflake.SetDefaultNode(app.AppConfiguration().Sub("snowflake"))
if err != nil {
log.Fatal(err)
}
wf, err := workflow.NewService(cnf)
if err != nil {
log.Fatal(err)
}
wf.Db = dbClient
ds := &deployment.Service{
WFDB: dbClient,
Engine: wf,
}
webSrv := buildApiServer(cnf, ds)
app.RegisterServer(webSrv)
if cnf.Bool("ui.enabled") {
app.RegisterServer(buildUiServer(cnf))
}
if err := app.Run(); err != nil {
log.Fatal(err)
}
}
func buildApiServer(cnf *conf.AppConfiguration, service *deployment.Service) *web.Server {
webSrv := web.New(web.WithConfiguration(cnf.Sub("web")),
web.WithGracefulStop(),
web.RegisterMiddleware(gql.New()),
web.RegisterMiddleware(authz.New()),
identity.RegistryTenantIDMiddleware(),
)
// 如果需要设置gqlserver的中间件,可以使用第一个返回值
gqlsrv := handler.NewDefaultServer(
graphql.NewSchema(
graphql.WithEntClient(dbClient),
graphql.WithDeploymentService(service),
),
)
gqlsrv.AroundResponses(gqlx.ContextCache())
gqlsrv.AroundResponses(gqlx.SimplePagination())
// mutation事务
gqlsrv.Use(entgql.Transactioner{TxOpener: dbClient})
if err := gql.RegisterGraphqlServer(webSrv, gqlsrv); err != nil {
log.Fatal(err)
}
return webSrv
}
func buildEntClient(cnf *conf.AppConfiguration) *ent.Client {
pd := oteldriver.BuildOTELDriver(cnf, "store.workflow")
pd = ecx.BuildEntCacheDriver(cnf, pd)
scfg := workflow.AlternateSchema()
if cnf.Development {
dbClient = ent.NewClient(ent.Driver(pd), ent.Debug(), scfg)
} else {
dbClient = ent.NewClient(ent.Driver(pd), scfg)
}
return dbClient
}
func buildUiServer(cnf *conf.AppConfiguration) *web.Server {
uiSrv := web.New(web.WithConfiguration(cnf.Sub("ui")),
web.WithGracefulStop(),
)
uiSrv.Router().StaticFS("/", http.Dir("../../web/build"))
return uiSrv
}