mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
fix handler file names
This commit is contained in:
parent
9ca5cf3840
commit
6d313765fb
98
handler.go
98
handler.go
@ -1,99 +1,27 @@
|
|||||||
//go:build js && wasm
|
//go:build !js
|
||||||
|
|
||||||
package workers
|
package workers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"syscall/js"
|
"os"
|
||||||
|
|
||||||
"github.com/syumai/workers/internal/jshttp"
|
|
||||||
"github.com/syumai/workers/internal/jsutil"
|
|
||||||
"github.com/syumai/workers/internal/runtimecontext"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
// Server serves http.Handler as a normal HTTP server.
|
||||||
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.
|
// 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) {
|
func Serve(handler http.Handler) {
|
||||||
if handler == nil {
|
if handler == nil {
|
||||||
handler = http.DefaultServeMux
|
handler = http.DefaultServeMux
|
||||||
}
|
}
|
||||||
httpHandler = handler
|
port := os.Getenv("PORT")
|
||||||
ready()
|
if port == "" {
|
||||||
<-closeCh
|
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)
|
||||||
}
|
}
|
||||||
|
99
handler_js.go
Normal file
99
handler_js.go
Normal file
@ -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
|
||||||
|
}
|
@ -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)
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user