mirror of
https://github.com/syumai/workers.git
synced 2025-03-11 01:39:11 +00:00
Merge pull request #114 from syumai/add-non-js-handler
add normal HTTP server listener for debugging purposes
This commit is contained in:
commit
9ca5cf3840
@ -1,3 +1,5 @@
|
|||||||
|
//go:build js && wasm
|
||||||
|
|
||||||
package workers
|
package workers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -85,7 +87,7 @@ func handleRequest(reqObj js.Value) (js.Value, error) {
|
|||||||
//go:wasmimport workers ready
|
//go:wasmimport workers ready
|
||||||
func 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.
|
// if the given handler is nil, http.DefaultServeMux will be used.
|
||||||
func Serve(handler http.Handler) {
|
func Serve(handler http.Handler) {
|
||||||
if handler == nil {
|
if handler == nil {
|
||||||
|
27
handler_without_js.go
Normal file
27
handler_without_js.go
Normal file
@ -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…
x
Reference in New Issue
Block a user