Skip to content

Commit

Permalink
export View, Redirect, add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Nov 6, 2023
1 parent e38c2a9 commit 8062669
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 9 deletions.
26 changes: 17 additions & 9 deletions primate/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
package-lock.json
*.wasm
7 changes: 7 additions & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"@primate/binding": "file:../../primate/packages/binding",
"rcompat": "^0.2.0"
},
"type": "module"
}
24 changes: 24 additions & 0 deletions test/route.go
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions test/run.js
Original file line number Diff line number Diff line change
@@ -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});
});
2 changes: 2 additions & 0 deletions test/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GOOS=js GOARCH=wasm go build -o route.wasm route.go
node run.js

0 comments on commit 8062669

Please sign in to comment.