-
Notifications
You must be signed in to change notification settings - Fork 2
/
apiconsole.go
132 lines (113 loc) · 2.64 KB
/
apiconsole.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"bytes"
"flag"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"github.com/GeertJohan/go.rice"
)
func main() {
upstream := flag.String(
"upstream",
"http://localhost",
"An upstream server to which all requests are forwarded.",
)
mount := flag.String(
"mount",
"/devel/console",
"A mount point that the console is served. Note that the static files related to the console are also served under this path.",
)
extra := flag.String(
"append",
"",
"Append lines at the end of the body of the console page.",
)
port := flag.Int(
"port",
8000,
"Port to listen",
)
flag.Parse()
staticHandler := http.FileServer(rice.MustFindBox("static").HTTPBox())
upstreamURL, err := url.Parse(*upstream)
if err != nil {
panic(err)
}
director := func(req *http.Request) {
req.URL.Scheme = upstreamURL.Scheme
req.URL.Host = upstreamURL.Host
req.Host = upstreamURL.Host
}
upstreamHandler := &httputil.ReverseProxy{Director: director}
go func() {
log.Printf("Console: http://localhost:%d%s/", *port, *mount)
}()
panic(http.ListenAndServe(fmt.Sprintf(":%d", *port), &handler{
extra: *extra,
staticPath: *mount,
staticHandler: http.StripPrefix(*mount, staticHandler),
upstreamHandler: upstreamHandler,
}))
}
type handler struct {
extra string
staticPath string
staticHandler http.Handler
upstreamHandler http.Handler
}
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
method := req.Method
path := req.URL.Path
res := &responseWithStatus{ResponseWriter: w}
defer func() {
log.Printf(
"method:%s\tpath:%s\tstatus:%d",
method,
path,
res.status,
)
}()
if !strings.HasPrefix(req.URL.Path, h.staticPath) {
h.upstreamHandler.ServeHTTP(res, req)
return
}
staticPath := strings.TrimPrefix(req.URL.Path, h.staticPath)
if staticPath == "" {
http.Redirect(res, req, h.staticPath+"/", http.StatusMovedPermanently)
}
if !(staticPath == "/" || staticPath == "/index.html") {
h.staticHandler.ServeHTTP(res, req)
return
}
box, err := rice.FindBox("static")
if err != nil {
res.WriteHeader(404)
return
}
buf, err := box.Bytes("index.html")
if err != nil {
http.NotFound(res, req)
return
}
res.WriteHeader(200)
for _, line := range bytes.Split(buf, []byte{0xa}) {
if strings.Index(string(line), "</body>") >= 0 {
res.Write([]byte(h.extra))
res.Write([]byte{0xa})
}
res.Write(line)
res.Write([]byte{0xa})
}
}
type responseWithStatus struct {
http.ResponseWriter
status int
}
func (r *responseWithStatus) WriteHeader(status int) {
r.status = status
r.ResponseWriter.WriteHeader(status)
}