-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.go
43 lines (35 loc) · 846 Bytes
/
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
package main
import (
"fmt"
"github.com/unrolled/secure"
"github.com/gin-gonic/gin"
"tsingsee.com/adminserver/app"
"tsingsee.com/adminserver/routes"
)
func main() {
r := gin.Default()
https := gin.Default()
app := app.NewApp()
if app.Config().HttpsPort > 0 {
httpsPort := fmt.Sprintf(":%d", app.Config().HttpsPort)
https.Use(TlsHandler(httpsPort))
routes.Setup(https, app)
go https.RunTLS(httpsPort, app.Config().CertPath, app.Config().KeyPath)
}
routes.Setup(r, app)
r.Run(fmt.Sprintf(":%d", app.Config().Port))
}
// 初始 TLS
func TlsHandler(httpsPort string) gin.HandlerFunc {
return func(c *gin.Context) {
secureMiddleware := secure.New(secure.Options{
SSLRedirect: true,
SSLHost: httpsPort,
})
err := secureMiddleware.Process(c.Writer, c.Request)
if err != nil {
return
}
c.Next()
}
}