-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb.go
71 lines (59 loc) · 1.36 KB
/
web.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
package main
import (
"bytes"
"github.com/clarkduvall/spoonerizer/spoonerize"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
)
type SneakyTransport struct {
http.RoundTripper
ExtraHTML string
}
func (t *SneakyTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
// Use default value for Host.
req.Host = ""
// Make sure the response isn't gzipped.
req.Header.Del("Accept-Encoding")
resp, err = t.RoundTripper.RoundTrip(req)
if err != nil {
return
}
for _, v := range resp.Header["Content-Type"] {
if strings.Contains(v, "text/html") {
oldBody := resp.Body
resp.Body = spoonerize.SpoonerizeHTML(oldBody, t.ExtraHTML)
oldBody.Close()
break
}
}
return
}
func extraHTML() string {
html, err := ioutil.ReadFile("extra.html")
if err != nil {
return ""
}
var b bytes.Buffer
t := template.Must(template.New("extra").Parse(string(html)))
t.Execute(&b, os.Getenv("GA_TRACKING_ID"))
return b.String()
}
func main() {
baseURL := os.Getenv("BASE_URL")
if baseURL == "" {
log.Fatal("Please set the BASE_URL environment variable.")
}
parsedURL, err := url.Parse(baseURL)
if err != nil {
panic(err)
}
rev := httputil.NewSingleHostReverseProxy(parsedURL)
rev.Transport = &SneakyTransport{http.DefaultTransport, extraHTML()}
http.ListenAndServe(":"+os.Getenv("PORT"), rev)
}