-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
62 lines (50 loc) · 1.44 KB
/
main.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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
)
// DispatchRequest returns response received from target resource
func DispatchRequest(w http.ResponseWriter, r *http.Request) {
// params := mux.Vars(r) // Need to embed optional slash parsinfg in URL
params := r.URL.Query()
targetURL := params.Get("u")
if targetURL == "" {
w.Write([]byte("Exiting request. URL not specified."))
return
}
// TBD: Validate `targetURL` structure in preflight request (1).
// TBD: Allow more params for customized functionality (2).
response, err := http.Get(targetURL)
if err != nil {
w.Write([]byte("Something went wrong :("))
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
w.Header().Set("X-Frame-Options", "SAMEORIGIN")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write([]byte(string(body)))
}
func main() {
r := mux.NewRouter()
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
r.
PathPrefix("/static/").
Handler(http.StripPrefix("/static/",
http.FileServer(http.Dir("."+"/static/"))))
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
htmlContent, err := ioutil.ReadFile("static/usage.html")
if err != nil {
w.Write([]byte("There's a runtime issue with the app :("))
} else {
w.Write([]byte(htmlContent))
}
}).Methods("GET")
r.HandleFunc("/proxy", DispatchRequest).Methods("GET")
log.Fatal(http.ListenAndServe(":"+port, r))
}