package govue import ( "fmt" "git.ouxuan.net/3136352472/go-service-template/jsruntime" "git.ouxuan.net/3136352472/go-service-template/pool" "git.ouxuan.net/hasaki-service/hasaki-sdk/hskutils" "io/ioutil" "net/http" "os" "path/filepath" ) type GoVue struct { StaticPath string UseJsPath string jsRuntimePool *pool.JsRuntimePool } func NewGoVueDefaultConfig() (gv *GoVue, err error) { gv = &GoVue{} err = gv.initRender() return } func NewGoVue(staticPath, useJsPath string) (gv *GoVue, err error) { gv = &GoVue{ StaticPath: staticPath, UseJsPath: useJsPath, } err = gv.initRender() return } func (gv *GoVue) initRender() (err error) { mainFile := filepath.Join(hskutils.GetSelfFilePath(), "govue-runtime", "runtime.js") var useFileName string if gv.UseJsPath == "" { useFileName = filepath.Join(hskutils.GetSelfFilePath(), "static", "use.js") } else { useFileName = gv.UseJsPath } gv.jsRuntimePool = pool.NewJsRuntimePool(mainFile, useFileName, jsruntime.Relys{ jsruntime.Rely{ FileName: filepath.Join("govue-runtime", "header.js"), }, jsruntime.Rely{ FileName: filepath.Join("govue-runtime", "request.js"), }, //jsruntime.Rely{ // FileName: filepath.Join("govue-runtime", "govue.js"), //}, jsruntime.Rely{ FileName: filepath.Join("govue-js-src", "dist", "index.js"), }, }, jsruntime.ModeSync) return } func (gv *GoVue) LoadStaticResources(request *http.Request) (result []byte, err error) { var path string var staticDir string var filePath = request.URL.Path if gv.StaticPath == "" { staticDir = filepath.Join(hskutils.GetSelfFilePath(), "static") } else { staticDir = gv.StaticPath } path = filepath.Join(staticDir, filePath) fi, err := os.Stat(path) if err != nil { return } else if fi.IsDir() { filePath = filepath.Join(filePath, "index.html") path = filepath.Join(staticDir, filePath) } result, err = ioutil.ReadFile(path) if !hskutils.PathExists(path) || err != nil { return } if filepath.Ext(path) != ".html" { return } err = gv.jsRuntimePool.JsRuntimeCall(func(jr *jsruntime.JsRuntime) { err = jr.Render(filePath, fmt.Sprintf("http://%s%s", request.Host, request.RequestURI), string(result), func(data string) { result = []byte(data) }) }) gv.jsRuntimePool.Log() return }