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.

104 lines
2.4 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
  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. "log"
  8. "mime"
  9. "path/filepath"
  10. )
  11. func main() {
  12. var addr string
  13. var static string
  14. var useFile string
  15. var mode string
  16. var path string
  17. flag.StringVar(&path, "config", "govue.json", "配置文件路径")
  18. flag.StringVar(&static, "static", "", "静态文件目录")
  19. flag.StringVar(&useFile, "use_file", "", "use文件路径")
  20. flag.StringVar(&addr, "addr", "", "监听ip:port")
  21. flag.StringVar(&mode, "mode", "", "模式release/debug")
  22. flag.Parse()
  23. if flag.Arg(0) == "init" {
  24. err := govue.GenerateConfig(path)
  25. if err != nil {
  26. fmt.Print("文件:", path, "\n生成失败:", err)
  27. } else {
  28. abs, _ := filepath.Abs(path)
  29. fmt.Print("文件:", abs, "\n生成成功:")
  30. }
  31. return
  32. }
  33. config, err := govue.GetConfig(path)
  34. fmt.Println("配置载入:", path)
  35. fmt.Println("渲染协程池最大总数:", config.Pool.MaxTotal)
  36. fmt.Println("渲染协程池最大空闲数:", config.Pool.MaxIdle)
  37. fmt.Println("渲染协程池最小空闲数:", config.Pool.MinIdle)
  38. if addr != "" {
  39. config.Addr = addr
  40. }
  41. if static != "" {
  42. config.StaticDir = static
  43. }
  44. if useFile != "" {
  45. config.UseJsFile = useFile
  46. }
  47. if mode != "" {
  48. config.Mode = mode
  49. }
  50. gin.SetMode(config.Mode)
  51. r := gin.Default()
  52. govue.SetPoolConfig(config.Pool)
  53. gv, err := govue.NewGoVue(config.UseJsFile, config.StaticDir)
  54. if err != nil {
  55. panic(err)
  56. }
  57. r.NoRoute(func(context *gin.Context) {
  58. raw, err := gv.LoadStaticResources(context.Request)
  59. if err != nil {
  60. _, _ = context.Writer.Write([]byte(fmt.Sprintln("服务器错误", err.Error())))
  61. return
  62. }
  63. mime.TypeByExtension(filepath.Ext(context.Request.URL.Path))
  64. context.Writer.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(context.Request.URL.Path)))
  65. context.Writer.WriteHeader(200)
  66. _, _ = context.Writer.Write(raw)
  67. })
  68. r.Use(func(context *gin.Context) {
  69. context.Header("Access-Control-Allow-Origin", "*")
  70. context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
  71. context.Header("Access-Control-Allow-Headers", "Action, Module, X-PINGOTHER, Content-Type, Content-Disposition")
  72. })
  73. r.GET("/version", func(context *gin.Context) {
  74. context.JSON(200, map[string]interface{}{
  75. "code": 0,
  76. "data": "0.9.1",
  77. })
  78. })
  79. gv.StartPoolLog()
  80. err = r.Run(config.Addr)
  81. if err != nil {
  82. log.Fatalln("服务意外停止:", err)
  83. }
  84. }