middleware.go 636 B

12345678910111213141516171819202122232425262728
  1. package middleware
  2. import (
  3. "log"
  4. "time"
  5. "github.com/gin-contrib/cors"
  6. "github.com/gin-gonic/gin"
  7. )
  8. func Logger() gin.HandlerFunc {
  9. return func(c *gin.Context) {
  10. start := time.Now()
  11. //继续调用后续的中间件处理函数
  12. c.Next()
  13. log.Printf("[GIN] %s %s %d %s",
  14. c.Request.Method, c.Request.URL.Path, c.Writer.Status(), time.Since(start))
  15. }
  16. }
  17. func CORS() gin.HandlerFunc {
  18. return cors.New(cors.Config{
  19. AllowOrigins: []string{"*"},
  20. AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
  21. AllowHeaders: []string{"Origin", "Content-Type", "Accept"},
  22. AllowCredentials: false,
  23. })
  24. }