-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (45 loc) · 1.04 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
package main
import (
"NanoKVM-Server/backend"
"NanoKVM-Server/backend/middleware"
"NanoKVM-Server/backend/utils"
"fmt"
"github.com/gin-gonic/gin"
cors "github.com/rs/cors/wrapper/gin"
log "github.com/sirupsen/logrus"
)
func main() {
gin.SetMode(gin.ReleaseMode)
conf := utils.GetConfig()
utils.InitLog()
r := gin.New()
r.Use(gin.Recovery())
r.Use(cors.AllowAll())
backend.InitRouter(r)
if conf.Protocol == "https" {
runTls(r)
} else {
run(r)
}
}
func run(r *gin.Engine) {
conf := utils.GetConfig()
address := fmt.Sprintf(":%d", conf.Port.Http)
log.Debugf("server started at %s", address)
err := r.Run(address)
if err != nil {
panic("start server failed")
}
}
func runTls(r *gin.Engine) {
conf := utils.GetConfig()
httpAddr := fmt.Sprintf(":%d", conf.Port.Http)
httpsAddr := fmt.Sprintf(":%d", conf.Port.Https)
log.Debugf("server started at %s, %s", httpsAddr, httpAddr)
r.Use(middleware.Tls())
go run(r)
err := r.RunTLS(httpsAddr, conf.Cert.Crt, conf.Cert.Key)
if err != nil {
panic("start server failed")
}
}