手把手,带你从零封装Gin框架(五):静态资源处理 & 优雅

项目源码

地址: github.com/jassue/jass…

前言

这一篇将对路由进行分组调整,把定义路由的文件集中到同一个目录下,并处理前端项目打包后的静态文件。在 Go 1.8 及以上版本中,内置的 http.Server 提供了 Shutdown() 方法,支持平滑重启服务器,本次将使用它调整项目启动代码,若 Go 版本低于 1.8 可以使用 fvbock/endless 来替代

路由分组调整

新建 routes/api.go 文件,用来存放 api 分组路由

1
2
3
4
5
6
7
8
9
10
11
12
13
go复制代码package routes

import (
"github.com/gin-gonic/gin"
"net/http"
)

// SetApiGroupRoutes 定义 api 分组路由
func SetApiGroupRoutes(router *gin.RouterGroup) {
router.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
}

新建 bootstrap/router.go 文件,编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
go复制代码package bootstrap

import (
"github.com/gin-gonic/gin"
"jassue-gin/global"
"jassue-gin/routes"
)

func setupRouter() *gin.Engine {
router := gin.Default()

// 注册 api 分组路由
apiGroup := router.Group("/api")
routes.SetApiGroupRoutes(apiGroup)

return router
}

// RunServer 启动服务器
func RunServer() {
r := setupRouter()
r.Run(":" + global.App.Config.App.Port)
}

若之后还有其它的分组路由,可以先在 routes 目录下新建一个文件,编写定义路由的方法,然后再到 bootstrap/router.go 调用注册

main.go 文件中调用 RunServer() 方法

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
go复制代码package main

import (
"jassue-gin/bootstrap"
"jassue-gin/global"
)

func main() {
// 初始化配置
bootstrap.InitializeConfig()

// 初始化日志
global.App.Log = bootstrap.InitializeLog()
global.App.Log.Info("log init success!")

// 初始化数据库
global.App.DB = bootstrap.InitializeDB()
// 程序关闭前,释放数据库连接
defer func() {
if global.App.DB != nil {
db, _ := global.App.DB.DB()
db.Close()
}
}()

// 启动服务器
bootstrap.RunServer()
}

静态资源处理

bootstrap/router.go 文件,setupRouter() 方法中编写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
go复制代码func setupRouter() *gin.Engine {
router := gin.Default()

// 前端项目静态资源
router.StaticFile("/", "./static/dist/index.html")
router.Static("/assets", "./static/dist/assets")
router.StaticFile("/favicon.ico", "./static/dist/favicon.ico")
// 其他静态资源
router.Static("/public", "./static")
router.Static("/storage", "./storage/app/public")

// 注册 api 分组路由
apiGroup := router.Group("/api")
routes.SetApiGroupRoutes(apiGroup)

return router
}

使用 docker 快速打包一份前端项目文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
shell复制代码# 创建 node环境 容器
docker run -idt --name vue-app jassue/node
# 进入容器
docker exec -it vue-app bash
# 初始化 vue 模板
npm init @vitejs/app . --template vue-ts
# 安装项目依赖
npm install
# 打包
npm run build
# 退出容器
exit
# 拷贝前端文件到 go 项目静态资源文件夹
docker cp vue-app:/app/dist ~/go/src/jassue-gin/static

启动 main.go ,访问 http://localhost:8888/ ,前端资源处理成功

image-20211015195022062.png

优雅重启/停止服务器

bootstrap/router.go 文件中,调整 RunServer() 方法

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
go复制代码package bootstrap

import (
"context"
"github.com/gin-gonic/gin"
"jassue-gin/global"
"jassue-gin/routes"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)

//...

func RunServer() {
r := setupRouter()

srv := &http.Server{
Addr: ":" + global.App.Config.App.Port,
Handler: r,
}

go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()

// 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间)
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutdown Server ...")

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
log.Println("Server exiting")
}

routes/api.go 中,添加一条测试路由:

1
2
3
4
go复制代码router.GET("/test", func(c *gin.Context) {
time.Sleep(5*time.Second)
c.String(http.StatusOK, "success")
})

启动 main.go,访问 http://localhost:8888/api/test ,使用 CTRL + C 停止服务器,如下图所示:

image-20211015200006388.png

服务器接收到中止命令后,依旧等待 /api/test 接口完成响应后才停止服务器

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%