main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "gitee.com/zr233/bsf"
  6. "github.com/gin-gonic/gin"
  7. "github.com/sirupsen/logrus"
  8. ginSwagger "github.com/swaggo/gin-swagger"
  9. "github.com/swaggo/gin-swagger/swaggerFiles"
  10. "os"
  11. "video_course/controller"
  12. "video_course/docs"
  13. "video_course/global"
  14. "video_course/logger"
  15. "video_course/middleware"
  16. "video_course/repository/postgres"
  17. "video_course/repository/redis"
  18. )
  19. var _VERSION_ = "unknown"
  20. func flagInit() {
  21. flag.BoolVar(&global.Project.Debug, "d", global.Project.Debug, "是否debug")
  22. flag.StringVar(&global.Project.Host, "s", global.Project.Host, "swag host")
  23. flag.BoolVar(&help, "h", false, "this help")
  24. flag.Parse()
  25. if help {
  26. flag.Usage()
  27. os.Exit(0)
  28. }
  29. }
  30. // @title Video Course框架
  31. // @version 1.0
  32. // @description Video Course框架 API 文档
  33. // @termsOfService http://swagger.io/terms/
  34. // @contact.name API Support
  35. // @contact.url http://www.swagger.io/support
  36. // @contact.email support@swagger.io
  37. // @license.name Apache 2.0
  38. // @license.url http://www.apache.org/licenses/LICENSE-2.0.html
  39. // @BasePath /v1
  40. // @securityDefinitions.basic BasicAuth
  41. // @securityDefinitions.apikey ApiKeyAuth
  42. // @in header
  43. // @name Authorization
  44. // @securitydefinitions.oauth2.application OAuth2Application
  45. // @tokenUrl https://example.com/oauth/token
  46. // @scope.write Grants write access
  47. // @scope.admin Grants read and write access to administrative information
  48. // @securitydefinitions.oauth2.implicit OAuth2Implicit
  49. // @authorizationurl https://example.com/oauth/authorize
  50. // @scope.write Grants write access
  51. // @scope.admin Grants read and write access to administrative information
  52. // @securitydefinitions.oauth2.password OAuth2Password
  53. // @tokenUrl https://example.com/oauth/token
  54. // @scope.read Grants read access
  55. // @scope.write Grants write access
  56. // @scope.admin Grants read and write access to administrative information
  57. // @securitydefinitions.oauth2.accessCode OAuth2AccessCode
  58. // @tokenUrl https://example.com/oauth/token
  59. // @authorizationurl https://example.com/oauth/authorize
  60. // @scope.admin Grants read and write access to administrative information
  61. func main() {
  62. logger.Init()
  63. global.Init()
  64. logger.SetOnlineWriters()
  65. flagInit()
  66. postgres.Init()
  67. redis.Init()
  68. r := bsf.NewDefaultEngine()
  69. gin.SetMode(gin.ReleaseMode)
  70. r.Use(
  71. middleware.ErrorResponse(),
  72. middleware.Logger(),
  73. middleware.Session(),
  74. )
  75. r.AutoRegisterController("/v1", &controller.Auth{})
  76. r.AutoRegisterController("/v1", &controller.User{})
  77. //debug模式下启用swag文档
  78. if global.Project.Debug {
  79. gin.SetMode(gin.DebugMode)
  80. //设置swagger文档主机
  81. docs.SwaggerInfo.Host = fmt.Sprintf("%s:%d", global.Project.Host, global.Port)
  82. // 注册swagger文档
  83. // 查看地址为 /v1/docs/index.html /v1/SignIn/docs/doc.json
  84. r.GET("/v1/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
  85. }
  86. logrus.Infof("Version:[%s]\n", _VERSION_)
  87. _ = r.Run(fmt.Sprintf(":%d", global.Port))
  88. }
  89. var (
  90. help bool
  91. )