/
proxy.go
65 lines (52 loc) · 1.22 KB
/
proxy.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
package main
import (
"fmt"
"github.com/gorilla/mux"
"github.com/mailgun/log"
"io"
"io/ioutil"
"mime"
"net/http"
"path"
"strings"
)
func proxyHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
domain := vars["domain"]
reqPath := vars["path"]
if domain == "github.com" {
domain = "raw.githubusercontent.com"
reqPath = strings.Replace(reqPath, "/blob", "", 1)
}
url := fmt.Sprintf("%s/%s", domain, reqPath)
cachePath := fmt.Sprintf("%s/%s", tmpDir, url)
// Check for file in cache.
if useFileCache {
contents, err := ioutil.ReadFile(path.Clean(cachePath))
if err == nil {
log.Infof("HIT %s", cachePath)
addCacheHeader(&w, "yes")
w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(reqPath)))
w.Write(contents)
return
}
}
resp, err := http.Get(fmt.Sprintf("http://%s", url))
if err != nil {
log.Infof("ERROR %s", err.Error())
http.Error(w, http.StatusText(500), 500)
return
}
if resp.StatusCode >= 400 {
http.Error(w, resp.Status, resp.StatusCode)
return
}
log.Infof("PROXY %s", url)
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
if useFileCache {
writeAndCache(w, resp.Body, cachePath)
return
}
io.Copy(w, resp.Body)
return
}