From 6d313765fb53c065c0ca88dd6b6abedd87cd3f67 Mon Sep 17 00:00:00 2001 From: syumai Date: Sat, 18 May 2024 01:00:59 +0900 Subject: [PATCH] fix handler file names --- handler.go | 98 ++++++------------------------------------ handler_js.go | 99 +++++++++++++++++++++++++++++++++++++++++++ handler_without_js.go | 27 ------------ 3 files changed, 112 insertions(+), 112 deletions(-) create mode 100644 handler_js.go delete mode 100644 handler_without_js.go diff --git a/handler.go b/handler.go index ed026a3..edc3f5b 100644 --- a/handler.go +++ b/handler.go @@ -1,99 +1,27 @@ -//go:build js && wasm +//go:build !js package workers import ( - "context" "fmt" - "io" "net/http" - "syscall/js" - - "github.com/syumai/workers/internal/jshttp" - "github.com/syumai/workers/internal/jsutil" - "github.com/syumai/workers/internal/runtimecontext" -) - -var ( - httpHandler http.Handler - closeCh = make(chan struct{}) + "os" ) -func init() { - var handleRequestCallback js.Func - handleRequestCallback = js.FuncOf(func(this js.Value, args []js.Value) any { - reqObj := args[0] - var cb js.Func - cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any { - defer cb.Release() - resolve := pArgs[0] - reject := pArgs[1] - go func() { - if len(args) > 1 { - reject.Invoke(jsutil.Errorf("too many args given to handleRequest: %d", len(args))) - return - } - res, err := handleRequest(reqObj) - if err != nil { - reject.Invoke(jsutil.Error(err.Error())) - return - } - resolve.Invoke(res) - }() - return js.Undefined() - }) - return jsutil.NewPromise(cb) - }) - jsutil.Binding.Set("handleRequest", handleRequestCallback) -} - -type appCloser struct { - io.ReadCloser -} - -func (c *appCloser) Close() error { - defer close(closeCh) - return c.ReadCloser.Close() -} - -// handleRequest accepts a Request object and returns Response object. -func handleRequest(reqObj js.Value) (js.Value, error) { - if httpHandler == nil { - return js.Value{}, fmt.Errorf("Serve must be called before handleRequest.") - } - req, err := jshttp.ToRequest(reqObj) - if err != nil { - return js.Value{}, err - } - ctx := runtimecontext.New(context.Background(), reqObj) - req = req.WithContext(ctx) - reader, writer := io.Pipe() - w := &jshttp.ResponseWriter{ - HeaderValue: http.Header{}, - StatusCode: http.StatusOK, - Reader: &appCloser{reader}, - Writer: writer, - ReadyCh: make(chan struct{}), - } - go func() { - defer w.Ready() - defer writer.Close() - httpHandler.ServeHTTP(w, req) - }() - <-w.ReadyCh - return w.ToJSResponse(), nil -} - -//go:wasmimport workers ready -func ready() - -// Server serves http.Handler on a JS runtime. +// Server serves http.Handler as a normal HTTP server. // if the given handler is nil, http.DefaultServeMux will be used. +// As a port number, PORT environment variable or default value (9900) is used. +// This function is implemented for non-JS environments for debugging purposes. func Serve(handler http.Handler) { if handler == nil { handler = http.DefaultServeMux } - httpHandler = handler - ready() - <-closeCh + port := os.Getenv("PORT") + if port == "" { + port = "9900" + } + addr := fmt.Sprintf(":%s", port) + fmt.Printf("listening on: http://localhost%s\n", addr) + fmt.Fprintln(os.Stderr, "warn: this server is currently running in non-JS mode. to enable JS-related features, please use the make command in the syumai/workers template.") + http.ListenAndServe(addr, handler) } diff --git a/handler_js.go b/handler_js.go new file mode 100644 index 0000000..ed026a3 --- /dev/null +++ b/handler_js.go @@ -0,0 +1,99 @@ +//go:build js && wasm + +package workers + +import ( + "context" + "fmt" + "io" + "net/http" + "syscall/js" + + "github.com/syumai/workers/internal/jshttp" + "github.com/syumai/workers/internal/jsutil" + "github.com/syumai/workers/internal/runtimecontext" +) + +var ( + httpHandler http.Handler + closeCh = make(chan struct{}) +) + +func init() { + var handleRequestCallback js.Func + handleRequestCallback = js.FuncOf(func(this js.Value, args []js.Value) any { + reqObj := args[0] + var cb js.Func + cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any { + defer cb.Release() + resolve := pArgs[0] + reject := pArgs[1] + go func() { + if len(args) > 1 { + reject.Invoke(jsutil.Errorf("too many args given to handleRequest: %d", len(args))) + return + } + res, err := handleRequest(reqObj) + if err != nil { + reject.Invoke(jsutil.Error(err.Error())) + return + } + resolve.Invoke(res) + }() + return js.Undefined() + }) + return jsutil.NewPromise(cb) + }) + jsutil.Binding.Set("handleRequest", handleRequestCallback) +} + +type appCloser struct { + io.ReadCloser +} + +func (c *appCloser) Close() error { + defer close(closeCh) + return c.ReadCloser.Close() +} + +// handleRequest accepts a Request object and returns Response object. +func handleRequest(reqObj js.Value) (js.Value, error) { + if httpHandler == nil { + return js.Value{}, fmt.Errorf("Serve must be called before handleRequest.") + } + req, err := jshttp.ToRequest(reqObj) + if err != nil { + return js.Value{}, err + } + ctx := runtimecontext.New(context.Background(), reqObj) + req = req.WithContext(ctx) + reader, writer := io.Pipe() + w := &jshttp.ResponseWriter{ + HeaderValue: http.Header{}, + StatusCode: http.StatusOK, + Reader: &appCloser{reader}, + Writer: writer, + ReadyCh: make(chan struct{}), + } + go func() { + defer w.Ready() + defer writer.Close() + httpHandler.ServeHTTP(w, req) + }() + <-w.ReadyCh + return w.ToJSResponse(), nil +} + +//go:wasmimport workers ready +func ready() + +// Server serves http.Handler on a JS runtime. +// if the given handler is nil, http.DefaultServeMux will be used. +func Serve(handler http.Handler) { + if handler == nil { + handler = http.DefaultServeMux + } + httpHandler = handler + ready() + <-closeCh +} diff --git a/handler_without_js.go b/handler_without_js.go deleted file mode 100644 index edc3f5b..0000000 --- a/handler_without_js.go +++ /dev/null @@ -1,27 +0,0 @@ -//go:build !js - -package workers - -import ( - "fmt" - "net/http" - "os" -) - -// Server serves http.Handler as a normal HTTP server. -// if the given handler is nil, http.DefaultServeMux will be used. -// As a port number, PORT environment variable or default value (9900) is used. -// This function is implemented for non-JS environments for debugging purposes. -func Serve(handler http.Handler) { - if handler == nil { - handler = http.DefaultServeMux - } - port := os.Getenv("PORT") - if port == "" { - port = "9900" - } - addr := fmt.Sprintf(":%s", port) - fmt.Printf("listening on: http://localhost%s\n", addr) - fmt.Fprintln(os.Stderr, "warn: this server is currently running in non-JS mode. to enable JS-related features, please use the make command in the syumai/workers template.") - http.ListenAndServe(addr, handler) -}