-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (47 loc) · 1.35 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
package main
import (
"blogServer/middleware"
"blogServer/router"
"flag"
"github.com/gin-gonic/gin"
"log/slog"
"net/http"
"time"
)
func main() {
time.Local = time.FixedZone("CST", 8*3600)
var protocol string
var isReleaseMode bool
flag.StringVar(&protocol, "protocol", "http", "目标协议,默认为http")
flag.BoolVar(&isReleaseMode, "releaseMode", false, "release模式,默认为false")
flag.Parse()
slog.Info("log", "目标协议为:", protocol)
slog.Info("log", "gin运行为release模式:", isReleaseMode)
var runError error
slog.Info("log", "blogServer is running on port 9000.")
if isReleaseMode {
gin.SetMode(gin.ReleaseMode)
}
ginServer := gin.Default()
ginServer.StaticFile("/robots.txt", "./robots.txt")
ginServer.Static("/uploads", "./uploads")
ginServer.Use(middleware.Cors())
ginServer.Use(middleware.AccessLog())
ginServer.Use(middleware.Timeout(10000))
ginServer.Use(middleware.Interceptor())
ginServer.Use(middleware.IPBan())
ginServer.Use(router.Default())
if protocol == "https" {
server := &http.Server{
Addr: ":9000",
Handler: ginServer,
}
runError = server.ListenAndServeTLS("nano71.com_bundle.crt", "nano71.com.key")
} else {
//_ = ginServer.SetTrustedProxies([]string{"127.0.0.1"})
runError = ginServer.Run(":9000")
}
if runError != nil {
slog.Error("Failed to start HTTPS server", runError)
}
}