main.go 2.8 KB

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