setup.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package handlers
  2. import (
  3. "Wine-Server/utils"
  4. "Wine-Server/utils/tables"
  5. "fmt"
  6. "github.com/gin-contrib/cors"
  7. "github.com/gin-gonic/gin"
  8. "os"
  9. )
  10. type routeFn func(*gin.Engine)
  11. type createTableFn func() error
  12. type App struct {
  13. router *gin.Engine
  14. config *utils.Config
  15. }
  16. func lostHandler(ctx *gin.Context) {
  17. ctx.JSON(utils.HttpNotFound, utils.Fail("api/resource not found"))
  18. }
  19. func loggerFormat(timeFmt string) gin.HandlerFunc {
  20. return gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
  21. return fmt.Sprintf("[wine] %s - %s [%s %s %s] %s, %s %d %s: %s\n",
  22. param.ClientIP,
  23. param.TimeStamp.Format(timeFmt),
  24. param.MethodColor(), param.Method, param.ResetColor(),
  25. param.Path,
  26. param.StatusCodeColor(), param.StatusCode, param.ResetColor(),
  27. param.ErrorMessage,
  28. )
  29. })
  30. }
  31. func CreateApp(config *utils.Config) *App {
  32. if config.WxMerchantAcc == "" {
  33. fmt.Println("wx-pay-api config is null")
  34. os.Exit(-1)
  35. }
  36. utils.WxMerchantAcc, utils.WxApiV3Key = config.WxMerchantAcc, config.WxApiV3Key
  37. utils.WxApiCertSeq, utils.WxApiCertPath = config.WxApiCertSeq, config.WxApiCertPath
  38. if config.Release {
  39. gin.SetMode(gin.ReleaseMode)
  40. } else {
  41. gin.SetMode(gin.DebugMode)
  42. }
  43. router := gin.New()
  44. err := router.SetTrustedProxies([]string{"127.0.0.1"})
  45. if err != nil {
  46. fmt.Printf("can't trust '127.0.0.1', for: %s", err)
  47. os.Exit(-1)
  48. }
  49. if err = utils.GenRsaKey(); err != nil {
  50. fmt.Println("can't generate private and public key.")
  51. os.Exit(-1)
  52. }
  53. conf := cors.DefaultConfig()
  54. allow := append(conf.AllowHeaders, "Token", "Device")
  55. conf.AllowAllOrigins, conf.AllowHeaders = true, allow
  56. router.Use(loggerFormat(config.TimeFormat), gin.Recovery(), utils.ErrorHandler, cors.New(conf))
  57. router.NoRoute(lostHandler)
  58. router.Static("/static", "./static")
  59. utils.SqlInit(config)
  60. toCreate := []createTableFn{
  61. tables.CreateAdvertiseTable, tables.CreateDeviceTable, tables.CreateManagerTable,
  62. tables.CreateParamsTable, tables.CreateVersionTable, tables.CreateWineTable,
  63. }
  64. for _, Fn := range toCreate {
  65. err = Fn()
  66. if err != nil {
  67. fmt.Println("create mysql tables failed.")
  68. os.Exit(-1)
  69. }
  70. }
  71. return &App{router: router, config: config}
  72. }
  73. func (app *App) RouteRegister(routeFns ...routeFn) {
  74. for _, fn := range routeFns {
  75. fn(app.router)
  76. }
  77. }
  78. func (app *App) Start() {
  79. err := app.router.Run(app.config.ServerAddr)
  80. if err != nil {
  81. fmt.Printf("server can't run on address[%s] for: %s", app.config.ServerAddr, err)
  82. }
  83. }