-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvhost.go
176 lines (154 loc) · 4.12 KB
/
vhost.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// vhost is a simple HTTP proxy server front-end for virtual host backends.
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
)
var errorTemplate = template.Must(template.New("error").Parse(`<!DOCTYPE html>
<html>
<head>
<title>{{.StatusCode}} {{.StatusText}}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>{{.StatusText}}</h1>
<pre>
{{.Message}}
</pre>
</body>
</html>
`))
var errorMessages = map[int]string{
http.StatusNotFound: `Hmmm, there's nothing here matching that URL :-(
Maybe a typo in the URL?`,
http.StatusInternalServerError: `Dang it, something broke :-(
Our hackers are working on it...
Please try again later`,
}
func writeDefaultError(w http.ResponseWriter, statusCode int) {
w.WriteHeader(statusCode)
errorTemplate.Execute(w, map[string]interface{}{
"StatusCode": statusCode,
"StatusText": http.StatusText(statusCode),
"Message": errorMessages[statusCode],
})
}
// File is used for canned error responses
type File struct {
Name string `json:"file"`
StatusCode int // HTTP status code
}
func writeResponse(w http.ResponseWriter, statusCode int, f *File) {
if f != nil {
b, err := ioutil.ReadFile(f.Name)
if err == nil {
if f.StatusCode != 0 {
statusCode = f.StatusCode
}
w.WriteHeader(statusCode)
w.Write(b)
return
}
log.Printf("Failed to read `%s`: %v", f.Name, err)
}
// send built-in response
writeDefaultError(w, statusCode)
}
// A Server is a backend handling the forwarded request from Proxy
type Server struct {
URL string
Backend string
InternalError *File
}
// Vhost is an http.Handler whose ServeHTTP forwards the request to
// backend Servers according to the incoming request URL
type Vhost struct {
config Config
servers map[string]*Server
handlers map[*Server]http.Handler
}
func (v *Vhost) ServeHTTP(w http.ResponseWriter, r *http.Request) {
originalUrl := r.Host + r.URL.Path
server := v.servers[r.Host]
if server == nil {
log.Printf("Not Found: `%s`", originalUrl)
writeResponse(w, http.StatusNotFound, v.config.NotFound)
return
}
defer func() {
if val := recover(); val != nil {
log.Printf("Error proxying request `%s` to `%s`: %v", originalUrl, r.URL, val)
er := server.InternalError
if er == nil {
er = v.config.InternalError
}
writeResponse(w, http.StatusInternalServerError, er)
}
}()
v.handlers[server].ServeHTTP(w, r)
}
// PanicyRoundTripper decorates an http.RoundTripper and panics if
// RoundTrip fails with an error
type PanicyRoundTripper struct {
Transport http.RoundTripper
}
func (rt *PanicyRoundTripper) RoundTrip(r *http.Request) (res *http.Response, err error) {
res, err = rt.Transport.RoundTrip(r)
if err != nil {
panic("proxy: " + err.Error())
}
return
}
// Config is the complete server config and what the JSON config file
// is parsed into
type Config struct {
Port int
NotFound *File
InternalError *File
Proxies []*Server `json:"proxy"`
}
func applyConfig() *Vhost {
file, err := os.Open(*configFile)
if err != nil {
log.Fatalf("Failed to open config file `%s`: %v\n", *configFile, err)
}
config := Config{}
err = json.NewDecoder(file).Decode(&config)
if err != nil {
log.Fatalf("Failed to decode JSON config file `%s`: %v\n", *configFile, err)
}
servers := make(map[string]*Server)
handlers := make(map[*Server]http.Handler)
for _, s := range config.Proxies {
servers[s.URL] = s
url, err := url.Parse(s.Backend)
if err != nil {
log.Fatalf("Failed to parse URL `%s`: %v\n", s.Backend, err)
}
rp := httputil.NewSingleHostReverseProxy(url)
rp.Transport = &PanicyRoundTripper{http.DefaultTransport}
handlers[s] = rp
}
return &Vhost{config, servers, handlers}
}
var configFile = flag.String("config_file", "", "JSON config file. Required")
func main() {
flag.Parse()
if *configFile == "" {
flag.Usage()
os.Exit(1)
}
vhost := applyConfig()
err := http.ListenAndServe(fmt.Sprintf(":%d", vhost.config.Port), vhost)
if err != nil {
log.Fatalf("Unable to listen on port %d: %v\n", vhost.config.Port, err)
}
}