main.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package main
  2. import (
  3. _ "eta_mini_ht_api/common/component"
  4. logger "eta_mini_ht_api/common/component/log"
  5. "eta_mini_ht_api/common/exception"
  6. "eta_mini_ht_api/middleware"
  7. _ "eta_mini_ht_api/routers"
  8. _ "eta_mini_ht_api/task"
  9. "github.com/beego/beego/v2/server/web"
  10. "github.com/beego/beego/v2/server/web/filter/cors"
  11. "time"
  12. )
  13. func main() {
  14. if web.BConfig.RunMode == "dev" {
  15. web.BConfig.WebConfig.DirectoryIndex = true
  16. web.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
  17. }
  18. web.InsertFilter("*", web.BeforeRouter, cors.Allow(&cors.Options{
  19. AllowAllOrigins: true, // 允许所有来源的请求
  20. AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
  21. AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
  22. ExposeHeaders: []string{"Content-Length"},
  23. AllowCredentials: true,
  24. MaxAge: 12 * time.Hour,
  25. }))
  26. //增加授权拦截
  27. web.InsertFilter("*", web.BeforeRouter, middleware.AuthMiddleware())
  28. //web.ErrorHandler("*", exception.ControllerAdvice())
  29. web.BConfig.RecoverFunc = exception.PanicAdvice
  30. go func() {
  31. //内存数据预热预加载
  32. logger.Info("开始预加载数据")
  33. }()
  34. logger.Info("初始化成功")
  35. web.Run()
  36. }