-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
45 lines (39 loc) · 971 Bytes
/
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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"github.com/TV4/graceful"
"github.com/gorilla/mux"
)
var default_page []byte
var health_page []byte
func main() {
// read output pages into mem
var err error
default_page, err = ioutil.ReadFile("static/default.html")
health_page, err = ioutil.ReadFile("static/healthz.html")
if err != nil {
log.Fatal(err)
os.Exit(42)
}
// Setup routes
router := mux.NewRouter()
router.HandleFunc("/healthz", GetHealth)
router.PathPrefix("/").HandlerFunc(GetRoot)
// Handles gracefull shutdown nicely
graceful.LogListenAndServe(&http.Server{
Addr: ":8080",
Handler: router,
})
}
func GetRoot(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
w.Write(default_page)
}
func GetHealth(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html; charset=UTF-8")
w.Write(health_page)
}