-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (86 loc) · 2.11 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Wercsrv is a simple go http server to use for serving werc-driven sites.
To use, run in a werc root or provide a "root" argument. The address
to serve HTTP on is set by and argument to the "addr" flag.
To set up werc, see the werc homepage: http://werc.cat-v.org
NOTES
Any port is stripped from the "Host" header passed to werc.
The PLAN9 environment variable is passed through to child processes,
but bin/werc.rc may need to be patched if plan9port is not in
/usr/local/plan9.
*/
package main
import (
"context"
"flag"
"fmt"
"net"
"net/http"
"net/http/cgi"
"os"
"os/signal"
"path/filepath"
"strings"
)
func main() {
var (
rootArg = flag.String("root", ".", "Root for werc tree.")
addr = flag.String("addr", ":8080", "Address to serve HTTP on.")
)
flag.Parse()
root, err := filepath.Abs(*rootArg)
if err != nil {
fmt.Fprintln(os.Stderr, "!", err)
return
}
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt, os.Kill)
srv := &http.Server{
Addr: *addr,
Handler: werc(root),
}
go func() {
fmt.Fprintf(os.Stderr, "# using werc at %s\n", root)
fmt.Fprintf(os.Stderr, "# listening on %s\n", *addr)
if err := srv.ListenAndServe(); err != nil {
fmt.Fprintln(os.Stderr, "!", err)
}
}()
<-sig
if err := srv.Shutdown(context.Background()); err != nil {
fmt.Fprintln(os.Stderr, "!", err)
}
fmt.Fprintln(os.Stderr, "# exiting")
}
type wercHandler struct {
root string
werc *cgi.Handler
}
func (h *wercHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "_werc/config") {
http.Error(w, "", http.StatusUnauthorized)
return
}
if h, _, err := net.SplitHostPort(r.Host); err == nil {
r.Host = h
}
fn := filepath.Join(h.root, "sites", r.Host, r.URL.Path)
if fi, err := os.Stat(fn); err == nil && !fi.IsDir() {
http.ServeFile(w, r, fn)
return
}
h.werc.ServeHTTP(w, r)
}
func werc(root string) http.Handler {
return &wercHandler{
root: root,
werc: &cgi.Handler{
Path: filepath.Join(root, "bin", "werc.rc"),
Dir: filepath.Join(root, "bin"),
InheritEnv: []string{
"PLAN9",
},
PathLocationHandler: nil,
},
}
}