-
Notifications
You must be signed in to change notification settings - Fork 400
/
main.go
91 lines (76 loc) · 2.17 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
package main
import (
"context"
"flag"
"fmt"
"net"
"net/http"
"os/signal"
"syscall"
"code.pfad.fr/risefront"
"github.com/0xJacky/Nginx-UI/internal/kernel"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/router"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/uozi-tech/cosy"
cKernel "github.com/uozi-tech/cosy/kernel"
"github.com/uozi-tech/cosy/logger"
cRouter "github.com/uozi-tech/cosy/router"
cSettings "github.com/uozi-tech/cosy/settings"
)
func Program(confPath string) func(l []net.Listener) error {
return func(l []net.Listener) error {
defer logger.Sync()
defer logger.Info("Server exited")
cosy.RegisterModels(model.GenerateAllModel()...)
cosy.RegisterAsyncFunc(kernel.Boot, router.InitRouter)
// Initialize settings package
settings.Init(confPath)
// Set gin mode
gin.SetMode(cSettings.ServerSettings.RunMode)
// Initialize logger package
logger.Init(cSettings.ServerSettings.RunMode)
defer logger.Sync()
// Gin router initialization
cRouter.Init()
// Kernel boot
cKernel.Boot()
srv := &http.Server{
Handler: cRouter.GetEngine(),
}
// defer Shutdown to wait for ongoing requests to be served before returning
defer func(srv *http.Server, ctx context.Context) {
err := srv.Shutdown(ctx)
if err != nil {
logger.Fatal(err)
}
}(srv, context.Background())
return srv.Serve(l[0])
}
}
func main() {
var confPath string
flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file")
flag.Parse()
settings.Init(confPath)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
err := risefront.New(ctx, risefront.Config{
Run: Program(confPath),
Name: "nginx-ui",
Addresses: []string{fmt.Sprintf("%s:%d", cSettings.ServerSettings.Host, cSettings.ServerSettings.Port)},
ErrorHandler: func(kind string, err error) {
if errors.Is(err, net.ErrClosed) {
return
}
logger.Error(kind, err)
},
})
if err != nil && !errors.Is(err, context.DeadlineExceeded) &&
!errors.Is(err, context.Canceled) &&
!errors.Is(err, net.ErrClosed) {
logger.Error(err)
}
}