35 lines
667 B
Go
35 lines
667 B
Go
package middleware
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
|
)
|
|
|
|
// CORS 跨域中间件
|
|
func CORS() fiber.Handler {
|
|
return cors.New(cors.Config{
|
|
AllowOrigins: "*",
|
|
AllowMethods: "GET,POST,PUT,DELETE,OPTIONS",
|
|
AllowHeaders: "Origin,Content-Type,Accept,timestamp,sign",
|
|
})
|
|
}
|
|
|
|
// RequestLogger 请求日志中间件
|
|
func RequestLogger() fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
start := time.Now()
|
|
err := c.Next()
|
|
duration := time.Since(start)
|
|
log.Printf("[%s] %s %s - %d (%v)",
|
|
c.Method(),
|
|
c.Path(),
|
|
c.IP(),
|
|
c.Response().StatusCode(),
|
|
duration,
|
|
)
|
|
return err
|
|
}
|
|
} |