package main import ( "flag" "fmt" "git.ouxuan.net/3136352472/go-service-template/govue" "github.com/gin-gonic/gin" "log" "mime" "path/filepath" ) func main() { var addr string var static string var useFile string var mode string var path string flag.StringVar(&path, "config", "govue.json", "配置文件路径") flag.StringVar(&static, "static", "", "静态文件目录") flag.StringVar(&useFile, "use_file", "", "use文件路径") flag.StringVar(&addr, "addr", "", "监听ip:port") flag.StringVar(&mode, "mode", "", "模式release/debug") flag.Parse() if flag.Arg(0) == "init" { err := govue.GenerateConfig(path) if err != nil { fmt.Print("文件:", path, "\n生成失败:", err) } else { abs, _ := filepath.Abs(path) fmt.Print("文件:", abs, "\n生成成功:") } return } config, err := govue.GetConfig(path) fmt.Println("配置载入:", path) fmt.Println("渲染协程池最大总数:", config.Pool.MaxTotal) fmt.Println("渲染协程池最大空闲数:", config.Pool.MaxIdle) fmt.Println("渲染协程池最小空闲数:", config.Pool.MinIdle) if addr != "" { config.Addr = addr } if static != "" { config.StaticDir = static } if useFile != "" { config.UseJsFile = useFile } if mode != "" { config.Mode = mode } gin.SetMode(config.Mode) r := gin.Default() govue.SetPoolConfig(config.Pool) gv, err := govue.NewGoVue(config.UseJsFile, config.StaticDir) if err != nil { panic(err) } r.NoRoute(func(context *gin.Context) { raw, err := gv.LoadStaticResources(context.Request) if err != nil { _, _ = context.Writer.Write([]byte(fmt.Sprintln("服务器错误", err.Error()))) return } mime.TypeByExtension(filepath.Ext(context.Request.URL.Path)) context.Writer.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(context.Request.URL.Path))) context.Writer.WriteHeader(200) _, _ = context.Writer.Write(raw) }) r.Use(func(context *gin.Context) { context.Header("Access-Control-Allow-Origin", "*") context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") context.Header("Access-Control-Allow-Headers", "Action, Module, X-PINGOTHER, Content-Type, Content-Disposition") }) r.GET("/version", func(context *gin.Context) { context.JSON(200, map[string]interface{}{ "code": 0, "data": "0.9.1", }) }) gv.StartPoolLog() err = r.Run(config.Addr) if err != nil { log.Fatalln("服务意外停止:", err) } }