-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
111 lines (87 loc) · 2.53 KB
/
Copy pathmain.go
File metadata and controls
111 lines (87 loc) · 2.53 KB
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
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
)
var proxypath = "/proxy/"
var proxypreviewpath = "/proxy/preview/"
var proxyhost = os.Getenv("BWHERO_HOST")
var listen = os.Getenv("LISTEN")
var allowOrigin = "*"
func init() {
if ao, e := os.LookupEnv("ALLOW_ORIGIN"); e {
allowOrigin = ao
}
}
func main() {
if proxyhost == "" || listen == "" {
log.Println("please set LISTEN and BWHERO_HOST environment variable before running.")
log.Println(" LISTEN value could be 0.0.0.0:2000")
log.Println(" BWHERO_HOST could be http://localhost:8080/")
return
}
http.HandleFunc(proxypath, func(w http.ResponseWriter, r *http.Request) {
url, err := getUrl(r.URL.Path[len(proxypath):])
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
filename := r.URL.Query().Get("name")
log.Println("proxying", url)
resp, err := proxy(r.Context(), r, url)
if err != nil {
log.Println("give up on fetching to upstream:", err)
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
sex(w, resp.Body, resp, filename)
})
http.HandleFunc(proxypreviewpath, func(w http.ResponseWriter, r *http.Request) {
url, err := getUrl(r.URL.Path[len(proxypreviewpath):])
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
q := r.URL.Query()
isStatic := q.Get("static") == "true"
filename := q.Get("name")
log.Println("previewing", url)
resp, err := proxy(r.Context(), r, buildUrl(url, isStatic))
if err != nil {
log.Println("can't fetch from bwhero:", err)
resp, err := proxy(r.Context(), r, url)
if err != nil {
log.Println("give up on fetching to upstream:", err)
http.Error(w, "clusterbad", http.StatusBadGateway)
return
}
sex(w, resp.Body, resp, filename)
return
}
sex(w, resp.Body, resp, filename)
})
log.Println("Listening at", listen)
if err := http.ListenAndServe(listen, nil); err != nil {
panic(err)
}
}
func sex(hole http.ResponseWriter, dih io.ReadCloser, sperm *http.Response, childname string) {
defer dih.Close()
h := hole.Header()
h.Set("Access-Control-Allow-Credentials", "true")
h.Set("Access-Control-Allow-Origin", allowOrigin)
h.Set("Cache-Control", "public, max-age=604800, immutable")
if sperm.ContentLength > 0 {
h.Set("Content-Length", strconv.FormatInt(sperm.ContentLength, 10))
}
copyUpstreamHeaders(h, sperm.Header)
if len(childname) > 0 {
h.Set("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", childname))
}
hole.WriteHeader(sperm.StatusCode)
io.Copy(hole, dih)
}