# 5.4 Web服务器
# 5.4.1 基础介绍
在due (opens new window)框架中,Web服务器是基于fiber (opens new window)框架的二次封装。在继承fiber (opens new window)框架的所有优点的同时,还提供了符合due (opens new window)框架的调用接口,为游戏开发中的Web场景提供完善的解决方案。
# 5.4.2 跨域支持
框架原生支持跨域,你只需要在启动配置里设置跨域配置即可开启跨域。
# 5.4.3 Swagger支持
为了为广大开发者提供较好的开发体验,Web服务器已原生支持了Swagger的组件。使用方法也非常简单,只需要通过配置项开启即可打开swagger访问支持。
$ go install github.com/swaggo/swag/cmd/swag@latest
1
- 生成swagger文件
$ swag init --parseDependency
1
- 删除无用的go文件
$ rm -rf ./docs/docs.go
1
# 5.4.4 全局中间件
你可以在创建Web服务器的时候使用http.WithMiddlewares() (opens new window)来设置全局中间件。中间件支持http.Handler (opens new window)和fiber.Handler (opens new window)两种类型的中间件
# 5.4.5 示例代码
以下完整示例详见:web-example (opens new window)
- 创建项目
$ mkdir web-example
1
- 安装依赖
$ cd web-example
$ go mod init web-example
$ go get github.com/dobyte/due/v2@v2.4.2
$ go get github.com/dobyte/due/component/http/v2@e5cd009
1
2
3
4
2
3
4
- 启动配置
文件位置:web-example/etc/etc.toml (opens new window)
# 进程号
pid = "./run/due.pid"
# 开发模式。支持模式:debug、test、release(设置优先级:配置文件 < 环境变量 < 运行参数 < mode.SetMode())
mode = "debug"
# 统一时区设置。项目中的时间获取请使用xtime.Now()
timezone = "Local"
# 容器关闭最大等待时间。支持单位:纳秒(ns)、微秒(us | µs)、毫秒(ms)、秒(s)、分(m)、小时(h)、天(d)。默认为0
shutdownMaxWaitTime = "0s"
[http]
# 服务器名称
name = "http"
# 服务器监听地址,默认为:8080
addr = ":8080"
# 是否启用控制台输出,默认为false
console = false
# body大小,支持单位: B | K | KB | M | MB | G | GB | T | TB | P | PB | E | EB | Z | ZB,默认为4 * 1024 * 1024
bodyLimit = "4M"
# 最大并发连接数,默认为256 * 1024
concurrency = 262144
# 是否启用严格路由模式,默认为false,启用后"/foo"与"/foo/"为两个不同的路由
strictRouting = false
# 是否区分路由大小写,默认为false, 启用后"/FoO"与"/foo"为两个不同的路由
caseSensitive = false
# 秘钥文件
keyFile = ""
# 证书文件
certFile = ""
# 跨域配置
[http.cors]
# 是否启用跨域
enable = false
# 允许跨域的请求源。默认为[],即为允许所有请求源
allowOrigins = []
# 允许跨域的请求方法。默认为["GET", "POST", "HEAD", "PUT", "DELETE", "PATCH"]
allowMethods = []
# 允许跨域的请求头部。默认为[],即为允许所有请求头部
allowHeaders = []
# 当允许所有源时,根据CORS规范不允许携带凭据。默认为false
allowCredentials = false
# 允许暴露给客户端的头部。默认为[],即为允许暴露所有头部
exposeHeaders = []
# 浏览器缓存预检请求结果的时间。默认为0
maxAge = 0
# 是否允许来自私有网络的请求。设置为true时,响应头Access-Control-Allow-Private-Network会被设置为true。默认为false
allowPrivateNetwork = false
# swagger文档配置
[http.swagger]
# 是否启用文档
enable = true
# API文档标题
title = "API文档"
# URL访问基础路径
basePath = "/swagger"
# swagger文件路径
filePath = "./docs/swagger.json"
# swagger-ui-bundle.js地址。当系统默认的cdn失效时可替换为自己的cdn地址,默认为空,使用系统默认https://unpkg.com/swagger-ui@5.28.1/dist/swagger-ui.js
swaggerBundleUrl = ""
# swagger-ui-standalone-preset.js地址。当系统默认的cdn失效时可替换为自己的cdn地址,默认为空,使用系统默认https://unpkg.com/swagger-ui@5.28.1/dist/swagger-ui-standalone-preset.js
swaggerPresetUrl = ""
# swagger-ui.css地址。当系统默认的cdn失效时可替换为自己的cdn地址,默认为空,使用系统默认https://unpkg.com/swagger-ui@5.28.1/dist/swagger-ui.css
swaggerStylesUrl = ""
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
60
61
62
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
60
61
62
- 编写示例
文件位置:web-example/main.go (opens new window)
package main
import (
"fmt"
"github.com/dobyte/due/component/http/v2"
"github.com/dobyte/due/v2"
"github.com/dobyte/due/v2/codes"
"github.com/dobyte/due/v2/log"
"github.com/dobyte/due/v2/utils/xtime"
)
// @title API文档
// @version 1.0
// @host localhost:8080
// @BasePath /
func main() {
// 创建容器
container := due.NewContainer()
// 创建HTTP组件
component := http.NewServer()
// 初始化应用
initApp(component.Proxy())
// 添加网格组件
container.Add(component)
// 启动容器
container.Serve()
}
// 初始化应用
func initApp(proxy *http.Proxy) {
// 路由器
router := proxy.Router()
// 注册路由
router.Get("/greet", greetHandler)
}
// 请求
type greetReq struct {
Message string `json:"message"`
}
// 响应
type greetRes struct {
Message string `json:"message"`
}
// 路由处理器
// @Summary 测试接口
// @Tags 测试
// @Schemes
// @Accept json
// @Produce json
// @Param request body greetReq true "请求参数"
// @Response 200 {object} http.Resp{Data=greetRes} "响应参数"
// @Router /greet [get]
func greetHandler(ctx http.Context) error {
req := &greetReq{}
if err := ctx.Bind().JSON(req); err != nil {
return ctx.Failure(codes.InvalidArgument)
}
log.Info(req.Message)
return ctx.Success(&greetRes{
Message: fmt.Sprintf("I'm server, and the current time is: %s", xtime.Now().Format(xtime.DateTime)),
})
}
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
60
61
62
63
64
65
66
67
68
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
60
61
62
63
64
65
66
67
68
- 生成文档
- 生成swagger文件
$ cd web-example
$ swag init --parseDependency # 生成swagger文件
$ rm -rf ./docs/docs.go # 删除无用的docs.go文件
1
2
3
2
3
- 启动服务
$ cd web-example
$ go run main.go
____ __ ________
/ __ \/ / / / ____/
/ / / / / / / __/
/ /_/ / /_/ / /___
/_____/\____/_____/
┌──────────────────────────────────────────────────────┐
| [Website] https://github.com/dobyte/due |
| [Version] v2.4.2 |
└──────────────────────────────────────────────────────┘
┌────────────────────────Global────────────────────────┐
| PID: 3456 |
| Mode: debug |
| Time: 2025-10-30 18:32:09.2954402 +0800 CST |
└──────────────────────────────────────────────────────┘
┌─────────────────────────Http─────────────────────────┐
| Name: http |
| Url: http://192.168.2.202:8080 |
| Swagger: http://192.168.2.202:8080/swagger |
| Registry: - |
| Transporter: - |
└──────────────────────────────────────────────────────┘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 5.4.6 更多文档
due (opens new window) 框架的Web服务器严格遵循fiber (opens new window)框架的开发规范,如需了解更多的开发问题请移步fiber开发文档 (opens new window)