-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandlers.go
More file actions
69 lines (64 loc) · 1.75 KB
/
Copy pathhandlers.go
File metadata and controls
69 lines (64 loc) · 1.75 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
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
)
func reactAppServe(w http.ResponseWriter, req *http.Request) {
if !isDevelopment {
f, err := os.Open("./client/build/index.html")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Something bad happened"))
fmt.Println(err)
return
}
defer f.Close()
s := bufio.NewScanner(f)
var b bytes.Buffer
for s.Scan() {
b.WriteString(s.Text())
}
fmt.Fprintf(w, b.String())
} else {
http.Redirect(w, req, "http://localhost:3000", http.StatusSeeOther)
fmt.Printf("\n[+] This is development env, please view React app itself directly for debugging\n")
fmt.Printf("[+] Redirecting to http://localhost:3000\n")
}
}
func vegetaHandler(w http.ResponseWriter, req *http.Request) {
setupResponse(&w, req)
if req.Method == http.MethodPost {
b, err := ioutil.ReadAll(req.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Bad Request"))
fmt.Println(err)
return
}
vo, err := mapVegetaOptions(b)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Bad Request"))
fmt.Println(err)
return
}
res, ct, err := execVegetaCall(vo)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Bad Request"))
fmt.Println(err)
return
}
w.Header().Add("Content-Type", ct)
w.Write([]byte(res))
}
}
func setupResponse(w *http.ResponseWriter, req *http.Request) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
(*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
(*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}