-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (46 loc) · 1.2 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
package main
import (
"encoding/json"
"log"
"net/http"
)
// Response struct is used for http body responses
type Response struct {
Status int `json:"status"`
Message string `json:"message"`
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
data := Response{http.StatusOK, "Hello, World"}
responseData, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(responseData)
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
data := Response{http.StatusOK, "Service is healthy"}
responseData, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(responseData)
}
// ServerMux provides http routing
func ServerMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/", helloHandler)
mux.HandleFunc("/health", healthHandler)
return mux
}
func main() {
mux := ServerMux()
port := ":8080"
log.Printf("Starting server on %s\n", port)
http.ListenAndServe(port, mux)
}