You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "git.ouxuan.net/3136352472/go-service-template/govue"
  6. "github.com/gin-gonic/gin"
  7. "github.com/google/uuid"
  8. "log"
  9. "mime"
  10. "path/filepath"
  11. )
  12. func main() {
  13. var addr string
  14. var static string
  15. var useFile string
  16. var mode string
  17. var path string
  18. var errorPage404 string
  19. flag.StringVar(&path, "config", "govue.json", "配置文件路径")
  20. flag.StringVar(&static, "static", "", "静态文件目录")
  21. flag.StringVar(&useFile, "use_file", "", "use文件路径")
  22. flag.StringVar(&addr, "addr", "", "监听ip:port")
  23. flag.StringVar(&mode, "mode", "", "模式release/debug")
  24. flag.StringVar(&errorPage404, "error_page_404", "", "404错误页面路径")
  25. flag.Parse()
  26. if flag.Arg(0) == "init" {
  27. err := govue.GenerateConfig(path)
  28. if err != nil {
  29. fmt.Print("文件:", path, "\n生成失败:", err)
  30. } else {
  31. abs, _ := filepath.Abs(path)
  32. fmt.Print("文件:", abs, "\n生成成功:")
  33. }
  34. return
  35. }
  36. config, err := govue.GetConfig(path)
  37. fmt.Println("配置载入:", path)
  38. fmt.Println("渲染协程池最大总数:", config.Pool.MaxTotal)
  39. fmt.Println("渲染协程池最大空闲数:", config.Pool.MaxIdle)
  40. fmt.Println("渲染协程池最小空闲数:", config.Pool.MinIdle)
  41. if addr != "" {
  42. config.Addr = addr
  43. }
  44. if static != "" {
  45. config.StaticDir = static
  46. }
  47. if useFile != "" {
  48. config.UseJsFile = useFile
  49. }
  50. if mode != "" {
  51. config.Mode = mode
  52. }
  53. gin.SetMode(config.Mode)
  54. r := gin.Default()
  55. govue.SetPoolConfig(config.Pool)
  56. gv, err := govue.NewGoVue(config.UseJsFile, config.StaticDir, config.Mode == "debug")
  57. if errorPage404 != "" {
  58. config.ErrorPage404 = errorPage404
  59. }
  60. gv.SetErrorPage404(config.ErrorPage404)
  61. gv.SetCacheSec(5)
  62. if err != nil {
  63. panic(err)
  64. }
  65. r.NoRoute(func(context *gin.Context) {
  66. cookies := context.Request.Cookies()
  67. extData := map[string]interface{}{
  68. "cookie": map[string]string{},
  69. }
  70. for e := range cookies {
  71. extData["cookie"].(map[string]string)[cookies[e].Name] = cookies[e].Value
  72. }
  73. if extData["cookie"].(map[string]string)["extdatasessionkey"] == "" {
  74. context.SetCookie("extdatasessionkey", uuid.Must(uuid.NewUUID()).String(), 0, "", context.Request.Host, false, false)
  75. }
  76. raw, err := gv.LoadStaticResources(context.Request, extData)
  77. if err != nil {
  78. _, _ = context.Writer.Write(raw)
  79. return
  80. }
  81. mime.TypeByExtension(filepath.Ext(context.Request.URL.Path))
  82. ext := filepath.Ext(context.Request.URL.Path)
  83. if ext == "" {
  84. ext = ".html"
  85. }
  86. context.Writer.Header().Set("Content-Type", mime.TypeByExtension(ext)+"; charset=utf-8")
  87. context.Writer.WriteHeader(200)
  88. _, _ = context.Writer.Write(raw)
  89. })
  90. r.Use(func(context *gin.Context) {
  91. context.Header("Access-Control-Allow-Origin", "*")
  92. context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
  93. context.Header("Access-Control-Allow-Headers", "Action, Module, X-PINGOTHER, Content-Type, Content-Disposition")
  94. })
  95. //var GoExtDataMap map[string]map[string]interface{}
  96. //r.GET("/GoExtData", func(context *gin.Context) {
  97. // key, err := context.Cookie("extdatasessionkey")
  98. // if err != nil {
  99. // context.SetCookie("extdatasessionkey", uuid.Must(uuid.NewUUID()).String(), 0, "", context.Request.Host, http.SameSiteLaxMode, false, false)
  100. // }
  101. // var old = GoExtDataMap[key]
  102. //
  103. // var result map[string]interface{}
  104. // data, _ := ioutil.ReadAll(context.Request.Body)
  105. // _ = json.Unmarshal(data, &result)
  106. //
  107. // for e := range result {
  108. // old[e] = result[e]
  109. // }
  110. // GoExtDataMap[key] = old
  111. //
  112. // context.JSON(200, map[string]interface{}{
  113. // "code": 0,
  114. // "data": GoExtDataMap[key],
  115. // })
  116. //})
  117. r.GET("/version", func(context *gin.Context) {
  118. context.JSON(200, map[string]interface{}{
  119. "code": 0,
  120. "data": "0.9.1",
  121. })
  122. })
  123. gv.StartPoolLog()
  124. for {
  125. err = r.Run(config.Addr)
  126. if err != nil {
  127. log.Println("服务意外停止:", err)
  128. }
  129. }
  130. }