Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add normal HTTP server listener for debugging purposes #114

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build js && wasm

package workers

import (
Expand Down Expand Up @@ -85,7 +87,7 @@ func handleRequest(reqObj js.Value) (js.Value, error) {
//go:wasmimport workers ready
func ready()

// Server serves http.Handler on Cloudflare Workers.
// 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 {
Expand Down
27 changes: 27 additions & 0 deletions handler_without_js.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//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