-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
112 lines (101 loc) · 3.21 KB
/
Copy pathmain.go
File metadata and controls
112 lines (101 loc) · 3.21 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
package main
import (
"flag"
wf "github.com/chuccp/go-web-frame"
auth2 "github.com/chuccp/go-web-frame/component/auth"
"github.com/chuccp/go-web-frame/component/cors"
"github.com/chuccp/go-web-frame/component/schedule"
"github.com/chuccp/go-web-frame/config"
"github.com/chuccp/go-web-frame/core"
"github.com/chuccp/http2smtp/db"
"github.com/chuccp/http2smtp/runner"
"github.com/chuccp/go-web-frame/log"
"github.com/chuccp/go-web-frame/util"
"github.com/chuccp/http2smtp/auth"
"github.com/chuccp/http2smtp/model"
"github.com/chuccp/http2smtp/rest"
"github.com/chuccp/http2smtp/service"
"go.uber.org/zap"
)
func createAPP() (*wf.WebFrame, error) {
var webPort int
var apiPort int
var storageRoot string
flag.IntVar(&webPort, "web_port", 0, "web port")
flag.IntVar(&apiPort, "api_port", 0, "api port")
flag.StringVar(&storageRoot, "storage_root", "./", "storage root")
flag.Parse()
webPort = util.GetEnvIntOrDefault("web_port", webPort)
apiPort = util.GetEnvIntOrDefault("api_port", apiPort)
storageRoot = util.GetEnvOrDefault("storage_root", storageRoot)
fileConfig, err := config.LoadSingleFileConfig("config.ini")
if err != nil {
return nil, err
}
init := fileConfig.GetBoolOrDefault("core.init", false)
if webPort > 0 || apiPort > 0 {
if apiPort == 0 {
apiPort = webPort
}
if webPort == 0 {
webPort = apiPort
}
fileConfig.Put("core.isdocker", "true")
fileConfig.Put("manage.port", webPort)
fileConfig.Put("api.port", apiPort)
}
if len(storageRoot) > 0 {
fileConfig.Put("core.cachepath", storageRoot)
}
builder := wf.NewBuilder(fileConfig)
if webPort == 0 {
webPort = model.ManagePort
}
restGroupBuilder := core.NewRestGroupBuilder()
restGroupBuilder.Rest(&rest.Set{}, &rest.User{}, &rest.Token{}, &rest.Mail{}, &rest.Smtp{}, &rest.Schedule{}, &rest.Log{})
restGroupBuilder.Port(webPort)
restGroupBuilder.ContextPath("/api")
restGroupBuilder.Filter(cors.NewCrosFilter(), auth2.NewAuthenticationFilter[*model.User](&auth.Authentication{}))
restGroup := restGroupBuilder.Build()
builder.RestGroup(restGroup)
apiRestGroupBuilder := core.NewRestGroupBuilder()
apiRestGroupBuilder.Rest(&rest.API{})
if apiPort == 0 {
apiPort = model.ApiPort
}
apiRestGroupBuilder.Port(apiPort)
builder.RestGroup(apiRestGroupBuilder.Build())
manageModelGroupBuilder := core.NewModelGroupBuilder()
manageModelGroupBuilder.AutoCreateTable(true)
manageModelGroupBuilder.Model(
&model.MailModel{},
&model.SMTPModel{},
&model.TokenModel{},
&model.ScheduleModel{},
&model.LogModel{},
&model.UserModel{},
)
if init || fileConfig.GetBoolOrDefault("core.dbinit", false) {
connection, err := db.GetDb(fileConfig)
if err != nil {
return nil, err
}
manageModelGroupBuilder.DB(connection)
}
builder.Service(&service.TokenService{}, &service.ScheduleService{}, &service.LogService{}, &service.SmtpService{}, &service.UserService{})
builder.Runner(schedule.NewScheduleWithSeconds(), &runner.ScheduleRunner{})
builder.ModelGroup(manageModelGroupBuilder.Build())
return builder.Build(), nil
}
func main() {
app, err := createAPP()
if err != nil {
log.Panic("启动失败", zap.Error(err))
return
}
err = app.Start()
if err != nil {
log.Panic("启动失败", zap.Error(err))
return
}
}