服务器计时
本项目演示了如何在使用 Fiber 框架的 Go 应用中实现 Server-Timing 头部。
前提条件
请确保您已安装以下内容
- Golang
- Fiber 包
设置
-
克隆仓库
git clone https://github.com/gofiber/recipes.git
cd recipes/server-timing -
安装依赖
go get
运行应用
- 启动应用
go run main.go
示例
这里是在 Fiber 应用中设置 Server-Timing 头部的一个示例
package main
import (
"github.com/gofiber/fiber/v2"
"time"
)
func main() {
app := fiber.New()
app.Use(func(c *fiber.Ctx) error {
start := time.Now()
err := c.Next()
duration := time.Since(start)
c.Append("Server-Timing", "app;dur="+duration.String())
return err
})
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Listen(":3000")
}