diff --git a/primate/binding.go b/primate/binding.go index 8b22cb9..ba30bc5 100644 --- a/primate/binding.go +++ b/primate/binding.go @@ -25,8 +25,6 @@ type URL struct { type Request struct { Url URL - View t_view - Redirect t_redirect } func make_url(request js.Value) URL { @@ -58,21 +56,31 @@ func make_view(request js.Value) t_view { } } -func make_redirect(request js.Value) t_redirect { - redirect := request.Get("redirect") +func Redirect(location string, options map[string]interface{}) interface{} { + return js.FuncOf(func(this js.Value, args[] js.Value) interface{} { + return map[string]interface{}{ + "handler": "redirect", + "options": options, + } + }); +} - return func(location string, options map[string]interface{}) interface{} { - return redirect.Invoke(location, options); - } +func View(component string, props map[string]interface{}) interface{} { + return js.FuncOf(func(this js.Value, args[] js.Value) interface{} { + return map[string]interface{}{ + "handler": "view", + "component": component, + "props": props, + } + }); } + func MakeRequest(route t_request) t_response { return func(this js.Value, args[] js.Value) interface{} { request := args[0]; go_request := Request{ make_url(request), - make_view(request), - make_redirect(request), } return route(go_request) diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..9103f97 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,3 @@ +node_modules +package-lock.json +*.wasm diff --git a/test/go.mod b/test/go.mod new file mode 100644 index 0000000..dcb1aa8 --- /dev/null +++ b/test/go.mod @@ -0,0 +1,7 @@ +module route + +go 1.21.3 + +replace github.com/primatejs/go/primate => ../primate + +require github.com/primatejs/go/primate v0.0.0-20231105213429-e38c2a977313 // indirect diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..53da697 --- /dev/null +++ b/test/package.json @@ -0,0 +1,7 @@ +{ + "dependencies": { + "@primate/binding": "file:../../primate/packages/binding", + "rcompat": "^0.2.0" + }, + "type": "module" +} diff --git a/test/route.go b/test/route.go new file mode 100644 index 0000000..5d4c00b --- /dev/null +++ b/test/route.go @@ -0,0 +1,24 @@ +/// {{{ start of primate wrapper, prefix +package main +import "syscall/js" +// }}} end + +import "github.com/primatejs/go/primate"; + +func Get(request primate.Request) interface{} { + //return request.View("1test", map[string]interface{}{ + // "test": 1234, + //}); + //return map[string]interface{}{ + // "test": 1234, + //}; + return primate.View("test", map[string]interface{}{}); +} + +// {{{ start primate wrapper, postfix +func main() { + c := make(chan bool) + js.Global().Set("Get", js.FuncOf(primate.MakeRequest(Get))) + <-c +} +// }}} end diff --git a/test/run.js b/test/run.js new file mode 100644 index 0000000..6c3939d --- /dev/null +++ b/test/run.js @@ -0,0 +1,24 @@ +import { Path } from "rcompat/fs"; +import { serve } from "rcompat/http"; +import { make_request, make_response, env } from "@primate/binding/go"; + +env(); + +const cwd = await new Path(import.meta.url); +const { directory } = cwd; +const file = await directory.join("route.wasm").arrayBuffer(); +const typedArray = new Uint8Array(file); + +const go = new globalThis.Go(); +await WebAssembly.instantiate(typedArray, { + ...go.importObject, + env: {}}).then(async result => { + go.run(result.instance); + serve(request => { + const get = globalThis.Get; + const response = make_response(get(make_request({ url: new URL(request.url) }))); + console.log(response); + //const $response = typeof response === "object" ? JSON.stringify(response) : response; + return new Response("11"); + }, {host: "0.0.0.0", port: 6161}); +}); diff --git a/test/run.sh b/test/run.sh new file mode 100755 index 0000000..f280613 --- /dev/null +++ b/test/run.sh @@ -0,0 +1,2 @@ +GOOS=js GOARCH=wasm go build -o route.wasm route.go +node run.js