main.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "designs/bootstrap"
  4. "designs/global"
  5. "designs/utils"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. "net/http"
  9. "path/filepath"
  10. )
  11. func init() {
  12. fmt.Printf("初始化")
  13. }
  14. func main() {
  15. //创建一个服务
  16. gin.SetMode(gin.ReleaseMode)
  17. ginServer := gin.Default()
  18. // 初始化配置
  19. bootstrap.InitializeConfig(filepath.Join(global.App.PwdPath, ".env"))
  20. //config.PrintConfig()
  21. // 初始化日志
  22. global.App.Log, global.App.LogWriter = bootstrap.InitializeLog()
  23. //跨域
  24. ginServer.Use(Cors())
  25. // 初始化Redis
  26. global.Init.InitRedisFunc = bootstrap.InitializeRedis
  27. global.App.Redis = bootstrap.InitializeRedis()
  28. // 初始化facade
  29. global.InitFacade()
  30. //服务器端口
  31. //ginServer.Run(":" + config.Get("app.port")) /*默认是8080*/
  32. // 初始化数据库
  33. global.App.DB = &utils.WtDB{DB: bootstrap.InitializeDB()}
  34. // 程序关闭前,释放数据库连接
  35. defer func() {
  36. if global.App.DB != nil {
  37. db, _ := global.App.DB.DB.DB()
  38. db.Close()
  39. }
  40. }()
  41. bootstrap.RunServer()
  42. // ginServer.RunTLS(":443", "your_certificate.crt", "your_private_key.key")
  43. }
  44. /* 跨域 */
  45. func Cors() gin.HandlerFunc {
  46. return func(context *gin.Context) {
  47. method := context.Request.Method
  48. context.Header("Access-Control-Allow-Origin", "*")
  49. context.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token, x-token")
  50. context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PATCH, PUT")
  51. context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
  52. context.Header("Access-Control-Allow-Credentials", "true")
  53. if method == "OPTIONS" {
  54. context.AbortWithStatus(http.StatusNoContent)
  55. }
  56. }
  57. }