-
Notifications
You must be signed in to change notification settings - Fork 0
/
wcgi.go
50 lines (43 loc) · 782 Bytes
/
wcgi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package wcgi
import (
"io"
"net/http"
"net/http/cgi"
"os"
"github.com/hashicorp/yamux"
)
//go:wasmexport wagi_wcgi
func wagi_wcgi() {}
func Serve(h http.Handler) error {
wcgi := os.Getenv("WAGI_WCGI") == "true"
if !wcgi {
return cgi.Serve(h)
}
stdio := &Stdio{
Reader: os.Stdin,
Writer: os.Stdout,
}
sess, err := yamux.Server(stdio, nil)
if err != nil {
return err
}
return http.Serve(sess, h)
}
type Stdio struct {
io.Reader
io.Writer
}
var _ io.ReadWriteCloser = (*Stdio)(nil)
func (stdio *Stdio) Close() error {
if reader, ok := stdio.Reader.(io.Closer); ok {
if err := reader.Close(); err != nil {
return err
}
}
if writer, ok := stdio.Writer.(io.Closer); ok {
if err := writer.Close(); err != nil {
return err
}
}
return nil
}