mirror of
https://github.com/syumai/workers.git
synced 2025-03-11 01:39:11 +00:00
Merge pull request #91 from syumai/init-go-instance-on-request
init Go instance on request
This commit is contained in:
commit
151971832c
@ -1,8 +1,6 @@
|
|||||||
import "./wasm_exec.js";
|
import "./wasm_exec.js";
|
||||||
import { connect } from 'cloudflare:sockets';
|
import { connect } from 'cloudflare:sockets';
|
||||||
|
|
||||||
const go = new Go();
|
|
||||||
|
|
||||||
let mod;
|
let mod;
|
||||||
|
|
||||||
globalThis.tryCatch = (fn) => {
|
globalThis.tryCatch = (fn) => {
|
||||||
@ -22,6 +20,8 @@ export function init(m) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function run(ctx) {
|
async function run(ctx) {
|
||||||
|
const go = new Go();
|
||||||
|
|
||||||
let ready;
|
let ready;
|
||||||
const readyPromise = new Promise((resolve) => {
|
const readyPromise = new Promise((resolve) => {
|
||||||
ready = resolve;
|
ready = resolve;
|
||||||
|
20
handler.go
20
handler.go
@ -12,7 +12,10 @@ import (
|
|||||||
"github.com/syumai/workers/internal/runtimecontext"
|
"github.com/syumai/workers/internal/runtimecontext"
|
||||||
)
|
)
|
||||||
|
|
||||||
var httpHandler http.Handler
|
var (
|
||||||
|
httpHandler http.Handler
|
||||||
|
closeCh = make(chan struct{})
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var handleRequestCallback js.Func
|
var handleRequestCallback js.Func
|
||||||
@ -40,6 +43,15 @@ func init() {
|
|||||||
jsutil.Binding.Set("handleRequest", handleRequestCallback)
|
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.
|
// handleRequest accepts a Request object and returns Response object.
|
||||||
func handleRequest(reqObj js.Value, runtimeCtxObj js.Value) (js.Value, error) {
|
func handleRequest(reqObj js.Value, runtimeCtxObj js.Value) (js.Value, error) {
|
||||||
if httpHandler == nil {
|
if httpHandler == nil {
|
||||||
@ -55,7 +67,7 @@ func handleRequest(reqObj js.Value, runtimeCtxObj js.Value) (js.Value, error) {
|
|||||||
w := &jshttp.ResponseWriter{
|
w := &jshttp.ResponseWriter{
|
||||||
HeaderValue: http.Header{},
|
HeaderValue: http.Header{},
|
||||||
StatusCode: http.StatusOK,
|
StatusCode: http.StatusOK,
|
||||||
Reader: reader,
|
Reader: &appCloser{reader},
|
||||||
Writer: writer,
|
Writer: writer,
|
||||||
ReadyCh: make(chan struct{}),
|
ReadyCh: make(chan struct{}),
|
||||||
}
|
}
|
||||||
@ -79,5 +91,7 @@ func Serve(handler http.Handler) {
|
|||||||
}
|
}
|
||||||
httpHandler = handler
|
httpHandler = handler
|
||||||
ready()
|
ready()
|
||||||
select {}
|
select {
|
||||||
|
case <-closeCh:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
type ResponseWriter struct {
|
type ResponseWriter struct {
|
||||||
HeaderValue http.Header
|
HeaderValue http.Header
|
||||||
StatusCode int
|
StatusCode int
|
||||||
Reader *io.PipeReader
|
Reader io.ReadCloser
|
||||||
Writer *io.PipeWriter
|
Writer *io.PipeWriter
|
||||||
ReadyCh chan struct{}
|
ReadyCh chan struct{}
|
||||||
Once sync.Once
|
Once sync.Once
|
||||||
|
Loading…
x
Reference in New Issue
Block a user