|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "net/http/httputil" |
| 10 | + "net/url" |
4 | 11 | "os" |
5 | | - "fmt" |
6 | | - "log" |
7 | | - "strings" |
8 | | - "strconv" |
9 | | - "io/ioutil" |
10 | | - "encoding/json" |
11 | | - "net/url" |
12 | | - "net/http" |
13 | | - "net/http/httputil" |
14 | | - "github.com/PuerkitoBio/goquery" |
| 12 | + "strconv" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/PuerkitoBio/goquery" |
15 | 16 | ) |
16 | 17 |
|
17 | 18 | type HypernovaResult struct { |
18 | | - Success bool |
19 | | - Html string |
20 | | - Name string |
| 19 | + Success bool |
| 20 | + Html string |
| 21 | + Name string |
21 | 22 | } |
22 | 23 |
|
23 | 24 | type HypernovaResponse struct { |
24 | | - Results map[string]HypernovaResult |
| 25 | + Results map[string]HypernovaResult |
25 | 26 | } |
26 | 27 |
|
27 | 28 | type Location struct { |
28 | | - Path string |
29 | | - Host string |
30 | | - ModifyResponse bool |
| 29 | + Path string |
| 30 | + Host string |
| 31 | + ModifyResponse bool |
31 | 32 | } |
32 | 33 |
|
33 | 34 | type Configuration struct { |
34 | | - Locations []Location |
| 35 | + Locations []Location |
35 | 36 | } |
36 | 37 |
|
37 | 38 | func createQuery(tag string, uuid string, name string) string { |
38 | | - query := fmt.Sprintf("%s[data-hypernova-id=\"%s\"][data-hypernova-key=\"%s\"]", tag, uuid, name) |
| 39 | + query := fmt.Sprintf("%s[data-hypernova-id=\"%s\"][data-hypernova-key=\"%s\"]", tag, uuid, name) |
39 | 40 |
|
40 | | - return query |
| 41 | + return query |
41 | 42 | } |
42 | 43 |
|
43 | 44 | func modifyBody(html string) string { |
44 | | - doc, err := goquery.NewDocumentFromReader(strings.NewReader(html)) |
45 | | - |
| 45 | + doc, err := goquery.NewDocumentFromReader(strings.NewReader(html)) |
| 46 | + |
46 | 47 | if err != nil { |
47 | 48 | log.Fatal(err) |
48 | | - } |
| 49 | + } |
49 | 50 |
|
50 | | - batch := make(map[string]map[string]interface{}) |
51 | | - |
52 | | - doc.Find("div[data-hypernova-key]").Each(func(i int, s *goquery.Selection) { |
53 | | - uuid, uuidOk := s.Attr("data-hypernova-id") |
54 | | - name, nameOk := s.Attr("data-hypernova-key") |
55 | | - if !uuidOk || !nameOk { |
56 | | - return |
57 | | - } |
| 51 | + batch := make(map[string]map[string]interface{}) |
58 | 52 |
|
59 | | - scriptQuery := createQuery("script", uuid, name) |
| 53 | + doc.Find("div[data-hypernova-key]").Each(func(i int, s *goquery.Selection) { |
| 54 | + uuid, uuidOk := s.Attr("data-hypernova-id") |
| 55 | + name, nameOk := s.Attr("data-hypernova-key") |
| 56 | + if !uuidOk || !nameOk { |
| 57 | + return |
| 58 | + } |
60 | 59 |
|
61 | | - script := doc.Find(scriptQuery).First() |
| 60 | + scriptQuery := createQuery("script", uuid, name) |
62 | 61 |
|
63 | | - if script == nil { |
64 | | - return |
65 | | - } |
| 62 | + script := doc.Find(scriptQuery).First() |
66 | 63 |
|
67 | | - content := script.Text() |
68 | | - content = content[4:(len(content) - 3)] |
69 | | - |
70 | | - var data interface{} |
| 64 | + if script == nil { |
| 65 | + return |
| 66 | + } |
71 | 67 |
|
72 | | - json.Unmarshal([]byte(content), &data) |
| 68 | + content := script.Text() |
| 69 | + content = content[4:(len(content) - 3)] |
73 | 70 |
|
74 | | - batch[uuid] = make(map[string]interface{}) |
75 | | - batch[uuid]["name"] = name |
76 | | - batch[uuid]["data"] = data |
77 | | - }) |
| 71 | + var data interface{} |
78 | 72 |
|
79 | | - b, encodeErr := json.Marshal(batch) |
| 73 | + json.Unmarshal([]byte(content), &data) |
80 | 74 |
|
81 | | - if encodeErr != nil { |
82 | | - log.Fatal(encodeErr) |
83 | | - } |
| 75 | + batch[uuid] = make(map[string]interface{}) |
| 76 | + batch[uuid]["name"] = name |
| 77 | + batch[uuid]["data"] = data |
| 78 | + }) |
84 | 79 |
|
85 | | - payload := string(b) |
| 80 | + b, encodeErr := json.Marshal(batch) |
86 | 81 |
|
87 | | - resp, reqErr := http.Post(os.Getenv("HYPERNOVA_BATCH"), "application/json", strings.NewReader(payload)) |
| 82 | + if encodeErr != nil { |
| 83 | + log.Fatal(encodeErr) |
| 84 | + } |
88 | 85 |
|
89 | | - if reqErr != nil { |
90 | | - log.Fatal(reqErr) |
91 | | - } |
| 86 | + payload := string(b) |
92 | 87 |
|
93 | | - defer resp.Body.Close() |
| 88 | + resp, reqErr := http.Post(os.Getenv("HYPERNOVA_BATCH"), "application/json", strings.NewReader(payload)) |
94 | 89 |
|
95 | | - body, bodyErr := ioutil.ReadAll(resp.Body) |
| 90 | + if reqErr != nil { |
| 91 | + log.Fatal(reqErr) |
| 92 | + } |
96 | 93 |
|
97 | | - if bodyErr != nil { |
98 | | - log.Fatal(bodyErr) |
99 | | - } |
100 | | - |
101 | | - var hypernovaResponse HypernovaResponse |
| 94 | + defer resp.Body.Close() |
102 | 95 |
|
103 | | - json.Unmarshal(body, &hypernovaResponse) |
| 96 | + body, bodyErr := ioutil.ReadAll(resp.Body) |
104 | 97 |
|
105 | | - for uuid, result := range hypernovaResponse.Results { |
106 | | - if !result.Success { |
107 | | - break |
108 | | - } |
109 | | - |
110 | | - scriptQuery := createQuery("script", uuid, result.Name) |
111 | | - doc.Find(scriptQuery).Remove() |
| 98 | + if bodyErr != nil { |
| 99 | + log.Fatal(bodyErr) |
| 100 | + } |
112 | 101 |
|
113 | | - divQuery := createQuery("div", uuid, result.Name) |
114 | | - doc.Find(divQuery).ReplaceWithHtml(result.Html) |
115 | | - } |
| 102 | + var hypernovaResponse HypernovaResponse |
116 | 103 |
|
117 | | - html, htmlError := doc.Html() |
| 104 | + json.Unmarshal(body, &hypernovaResponse) |
118 | 105 |
|
119 | | - if htmlError != nil { |
120 | | - log.Fatal(htmlError) |
121 | | - } |
| 106 | + for uuid, result := range hypernovaResponse.Results { |
| 107 | + if !result.Success { |
| 108 | + break |
| 109 | + } |
122 | 110 |
|
123 | | - return html |
124 | | -} |
| 111 | + scriptQuery := createQuery("script", uuid, result.Name) |
| 112 | + doc.Find(scriptQuery).Remove() |
125 | 113 |
|
126 | | -func main() { |
127 | | - setUpLocations(); |
| 114 | + divQuery := createQuery("div", uuid, result.Name) |
| 115 | + doc.Find(divQuery).ReplaceWithHtml(result.Html) |
| 116 | + } |
128 | 117 |
|
129 | | - log.Fatal(http.ListenAndServe(":8080", nil)) |
| 118 | + html, htmlError := doc.Html() |
| 119 | + |
| 120 | + if htmlError != nil { |
| 121 | + log.Fatal(htmlError) |
| 122 | + } |
| 123 | + |
| 124 | + return html |
130 | 125 | } |
131 | 126 |
|
132 | | -func setUpLocations () error { |
133 | | - b, err := ioutil.ReadFile(os.Getenv("CONFIG_FILE")); |
| 127 | +func main() { |
| 128 | + setUpLocations() |
134 | 129 |
|
135 | | - if err != nil { |
136 | | - fmt.Println("Config file not found"); |
137 | | - return err |
138 | | - } |
| 130 | + log.Fatal(http.ListenAndServe(":8080", nil)) |
| 131 | +} |
139 | 132 |
|
140 | | - var configuration Configuration |
| 133 | +func setUpLocations() error { |
| 134 | + b, err := ioutil.ReadFile(os.Getenv("CONFIG_FILE")) |
141 | 135 |
|
142 | | - json.Unmarshal(b, &configuration) |
| 136 | + if err != nil { |
| 137 | + fmt.Println("Config file not found") |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + var configuration Configuration |
| 142 | + |
| 143 | + json.Unmarshal(b, &configuration) |
| 144 | + |
| 145 | + fmt.Println(configuration) |
| 146 | + |
| 147 | + for _, location := range configuration.Locations { |
| 148 | + origin, err := url.Parse(location.Host) |
| 149 | + if err != nil { |
| 150 | + log.Fatal(err) |
| 151 | + } else { |
| 152 | + proxy := httputil.NewSingleHostReverseProxy(origin) |
| 153 | + |
| 154 | + if location.ModifyResponse { |
| 155 | + proxy.ModifyResponse = ModifyResponse |
| 156 | + proxy.Director = func(req *http.Request) { |
| 157 | + req.Header.Add("X-Forwarded-Host", req.Host) |
| 158 | + req.Header.Add("X-Origin-Host", origin.Host) |
| 159 | + req.Header.Del("Accept-Encoding") |
| 160 | + req.URL.Scheme = "http" |
| 161 | + req.URL.Host = origin.Host |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + http.Handle(location.Path, proxy) |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | +} |
143 | 171 |
|
144 | | - fmt.Println(configuration) |
| 172 | +func ModifyResponse(r *http.Response) error { |
| 173 | + contentType := r.Header.Get("Content-Type") |
| 174 | + if !strings.HasPrefix(contentType, "text/html") { |
| 175 | + return nil |
| 176 | + } |
145 | 177 |
|
146 | | - for _, location := range configuration.Locations { |
147 | | - host, err := url.Parse(location.Host) |
148 | | - if err != nil { |
149 | | - log.Fatal(err) |
150 | | - } else { |
151 | | - proxy := httputil.NewSingleHostReverseProxy(host) |
152 | | - |
153 | | - if (location.ModifyResponse) { |
154 | | - proxy.ModifyResponse = ModifyResponse |
155 | | - } |
| 178 | + html, err := ioutil.ReadAll(r.Body) |
156 | 179 |
|
157 | | - http.Handle(location.Path, proxy) |
158 | | - } |
159 | | - } |
| 180 | + if err != nil { |
| 181 | + log.Fatal(err) |
| 182 | + return err |
| 183 | + } |
160 | 184 |
|
161 | | - return nil |
162 | | -} |
| 185 | + newHtml := modifyBody(string(html)) |
163 | 186 |
|
164 | | -func ModifyResponse(r *http.Response) error { |
165 | | - contentType := r.Header.Get("Content-Type") |
166 | | - if !strings.HasPrefix(contentType, "text/html") { |
167 | | - return nil |
168 | | - } |
169 | | - |
170 | | - html, err := ioutil.ReadAll(r.Body) |
171 | | - |
172 | | - if err != nil { |
173 | | - return err |
174 | | - } |
175 | | - |
176 | | - newHtml := modifyBody(string(html)); |
177 | | - r.Body = ioutil.NopCloser(strings.NewReader(newHtml)) |
178 | | - r.ContentLength = int64(len(newHtml)) |
179 | | - r.Header.Set("Content-Length", strconv.Itoa(len(newHtml))) |
180 | | - return nil |
| 187 | + r.Body = ioutil.NopCloser(strings.NewReader(newHtml)) |
| 188 | + r.ContentLength = int64(len(newHtml)) |
| 189 | + r.Header.Set("Content-Length", strconv.Itoa(len(newHtml))) |
| 190 | + return nil |
181 | 191 | } |
0 commit comments