-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
67 lines (56 loc) · 1.57 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/jacexh/multiconfig"
"github.com/valyala/fasthttp"
"github.com/wosai/deepmock/application"
"github.com/wosai/deepmock/infrastructure"
"github.com/wosai/deepmock/misc"
"github.com/wosai/deepmock/option"
"github.com/wosai/deepmock/router"
"go.uber.org/zap"
)
var (
version = "(git commit revision)"
)
func main() {
loader := multiconfig.NewWithPathAndEnvPrefix("", "DEEPMOCK")
opt := new(option.Option)
loader.MustLoad(opt)
// 连接数据库
db := infrastructure.BuildDBConnection(opt.DB)
mem := infrastructure.NewExecutorRepository(1000)
job := infrastructure.NewJob(2 * time.Second)
// 初始化service
application.BuildMockApplication(
infrastructure.NewRuleRepository(db),
mem,
job,
)
// 初始化http handler
app := router.BuildRouter()
server := &fasthttp.Server{
Name: "DeepMock Service",
Handler: app.Handler,
Concurrency: 1024 * 1024,
}
misc.Logger.Info("deepmock is running on port "+opt.Server.Port, zap.String("version", version))
errChan := make(chan error, 1)
go func() {
if opt.Server.KeyFile != "" && opt.Server.CertFile != "" {
errChan <- server.ListenAndServeTLS(opt.Server.Port, opt.Server.CertFile, opt.Server.KeyFile)
} else {
errChan <- server.ListenAndServe(opt.Server.Port)
}
}()
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
errChan <- fmt.Errorf("caught signal: %s", (<-sigs).String())
}()
misc.Logger.Panic("deepmock is shutdown", zap.Error(<-errChan))
}