forked from im2nguyen/rover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
64 lines (54 loc) · 1.54 KB
/
server.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
tfjson "github.com/hashicorp/terraform-json"
)
func startServer(frontendFS http.Handler, plan *tfjson.Plan, rso *ResourcesOverview, mapDM *Map, graph Graph) error {
http.Handle("/", frontendFS)
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
// simple healthcheck
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"alive": true}`)
})
http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
fileType := strings.Replace(r.URL.Path, "/api/", "", 1)
var j []byte
var err error
enableCors(&w)
switch fileType {
case "plan":
j, err = json.Marshal(plan)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing plan JSON: %s\n", err))
}
case "rso":
j, err = json.Marshal(rso)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing rso JSON: %s\n", err))
}
case "map":
j, err = json.Marshal(mapDM)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing map JSON: %s\n", err))
}
case "graph":
j, err = json.Marshal(graph)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing graph JSON: %s\n", err))
}
default:
io.WriteString(w, "Please enter a valid file type: plan, rso, map, graph\n")
}
w.Header().Set("Content-Type", "application/json")
io.Copy(w, bytes.NewReader(j))
})
log.Println("Rover is running on localhost:9000")
return http.ListenAndServe(":9000", nil)
}