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.
 
 

154 lines
3.3 KiB

package govue
//go-bindata-assetfs --pkg=govue govue-runtime/...
import (
"fmt"
"git.ouxuan.net/3136352472/go-service-template/jsruntime"
"git.ouxuan.net/3136352472/go-service-template/pool"
"github.com/elazarl/go-bindata-assetfs"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"time"
)
type GoVue struct {
StaticPath string
UseJsPath string
Resources *assetfs.AssetFS
jsRuntimePool *pool.JsRuntimePool
}
func NewGoVueDefaultConfig() (gv *GoVue, err error) {
gv = &GoVue{
Resources: assetFS(),
}
err = gv.initRender()
return
}
func NewGoVue(useJsPath string, staticPath string) (gv *GoVue, err error) {
gv = &GoVue{
StaticPath: staticPath,
UseJsPath: useJsPath,
Resources: assetFS(),
}
err = gv.initRender()
return
}
func SetPoolConfig(config pool.Config) {
pool.DefaultConfig = config
}
func (gv *GoVue) initRender() (err error) {
if gv.StaticPath == "" {
gv.StaticPath = filepath.Join(getSelfFilePath(), "static")
}
if gv.UseJsPath == "" {
gv.UseJsPath = filepath.Join(gv.StaticPath, "use.js")
}
//mainScript, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime", "runtime.js"))
//if err != nil {
// return
//}
//headerScript, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime", "header.js"))
//if err != nil {
// return
//}
//govueScript, err := ioutil.ReadFile(filepath.Join("govue-js-src", "dist", "index.js"))
//if err != nil {
// return
//}
mainScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", "runtime.js"))
if err != nil {
return
}
headerScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", "header.js"))
if err != nil {
return
}
govueScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", "govue.js"))
if err != nil {
return
}
gv.jsRuntimePool = pool.NewJsRuntimePool(string(mainScript), gv.UseJsPath, gv.StaticPath, jsruntime.Relys{
jsruntime.Rely{
Src: string(headerScript),
},
jsruntime.Rely{
Src: string(govueScript),
},
}, jsruntime.ModeSync)
return
}
func (gv *GoVue) PoolLog() {
gv.jsRuntimePool.Log()
}
func (gv *GoVue) StartPoolLog() {
go func() {
defer func() {
recover()
log.Println("统计协程异常")
}()
lastidle := 0
lastactive := 0
for {
all, idle, active := gv.jsRuntimePool.NumInfo()
if idle != lastidle || active != lastactive {
fmt.Println("渲染协程数量变动变动:所有:", all, "空闲:", idle, "活动:", active)
}
lastidle = idle
lastactive = active
time.Sleep(time.Second)
}
}()
}
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(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 !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)
})
})
return
}