main.go 3.2 KB

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