-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (46 loc) · 1.07 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
package main
import (
"fmt"
"go-service-api/config"
"go-service-api/global"
"go-service-api/initialize"
"go-service-api/middleware"
"go-service-api/router"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/gin-gonic/gin"
)
func main() {
initialize.Config()
initialize.Mysql()
initialize.Log()
r := gin.New()
r.Use(middleware.Logger(global.Logger, time.RFC3339, true), middleware.Recovery(global.Logger, true))
r.Use(middleware.Cors())
router.Router(r)
addr := fmt.Sprintf("%s:%d", config.Server.Host, config.Server.Port)
server := &http.Server{
Addr: addr,
Handler: r,
}
// 等待中断信号以优雅地关闭服务器
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
go func() {
<-quit
go func() {
global.Logger.Sync()
}()
if err := server.Close(); err != nil {
log.Fatal("服务关闭出现错误:", err)
}
}()
log.Println("正在启动服务器:", addr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Println("服务启动错误:", err)
}
log.Println("服务器已退出")
}