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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package govue
  2. //go-bindata-assetfs --pkg=govue govue-runtime/...
  3. import (
  4. "fmt"
  5. "git.ouxuan.net/3136352472/go-service-template/jsruntime"
  6. "git.ouxuan.net/3136352472/go-service-template/pool"
  7. "github.com/elazarl/go-bindata-assetfs"
  8. "io/ioutil"
  9. "log"
  10. "net/http"
  11. "os"
  12. "path/filepath"
  13. "time"
  14. )
  15. type GoVue struct {
  16. StaticPath string
  17. UseJsPath string
  18. Resources *assetfs.AssetFS
  19. jsRuntimePool *pool.JsRuntimePool
  20. }
  21. func NewGoVueDefaultConfig() (gv *GoVue, err error) {
  22. gv = &GoVue{
  23. Resources: assetFS(),
  24. }
  25. err = gv.initRender()
  26. return
  27. }
  28. func NewGoVue(useJsPath string, staticPath string) (gv *GoVue, err error) {
  29. gv = &GoVue{
  30. StaticPath: staticPath,
  31. UseJsPath: useJsPath,
  32. Resources: assetFS(),
  33. }
  34. err = gv.initRender()
  35. return
  36. }
  37. func SetPoolConfig(config pool.Config) {
  38. pool.DefaultConfig = config
  39. }
  40. func (gv *GoVue) initRender() (err error) {
  41. if gv.StaticPath == "" {
  42. gv.StaticPath = filepath.Join(getSelfFilePath(), "static")
  43. }
  44. if gv.UseJsPath == "" {
  45. gv.UseJsPath = filepath.Join(gv.StaticPath, "use.js")
  46. }
  47. //mainScript, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime", "runtime.js"))
  48. //if err != nil {
  49. // return
  50. //}
  51. //headerScript, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime", "header.js"))
  52. //if err != nil {
  53. // return
  54. //}
  55. //govueScript, err := ioutil.ReadFile(filepath.Join("govue-js-src", "dist", "index.js"))
  56. //if err != nil {
  57. // return
  58. //}
  59. mainScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", "runtime.js"))
  60. if err != nil {
  61. return
  62. }
  63. headerScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", "header.js"))
  64. if err != nil {
  65. return
  66. }
  67. govueScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", "govue.js"))
  68. if err != nil {
  69. return
  70. }
  71. gv.jsRuntimePool = pool.NewJsRuntimePool(string(mainScript), gv.UseJsPath, gv.StaticPath, jsruntime.Relys{
  72. jsruntime.Rely{
  73. Src: string(headerScript),
  74. },
  75. jsruntime.Rely{
  76. Src: string(govueScript),
  77. },
  78. }, jsruntime.ModeSync)
  79. return
  80. }
  81. func (gv *GoVue) PoolLog() {
  82. gv.jsRuntimePool.Log()
  83. }
  84. func (gv *GoVue) StartPoolLog() {
  85. go func() {
  86. defer func() {
  87. recover()
  88. log.Println("统计协程异常")
  89. }()
  90. lastidle := 0
  91. lastactive := 0
  92. for {
  93. all, idle, active := gv.jsRuntimePool.NumInfo()
  94. if idle != lastidle || active != lastactive {
  95. fmt.Println("渲染协程数量变动变动:所有:", all, "空闲:", idle, "活动:", active)
  96. }
  97. lastidle = idle
  98. lastactive = active
  99. time.Sleep(time.Second)
  100. }
  101. }()
  102. }
  103. func (gv *GoVue) LoadStaticResources(request *http.Request) (result []byte, err error) {
  104. var path string
  105. var staticDir string
  106. var filePath = request.URL.Path
  107. if gv.StaticPath == "" {
  108. staticDir = filepath.Join(getSelfFilePath(), "static")
  109. } else {
  110. staticDir = gv.StaticPath
  111. }
  112. path = filepath.Join(staticDir, filePath)
  113. fi, err := os.Stat(path)
  114. if err != nil {
  115. return
  116. } else if fi.IsDir() {
  117. filePath = filepath.Join(filePath, "index.html")
  118. path = filepath.Join(staticDir, filePath)
  119. }
  120. result, err = ioutil.ReadFile(path)
  121. if !pathExists(path) || err != nil {
  122. return
  123. }
  124. if filepath.Ext(path) != ".html" {
  125. return
  126. }
  127. err = gv.jsRuntimePool.JsRuntimeCall(func(jr *jsruntime.JsRuntime) {
  128. err = jr.Render(filePath, fmt.Sprintf("http://%s%s", request.Host, request.RequestURI), string(result), func(data string) {
  129. result = []byte(data)
  130. })
  131. })
  132. return
  133. }