Skip to content

Commit

Permalink
fix handler file names
Browse files Browse the repository at this point in the history
  • Loading branch information
syumai committed May 17, 2024
1 parent 9ca5cf3 commit 6d31376
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 112 deletions.
98 changes: 13 additions & 85 deletions handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
99 changes: 99 additions & 0 deletions handler_js.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 0 additions & 27 deletions handler_without_js.go

This file was deleted.

0 comments on commit 6d31376

Please sign in to comment.