-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
53 lines (44 loc) · 1.27 KB
/
main.go
File metadata and controls
53 lines (44 loc) · 1.27 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/melsheikh92/go-microservices/details"
)
func main() {
fmt.Println("Hello world!")
router := mux.NewRouter()
router.HandleFunc("/book/{id}/{page}", func(wr http.ResponseWriter, rq *http.Request) {
vars := mux.Vars(rq)
id := vars["id"]
page := vars["page"]
fmt.Fprintf(wr, "Book id: %s, page: %s", id, page)
})
router.HandleFunc("/health", func(wr http.ResponseWriter, req *http.Request) {
response := map[string]string{
"status": "up",
"timestamp": time.Now().String(),
}
json.NewEncoder(wr).Encode(response)
})
router.HandleFunc("/", func(wr http.ResponseWriter, req *http.Request) {
wr.WriteHeader(http.StatusOK)
fmt.Fprintf(wr, "Application is up and running")
})
router.HandleFunc("/details", func(wr http.ResponseWriter, req *http.Request) {
host, err := details.GetHostName()
if err != nil {
wr.WriteHeader(http.StatusExpectationFailed)
return
}
wr.WriteHeader(http.StatusOK)
addrs := details.GetLocalIp()
fmt.Fprintf(wr, "HostName: %s, local ip: %s", host, addrs)
})
fs := http.FileServer(http.Dir("static/"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
log.Fatal(http.ListenAndServe(":80", router))
}