main.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package main
  2. import (
  3. "designs/bootstrap"
  4. "designs/global"
  5. "designs/middleware"
  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. ginServer.Use(middleware.Logmiddleware())
  24. //ip库
  25. //common.InitIpFile()
  26. bootstrap.InitIpFile()
  27. //跨域
  28. ginServer.Use(Cors())
  29. // 初始化Redis
  30. global.Init.InitRedisFunc = bootstrap.InitializeRedis
  31. global.App.Redis = bootstrap.InitializeRedis()
  32. bootstrap.InitGameCfg()
  33. // 初始化facade
  34. global.InitFacade()
  35. //服务器端口
  36. //ginServer.Run(":" + config.Get("app.port")) /*默认是8080*/
  37. bootstrap.RunServer()
  38. // ginServer.RunTLS(":443", "your_certificate.crt", "your_private_key.key")
  39. }
  40. /* 跨域 */
  41. func Cors() gin.HandlerFunc {
  42. return func(context *gin.Context) {
  43. method := context.Request.Method
  44. context.Header("Access-Control-Allow-Origin", "*")
  45. context.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token, x-token")
  46. context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PATCH, PUT")
  47. context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
  48. context.Header("Access-Control-Allow-Credentials", "true")
  49. if method == "OPTIONS" {
  50. context.AbortWithStatus(http.StatusNoContent)
  51. }
  52. }
  53. }