-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
100 lines (78 loc) · 2.86 KB
/
api.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
package main
import (
"log"
"net/http"
)
type APIServer struct {
listenAddr string
Store Storage
}
func NewAPIServer(listenAddr string, store Storage) *APIServer {
return &APIServer{
listenAddr: listenAddr,
Store: store,
}
}
func (s *APIServer) Run() error {
router := http.NewServeMux()
router.HandleFunc("/users", MakeHandler(s.handleUser))
router.HandleFunc("/feeds", MakeHandler(s.handleFeed))
router.HandleFunc("/feed_follows", MakeHandler(s.handleFeedFollows))
router.HandleFunc("/feed_follows/{feedFollowID}", MakeHandler(s.handleFeedFollowsById))
router.HandleFunc("/posts", MakeHandler(s.handlePosts))
server := http.Server{
Addr: "0.0.0.0:" + s.listenAddr,
Handler: router,
}
log.Println("App is running on port:", s.listenAddr)
return server.ListenAndServe()
}
/* These following functions ensuring the right usage of http METHOD
with JSON response if theres an error,
and also act as a wrapper for authorized endpoints
*/
func (s *APIServer) handleUser(res http.ResponseWriter, req *http.Request) error {
if req.Method == "POST" {
return s.HandleCreateUser(res, req)
}
if req.Method == "GET" {
s.middlewareAuth(s.HandleGetUser).ServeHTTP(res, req)
return nil
}
return writeJSON(res, http.StatusBadRequest, map[string]string{"msg":req.Method + " is not allowed"})
}
func (s *APIServer) handleFeed(res http.ResponseWriter, req *http.Request) error {
if req.Method == "GET" {
return s.HandleGetFeeds(res, req)
}
if req.Method == "POST" {
s.middlewareAuth(s.HandleCreateFeed).ServeHTTP(res, req)
return nil
}
return writeJSON(res, http.StatusBadRequest, map[string]string{"msg":req.Method + " is not allowed"})
}
func (s *APIServer) handleFeedFollows(res http.ResponseWriter, req *http.Request) error {
if req.Method == "POST" {
s.middlewareAuth(s.HandleCreateFeedFollows).ServeHTTP(res, req)
return nil
}
if req.Method == "GET" {
s.middlewareAuth(s.HandleGetFeedFollows).ServeHTTP(res, req)
return nil
}
return writeJSON(res, http.StatusBadRequest, map[string]string{"msg":req.Method + " is not allowed"})
}
func (s *APIServer) handleFeedFollowsById(res http.ResponseWriter, req *http.Request) error {
if req.Method == "DELETE" {
s.middlewareAuth(s.HandleDeleteFeedFollows).ServeHTTP(res, req)
return nil
}
return writeJSON(res, http.StatusBadRequest, map[string]string{"msg":req.Method + " is not allowed"})
}
func (s *APIServer) handlePosts(res http.ResponseWriter, req *http.Request) error {
if req.Method == "GET" {
s.middlewareAuth(s.HandleGetPostsForUsers).ServeHTTP(res, req)
return nil
}
return writeJSON(res, http.StatusBadRequest, map[string]string{"msg":req.Method + " is not allowed"})
}