|
@ -23,6 +23,7 @@ type GoVue struct { |
|
|
Resources *assetfs.AssetFS |
|
|
Resources *assetfs.AssetFS |
|
|
IsDebugMode bool |
|
|
IsDebugMode bool |
|
|
jsRuntimePool *pool.JsRuntimePool |
|
|
jsRuntimePool *pool.JsRuntimePool |
|
|
|
|
|
CacheSec int64 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func NewGoVueDefaultConfig(debug bool) (gv *GoVue, err error) { |
|
|
func NewGoVueDefaultConfig(debug bool) (gv *GoVue, err error) { |
|
@ -46,6 +47,10 @@ func NewGoVue(useJsPath string, staticPath string, debug bool) (gv *GoVue, err e |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (gv *GoVue) SetCacheSec(cacheSec int64) { |
|
|
|
|
|
gv.CacheSec = cacheSec |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func (gv *GoVue) SetErrorPage404(page string) { |
|
|
func (gv *GoVue) SetErrorPage404(page string) { |
|
|
gv.ErrorPage404 = page |
|
|
gv.ErrorPage404 = page |
|
|
} |
|
|
} |
|
@ -146,6 +151,16 @@ func (gv *GoVue) StartPoolLog() { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (gv *GoVue) LoadStaticResources(request *http.Request, goExtDataS ...interface{}) (result []byte, err error) { |
|
|
func (gv *GoVue) LoadStaticResources(request *http.Request, goExtDataS ...interface{}) (result []byte, err error) { |
|
|
|
|
|
cacheKey := fmt.Sprintf("%s|%s", request.URL.Path, request.URL.RawQuery) |
|
|
|
|
|
if gv.CacheSec > 0 { |
|
|
|
|
|
html := GetCache(cacheKey) |
|
|
|
|
|
if html != "" { |
|
|
|
|
|
//log.Println("加载缓存")
|
|
|
|
|
|
return []byte(html), nil |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//log.Println("非缓存", cacheKey)
|
|
|
|
|
|
|
|
|
var staticDir string |
|
|
var staticDir string |
|
|
var filePath = request.URL.Path |
|
|
var filePath = request.URL.Path |
|
@ -184,9 +199,16 @@ func (gv *GoVue) LoadStaticResources(request *http.Request, goExtDataS ...interf |
|
|
|
|
|
|
|
|
result, err = ioutil.ReadFile(filePath) |
|
|
result, err = ioutil.ReadFile(filePath) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return gv.renderErrPage(404, request, goExtDataS...) |
|
|
|
|
|
|
|
|
result, err = gv.renderErrPage(404, request, goExtDataS...) |
|
|
|
|
|
if gv.CacheSec > 0 { |
|
|
|
|
|
SetCache(cacheKey, string(result), gv.CacheSec) |
|
|
|
|
|
} |
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
if filepath.Ext(filePath) != ".html" && filepath.Ext(filePath) != ".vue" { |
|
|
if filepath.Ext(filePath) != ".html" && filepath.Ext(filePath) != ".vue" { |
|
|
|
|
|
if gv.CacheSec > 0 { |
|
|
|
|
|
SetCache(cacheKey, string(result), gv.CacheSec) |
|
|
|
|
|
} |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -203,6 +225,9 @@ func (gv *GoVue) LoadStaticResources(request *http.Request, goExtDataS ...interf |
|
|
|
|
|
|
|
|
err = jr.Render(filePath, fmt.Sprintf("http://%s%s", request.Host, request.RequestURI), string(result), goExtData, func(data string) { |
|
|
err = jr.Render(filePath, fmt.Sprintf("http://%s%s", request.Host, request.RequestURI), string(result), goExtData, func(data string) { |
|
|
result = []byte(data) |
|
|
result = []byte(data) |
|
|
|
|
|
if gv.CacheSec > 0 { |
|
|
|
|
|
SetCache(cacheKey, string(result), gv.CacheSec) |
|
|
|
|
|
} |
|
|
}) |
|
|
}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
result = []byte(err.Error()) |
|
|
result = []byte(err.Error()) |
|
|