-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.go
75 lines (64 loc) · 2.03 KB
/
app.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
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"log"
"net/http"
)
type AuthorResp struct {
Id string `json:"id"`
Authors []*Author `json:"authors"`
}
type Author struct {
Name string `json:"name"`
Desc string `json:"desc"`
}
func main() {
r := mux.NewRouter()
r.PathPrefix("/health").Subrouter().Methods(http.MethodGet).Subrouter().HandleFunc("", health)
r.PathPrefix("/authors/{productId}").Subrouter().Methods(http.MethodGet).Subrouter().HandleFunc("", bookAuthorsById)
http.Handle("/", r)
log.Println("Start listening http port 9080 ...")
if err := http.ListenAndServe(":9080", nil); err != nil {
panic(err)
}
}
func health(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
resp, err := json.Marshal(map[string]string{
"status": "Authors is healthy",
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println(err)
return
}
w.Write(resp)
}
func getAuthors(productId string) *AuthorResp {
authorResp := &AuthorResp{
Id: productId,
Authors: []*Author{
{
Name: "William Shakespeare",
Desc: "(26 April 1564 – 23 April 1616) was an English playwright, poet, and actor, widely regarded as the greatest writer in the English language and the world's greatest dramatist. He is often called England's national poet and the \"Bard of Avon\" (or simply \"the Bard\"). His extant works, including collaborations, consist of some 39 plays, 154 sonnets, three long narrative poems, and a few other verses, some of uncertain authorship. His plays have been translated into every major living language and are performed more often than those of any other playwright.",
},
},
}
return authorResp
}
func bookAuthorsById(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
productId := vars["productId"]
authors := getAuthors(productId)
resp, err := json.Marshal(authors)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println(err)
return
}
w.Write(resp)
}