component of goja
> go-spirit pull --config build-goja.conf
> go-spirit build --config build-goja.conf
build-goja.conf
working with post-api
# project
goja {
# import packages
packages = ["github.com/spirit-component/goja", "github.com/spirit-component/postapi"]
build-args = {
go-get = ["-v"]
go-build = ["-v"]
}
fetchers {
git {
gopath = ${GOPATH}
}
goget {
gopath = ${GOPATH}
}
}
# the dependencies
repos = {
goja {
fetcher = goget
args = ["-v"]
url = "github.com/spirit-component/goja"
revision = master
}
postapi {
fetcher = goget
args = ["-v"]
url = "github.com/spirit-component/postapi"
revision = master
}
}
}
> ./goja run --config goja.conf
goja.conf
# goja config
components.goja.api-mock {
dir = "scripts/"
timeout = 3s
}
# post-api graph config
# default graph driver
components.post-api.external.grapher.default = {
todo-task-new {
name = "todo.task.new"
graph = {
error {
response {
seq = 1
url = "spirit://actors/fbp/post-api/external?action=callback"
}
}
entrypoint {
to-todo {
seq = 1
url = "spirit://actors/fbp/goja/api-mock?action=todo.task.new"
}
response {
seq = 2
url = "spirit://actors/fbp/post-api/external?action=callback"
}
}
}
}
}
# if you just want mapping api to js, you can use templer grapher
# the template file is json format, and it will render by text/template to replace some vars.
{
"error": {
"ports": [{
"seq": 1,
"url": "spirit://actors/fbp/post-api/external?action=callback"
}]
},
"entrypoint": {
"ports": [{
"seq": 1,
"url": "spirit://actors/fbp/goja/api-mock?action={{.api}}"
},{
"seq": 2,
"url": "spirit://actors/fbp/post-api/external?action=callback"
}]
}
}
before using
templer
grapher, you should append"github.com/spirit-component/postapi/grapher/templer"
to config ofpackages
list inbuild-goja.conf
and rebuild goja component
scripts/todo.task.new.js
go.Import("uuid")
go.Import("fmt")
go.Import("time", "encoding/base64")
log.Infoln("hello I am the logger by logrus")
id = uuid.New()
obj = fbp.Object()
cache.Set(id, {id: id, name: obj.name})
fbp.SetBody({id: id})
scripts/todo.task.get.js
obj = fbp.Object()
if (obj) {
ret = cache.Get(obj.id)
if (ret[1]) {
fbp.SetBody(ret[0])
} else {
fbp.SetError("SPIRIT-GOJA",404,"task not exist")
}
} else {
fbp.SetError("SPIRIT-GOJA",400,"bad request")
}
name | type |
---|---|
session | github.com/go-spirit/spirit/mail.Session |
cache | github.com/go-spirit/spirit/cache.Cache |
config | github.com/gogap/config.Configuration |
go | github.com/spirit-component/goja/modules.GoLib |
fbp | github.com/spirit-component/goja.fbp |
name | import path | repositry | default imported |
---|---|---|---|
md5 | crypto/md5 |
false | |
base64 | encoding/base64 |
false | |
json | encoding/json |
false | |
fmt | fmt |
false | |
uuid | uuid |
github.com/pborman/uuid |
false |
ioutil | io/ioutil |
false | |
os | os |
false | |
exec | os/exec |
false | |
time | time |
false | |
redis | redis |
github.com/go-redis/redis |
false |
log | log |
github.com/sirupsen/logrus |
true |
utils | utils |
internal |
true |
go.Import("uuid")
go.Import("fmt")
go.Import("time", "encoding/base64")
log.Infoln("hello I am the logger by logrus")
id = uuid.New()
fbp.SetBody({id: id})
fmt.Println(base64.StdEncoding.EncodeToString("hello"))
my another project is: gojs-tool, use this command to generate the module bridge
- generate moudle
# Default GOPATH
gojs-tool gen --template goja -r -n github.com/go-redis/redis
# GO Internal LIBs
gojs-tool gen --gopath $(go env GOROOT) --template goja -r -n encoding/json
- update generated code, add import
github.com/spirit-component/goja/modules
and addmodules.RegisterNativeModule
atfunc init()
package your_package_name
import (
// ....
"github.com/spirit-component/goja/modules"
)
import (
"github.com/dop251/goja"
"github.com/gogap/gojs-tool/gojs"
)
var (
// the package name should be better equals to the "import/path/at/js" 's last slash name
// here is js
module = gojs.NewGojaModule("you_package_name")
)
func init() {
// .........
modules.RegisterNativeModule("import/path/at/js", Enable)
}
func Enable(runtime *goja.Runtime) {
module.Enable(runtime)
}
- update
build-goja.conf
goja {
# append the generated module path
packages = [..., "your_native_package_path"]
}