-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
gateway.go
72 lines (60 loc) · 1.51 KB
/
gateway.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package web
import (
"fmt"
"io/fs"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strconv"
"syscall/js"
"tractor.dev/wanix/internal/jsutil"
"tractor.dev/wanix/kernel/web/gwutil"
)
type Gateway struct{}
func (s *Gateway) InitializeJS() {
// expose to host
js.Global().Get("sys").Call("handle", "web.request", map[string]any{
"respondRPC": js.FuncOf(s.request),
})
}
func (s *Gateway) request(this js.Value, args []js.Value) any {
var (
responder = args[0]
call = args[1]
)
return jsutil.Promise(func() (any, error) {
params := jsutil.Await(call.Call("receive"))
var (
method = params.Index(0).String()
fullURL = params.Index(1).String()
)
u, _ := url.Parse(fullURL)
req, err := http.NewRequest(method, u.Path, nil)
if err != nil {
fmt.Println("err:", err)
return nil, err
}
rr := httptest.NewRecorder()
//fmt.Println("SW", fullURL)
s.ServeHTTP(rr, req)
headers := make(map[string]any)
for k, v := range rr.Result().Header {
headers[k] = v[0]
}
headers["Wanix-Status-Code"] = strconv.Itoa(rr.Code)
headers["Wanix-Status-Text"] = rr.Result().Status
ch := jsutil.Await(responder.Call("continue", headers))
chw := &jsutil.Writer{ch}
if _, err := rr.Body.WriteTo(chw); err != nil {
fmt.Println("gateway:", err)
}
ch.Call("close")
return nil, nil
})
}
func (s *Gateway) ServeHTTP(w http.ResponseWriter, r *http.Request) {
gwutil.FileTransformer(os.DirFS("."), func(f fs.FS) http.Handler {
return http.FileServer(http.FS(f))
}).ServeHTTP(w, r)
}