This commit is contained in:
Nicolas Lepage
2019-11-27 11:20:52 +01:00
parent 5a9f7c57a2
commit bffb45066d
7 changed files with 99 additions and 13 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"os"
"strings"
"syscall/js"
"github.com/nlepage/go-wasm-http-server/internal/whutil"
@@ -16,6 +17,22 @@ func Serve(handler http.Handler) func() {
h = http.DefaultServeMux
}
path := os.Getenv("WASMHTTP_PATH")
if !strings.HasSuffix(path, "/") {
path = path + "/"
}
if path != "" {
prefix := os.Getenv("WASMHTTP_PATH")
for strings.HasSuffix(prefix, "/") {
prefix = strings.TrimSuffix(prefix, "/")
}
mux := http.NewServeMux()
mux.Handle(path, http.StripPrefix(prefix, h))
h = mux
}
cb := js.FuncOf(func(_ js.Value, args []js.Value) interface{} {
jsReq := whutil.Request(args[0])
@@ -29,14 +46,14 @@ func Serve(handler http.Handler) func() {
r := recover()
if r != nil {
if err, ok := r.(error); ok {
fmt.Fprintf("wasmhttp: panic: %+v", err)
fmt.Printf("wasmhttp: panic: %+v\n", err)
} else {
fmt.Fprintf("wasmhttp: panic: %v", r)
fmt.Printf("wasmhttp: panic: %v\n", r)
}
res := whutil.NewResponseWriter()
res.WriteHeader(500)
resolveRes(res)
resolveRes(res.JSResponse())
}
}()
@@ -47,7 +64,7 @@ func Serve(handler http.Handler) func() {
res := whutil.NewResponseWriter()
handler.ServeHTTP(res, req)
h.ServeHTTP(res, req)
resolveRes(res.JSResponse())
}()