-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
127 lines (104 loc) · 2.94 KB
/
routes.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
type invalidRequestResponse struct {
Message string `json:"message"`
}
func (s *server) routes() {
s.router.HandleFunc("/ping", s.handlePing()).Methods("GET")
s.router.HandleFunc("/regions", s.handleGetRegions()).Methods("GET")
s.router.HandleFunc("/subregions", s.handleGetSubregions()).Methods("GET")
s.router.HandleFunc("/pollen", s.HandleGetAllReports()).Methods("GET")
s.router.HandleFunc("/pollen/subregion/{subregion}", s.handleGetSubRegion()).Methods("GET")
s.router.HandleFunc("/pollen/region/{region}", s.handleGetRegion()).Methods("GET")
}
func (s *server) handlePing() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data := struct {
Message string `json:"message"`
}{"pong"}
respond(w, http.StatusOK, data)
}
}
func (s *server) HandleGetAllReports() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rs, err := s.storage.AllReports()
if err != nil {
log.Println(err)
respond(w, http.StatusInternalServerError, nil)
return
}
respond(w, http.StatusOK, rs)
}
}
func (s *server) handleGetSubRegion() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
v := mux.Vars(r)
subRegion := v["subregion"]
data, err := s.storage.GetBySubregion(subRegion)
if err != nil {
if err == ErrNotFound {
respond(w, http.StatusNotFound, &invalidRequestResponse{"No data found"})
return
}
respond(w, http.StatusInternalServerError, nil)
return
}
respond(w, http.StatusOK, data)
}
}
func (s *server) handleGetRegions() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rs, err := s.storage.AllRegions()
if err != nil {
respond(w, http.StatusInternalServerError, nil)
return
}
respond(w, http.StatusOK, rs)
}
}
func (s *server) handleGetSubregions() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rs, err := s.storage.AllSubregions()
if err != nil {
respond(w, http.StatusInternalServerError, nil)
return
}
respond(w, http.StatusOK, rs)
}
}
func (s *server) handleGetRegion() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
reg := mux.Vars(r)["region"]
rs, err := s.storage.GetByRegion(reg)
if err != nil {
if err == ErrNotFound {
respond(w, http.StatusNotFound, &invalidRequestResponse{"No data found"})
return
}
respond(w, http.StatusInternalServerError, nil)
return
}
respond(w, http.StatusOK, rs)
}
}
func respond(w http.ResponseWriter, status int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
if data == nil {
w.WriteHeader(status)
return
}
json, err := json.Marshal(data)
if err != nil {
log.Printf("[routes] unable to marshal response data: %q", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(status)
w.Write(json)
}