-
Notifications
You must be signed in to change notification settings - Fork 179
/
server.go
79 lines (66 loc) · 1.77 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"strings"
// tfjson "github.com/hashicorp/terraform-json"
)
func (ro *rover) startServer(ipPort string, frontendFS http.Handler) error {
m := http.NewServeMux()
s := http.Server{Addr: ipPort, Handler: m}
m.Handle("/", frontendFS)
m.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}`)
})
m.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(ro.Plan)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing plan JSON: %s\n", err))
}
case "rso":
j, err = json.Marshal(ro.RSO)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing rso JSON: %s\n", err))
}
case "map":
j, err = json.Marshal(ro.Map)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing map JSON: %s\n", err))
}
case "graph":
j, err = json.Marshal(ro.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.Printf("Rover is running on %s", ipPort)
l, err := net.Listen("tcp", ipPort)
if err != nil {
log.Fatal(err)
}
// The browser can connect now because the listening socket is open.
if ro.GenImage {
go screenshot(&s)
}
// Start the blocking server loop.
return s.Serve(l)
}