/
main.go
84 lines (69 loc) · 1.8 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
)
// Message is used for storing the
type Message struct {
Hostname string `json:"hostname"`
ServiceName string `json:"service_name"`
Error string `json:"error,omitempty"`
Messages []Message `json:"messages,omitempty"`
}
var hostname string
func splitPath(path string) (string, string) {
if paths := strings.SplitN(path, "/", 2); len(paths) > 1 {
return paths[0], paths[1]
}
return path, ""
}
// NewMessage creates a new Message
func NewMessage(path string) Message {
msg := Message{
Hostname: hostname,
ServiceName: os.Getenv("SERVICE_NAME"),
Messages: make([]Message, 0),
}
if service, path := splitPath(path); service != "" {
msg.Messages = append(msg.Messages, fetchSubURL(service, path))
}
return msg
}
func fetchSubURL(service, path string) Message {
url := fmt.Sprintf("http://nestsvc-%s/%s", service, path)
msg := Message{}
res, err := http.Get(url)
if err != nil {
fmt.Println("Failed request:", err)
msg.Error = err.Error()
return msg
}
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&msg); err != nil {
fmt.Println("Failed Decoding Response:", err)
}
return msg
}
func handleRequest(w http.ResponseWriter, req *http.Request) {
fmt.Println("Handling Request:", req.URL.Path)
m := NewMessage(req.URL.Path[1:])
json := json.NewEncoder(w)
w.Header().Set("Content-Type", "application/json")
json.Encode(m)
}
func handleIcon(w http.ResponseWriter, req *http.Request) {
}
func main() {
var err error
if hostname, err = os.Hostname(); err != nil {
fmt.Println("Unable to find Hostname")
os.Exit(1)
}
http.HandleFunc("/", handleRequest)
http.HandleFunc("/favicon.ico", handleIcon)
fmt.Println("Service Listening :3000")
http.ListenAndServe(":3000", nil)
}