3136352472
5 years ago
10 changed files with 10300 additions and 2 deletions
-
0domino/README.md
-
10073domino/vue.js
-
7govue/govue.js
-
72jsruntime/runtime.go
-
100main.go
-
16static/index.html
-
16static/index2.html
-
1static/main.js
-
13static/use.js
10073
domino/vue.js
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,7 @@ |
|||
window = domino.createWindow(HTML_SRC, '127.0.0.1'); |
|||
document = window.document; |
|||
|
|||
GoVueUseCall(Vue); |
|||
|
|||
HTML_OUT(document.innerHTML); |
|||
|
@ -0,0 +1,72 @@ |
|||
package jsruntime |
|||
|
|||
import ( |
|||
"github.com/dop251/goja" |
|||
"github.com/dop251/goja_nodejs/console" |
|||
"github.com/dop251/goja_nodejs/eventloop" |
|||
"github.com/dop251/goja_nodejs/require" |
|||
"io/ioutil" |
|||
"time" |
|||
) |
|||
|
|||
type JsRuntime struct { |
|||
MainSrc string |
|||
UseSrc string |
|||
Relys Relys |
|||
runtime *goja.Runtime |
|||
registry *require.Registry |
|||
requireModule *require.RequireModule |
|||
} |
|||
|
|||
type Rely struct { |
|||
Variable string |
|||
FileName string |
|||
} |
|||
type Relys []Rely |
|||
|
|||
func NewJsRuntime(mainFileName string, useFileName string, rels Relys) (*JsRuntime, error) { |
|||
jr := JsRuntime{ |
|||
Relys: rels, |
|||
} |
|||
mainScript, err := ioutil.ReadFile(mainFileName) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
jr.MainSrc = string(mainScript) |
|||
jr.registry = new(require.Registry) // this can be shared by multiple runtimes
|
|||
loop := eventloop.NewEventLoop() |
|||
loop.Run(func(r *goja.Runtime) { |
|||
jr.runtime = r |
|||
}) |
|||
|
|||
jr.requireModule = jr.registry.Enable(jr.runtime) |
|||
console.Enable(jr.runtime) |
|||
|
|||
for e := range rels { |
|||
v, err := jr.requireModule.Require(rels[e].FileName) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
jr.runtime.Set(rels[e].Variable, v) |
|||
} |
|||
|
|||
go func() { |
|||
for { |
|||
useSrc, _ := ioutil.ReadFile(mainFileName) |
|||
jr.UseSrc = string(useSrc) |
|||
time.Sleep(time.Second * 2) |
|||
} |
|||
}() |
|||
return &jr, nil |
|||
} |
|||
|
|||
func (jr *JsRuntime) Render(tpl string) string { |
|||
jr.runtime.Set("HTML_SRC", tpl) |
|||
|
|||
runtime.Set("HTML_OUT", func(data string) { |
|||
context.Writer.WriteHeader(200) |
|||
context.Writer.Write([]byte(data)) |
|||
|
|||
return |
|||
}) |
|||
} |
@ -0,0 +1,100 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"git.ouxuan.net/3136352472/go-service-template/jsruntime" |
|||
"git.ouxuan.net/hasaki-service/hasaki-sdk/hskutils" |
|||
"github.com/gin-gonic/gin" |
|||
"io/ioutil" |
|||
"log" |
|||
"os" |
|||
"path/filepath" |
|||
) |
|||
|
|||
func main() { |
|||
|
|||
os.Chdir("domino") |
|||
|
|||
r := gin.Default() |
|||
|
|||
mainFile := filepath.Join(hskutils.GetSelfFilePath(), "govue", "govue.js") |
|||
useFileName := filepath.Join(hskutils.GetSelfFilePath(), "static", "use.js") |
|||
render, err := jsruntime.NewJsRuntime(mainFile, useFileName, jsruntime.Relys{ |
|||
jsruntime.Rely{ |
|||
Variable: "domino", |
|||
FileName: "./index.js", |
|||
}, |
|||
jsruntime.Rely{ |
|||
Variable: "Vue", |
|||
FileName: "./vue.js", |
|||
}, |
|||
}) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
|
|||
mainScript, err := ioutil.ReadFile(filepath.Join(hskutils.GetSelfFilePath(), "govue", "govue.js")) |
|||
if err != nil { |
|||
panic(err.Error()) |
|||
return |
|||
} |
|||
|
|||
r.NoRoute(func(context *gin.Context) { |
|||
path := filepath.Join(hskutils.GetSelfFilePath(), "static", context.Request.URL.Path) |
|||
fi, err := os.Stat(path) |
|||
if err != nil { |
|||
context.Writer.Write([]byte("页面不存在")) |
|||
return |
|||
} else if fi.IsDir() { |
|||
path = filepath.Join(path, "index.html") |
|||
} |
|||
|
|||
raw, err := ioutil.ReadFile(path) |
|||
if !hskutils.PathExists(path) || err != nil { |
|||
context.Writer.Write([]byte("页面不存在")) |
|||
return |
|||
} |
|||
if filepath.Ext(path) != ".html" { |
|||
//log.Println("ext", filepath.Ext(path))
|
|||
context.Writer.WriteHeader(200) |
|||
context.Writer.Write(raw) |
|||
return |
|||
} |
|||
|
|||
htmlSrc := string(raw) |
|||
|
|||
registry.Lock() |
|||
defer registry.Unlock() |
|||
|
|||
useScript, err := ioutil.ReadFile(filepath.Join(hskutils.GetSelfFilePath(), "static", "use.js")) |
|||
if err != nil { |
|||
panic(err.Error()) |
|||
return |
|||
} |
|||
|
|||
runtime.Set("GoVueUse", func(v interface{}) { |
|||
runtime.Set("GoVueUseCall", v) |
|||
}) |
|||
_, err = runtime.RunString(string(useScript)) |
|||
|
|||
runtime.Set("HTML_SRC", htmlSrc) |
|||
|
|||
runtime.Set("HTML_OUT", func(data string) { |
|||
context.Writer.WriteHeader(200) |
|||
context.Writer.Write([]byte(data)) |
|||
|
|||
return |
|||
}) |
|||
|
|||
_, err = runtime.RunString(string(mainScript)) |
|||
if err != nil { |
|||
log.Println(err) |
|||
} |
|||
|
|||
context.Writer.Write([]byte("")) |
|||
|
|||
return |
|||
}) |
|||
|
|||
r.Run("127.0.0.1:8080") |
|||
|
|||
} |
@ -0,0 +1,16 @@ |
|||
<!DOCTYPE html> |
|||
<html lang="en"> |
|||
<head> |
|||
<meta charset="UTF-8"> |
|||
<title>Title</title> |
|||
</head> |
|||
<body> |
|||
<div id="classifyer"> |
|||
code : {{code}} |
|||
<li v-for="todo in data"> |
|||
classifyer_id :{{ todo.classifyer_id }} |
|||
name :{{ todo.name }} |
|||
</li> |
|||
</div> |
|||
</body> |
|||
</html> |
@ -0,0 +1,16 @@ |
|||
<!DOCTYPE html> |
|||
<html lang="en"> |
|||
<head> |
|||
<meta charset="UTF-8"> |
|||
<title>Title</title> |
|||
</head> |
|||
<body> |
|||
<div id="classifyer"> |
|||
code : {{code}} |
|||
<li v-for="todo in data"> |
|||
classifyer_123123id :{{ todo.classifyer_id }} |
|||
name :{{ todo.name }} |
|||
</li> |
|||
</div> |
|||
</body> |
|||
</html> |
@ -0,0 +1 @@ |
|||
12312 |
@ -0,0 +1,13 @@ |
|||
GoVueUse(function (Vue) { |
|||
console.log("213"); |
|||
new Vue({ |
|||
el: "#classifyer", |
|||
data: { |
|||
code: 212123213123, |
|||
data: [ |
|||
{name: "1asdasdasd23"} |
|||
] |
|||
} |
|||
}); |
|||
// console.log(vue);
|
|||
}); |
Write
Preview
Loading…
Cancel
Save
Reference in new issue