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
158 lines
3.8 KiB
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"git.ouxuan.net/3136352472/go-service-template/govue"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
"log"
|
|
"mime"
|
|
"path/filepath"
|
|
)
|
|
|
|
func main() {
|
|
|
|
var addr string
|
|
var static string
|
|
var useFile string
|
|
var mode string
|
|
var path string
|
|
var errorPage404 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.StringVar(&errorPage404, "error_page_404", "", "404错误页面路径")
|
|
|
|
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, config.Mode == "debug")
|
|
|
|
if errorPage404 != "" {
|
|
config.ErrorPage404 = errorPage404
|
|
}
|
|
gv.SetErrorPage404(config.ErrorPage404)
|
|
gv.SetCacheSec(5)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
r.NoRoute(func(context *gin.Context) {
|
|
cookies := context.Request.Cookies()
|
|
|
|
extData := map[string]interface{}{
|
|
"cookie": map[string]string{},
|
|
}
|
|
|
|
for e := range cookies {
|
|
extData["cookie"].(map[string]string)[cookies[e].Name] = cookies[e].Value
|
|
}
|
|
|
|
if extData["cookie"].(map[string]string)["extdatasessionkey"] == "" {
|
|
context.SetCookie("extdatasessionkey", uuid.Must(uuid.NewUUID()).String(), 0, "", context.Request.Host, false, false)
|
|
}
|
|
|
|
raw, err := gv.LoadStaticResources(context.Request, extData)
|
|
if err != nil {
|
|
_, _ = context.Writer.Write(raw)
|
|
return
|
|
}
|
|
|
|
mime.TypeByExtension(filepath.Ext(context.Request.URL.Path))
|
|
ext := filepath.Ext(context.Request.URL.Path)
|
|
if ext == "" {
|
|
ext = ".html"
|
|
}
|
|
context.Writer.Header().Set("Content-Type", mime.TypeByExtension(ext)+"; charset=utf-8")
|
|
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")
|
|
})
|
|
|
|
//var GoExtDataMap map[string]map[string]interface{}
|
|
//r.GET("/GoExtData", func(context *gin.Context) {
|
|
// key, err := context.Cookie("extdatasessionkey")
|
|
// if err != nil {
|
|
// context.SetCookie("extdatasessionkey", uuid.Must(uuid.NewUUID()).String(), 0, "", context.Request.Host, http.SameSiteLaxMode, false, false)
|
|
// }
|
|
// var old = GoExtDataMap[key]
|
|
//
|
|
// var result map[string]interface{}
|
|
// data, _ := ioutil.ReadAll(context.Request.Body)
|
|
// _ = json.Unmarshal(data, &result)
|
|
//
|
|
// for e := range result {
|
|
// old[e] = result[e]
|
|
// }
|
|
// GoExtDataMap[key] = old
|
|
//
|
|
// context.JSON(200, map[string]interface{}{
|
|
// "code": 0,
|
|
// "data": GoExtDataMap[key],
|
|
// })
|
|
//})
|
|
|
|
r.GET("/version", func(context *gin.Context) {
|
|
context.JSON(200, map[string]interface{}{
|
|
"code": 0,
|
|
"data": "0.9.1",
|
|
})
|
|
})
|
|
|
|
gv.StartPoolLog()
|
|
|
|
for {
|
|
err = r.Run(config.Addr)
|
|
if err != nil {
|
|
log.Println("服务意外停止:", err)
|
|
}
|
|
}
|
|
|
|
}
|