From af01b635544c62527e26db10c9f178ddec9b0400 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sat, 31 Aug 2019 17:39:26 +0800 Subject: [PATCH] Break apart middleware, router and main --- main.go | 59 +-------------------------------------------------- middleware.go | 34 +++++++++++++++++++++++++++++ router.go | 42 ++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 58 deletions(-) create mode 100644 middleware.go create mode 100644 router.go diff --git a/main.go b/main.go index d1f7690..838e1be 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,12 @@ package main import ( - "context" "database/sql" - "encoding/json" "log" "net/http" "os" "github.com/joho/godotenv" - "github.com/julienschmidt/httprouter" _ "github.com/lib/pq" ) @@ -35,63 +32,9 @@ func main() { // Handler h := NewHandler(db) - // Routes - router := httprouter.New() - // Users - router.POST("/user", h.CreateUser) - router.GET("/user", h.GetUserByPhone) - router.GET("/user/id/:user", h.GetUser) - router.GET("/user/username/:username", h.GetUserByUsername) - router.PATCH("/user", h.UpdateUser) - // Conversations - router.POST("/user/conversation", AuthMiddleware(h.CreateConversation)) - router.GET("/user/conversation", AuthMiddleware(h.GetConversations)) // USER MEMBER CONVERSATION - router.DELETE("/user/conversation/:conversation", AuthMiddleware(h.DeleteConversation)) - //router.GET("/user/:user/conversation/bymembers/", h.GetConversationsByMembers) // TODO - router.GET("/user/conversation/:conversation", AuthMiddleware(h.GetConversation)) // USER MEMBER CONVERSATION - router.PATCH("/user/conversation/:conversation", AuthMiddleware(h.UpdateConversation)) // USER MEMBER CONVERSATION ADMIN=true -> update conversation title - //router.DELETE("/user/:user/conversation/:conversation", h.DeleteConversation) // USER MEMBER CONVERSATION -> delete membership - router.POST("/user/conversation/:conversation/pin", AuthMiddleware(h.PinConversation)) - router.POST("/user/conversation/:conversation/member", AuthMiddleware(h.CreateConversationMember)) // USER MEMBER CONVERSATION ADMIN=true -> create new membership - router.GET("/user/conversation/:conversation/member", AuthMiddleware(h.GetConversationMembers)) // USER MEMBER CONVERSATION - //router.DELETE("/user/:user/conversation/:conversation/member/:member", h.DeleteConversationMember) // USER MEMBER CONVERSATION ADMIN=true -> delete membership - // Last heard - //router.GET("/user/:user/lastheard/:conversation", h.GetLastheard) - //router.PUT("/user/:user/lastheard/:conversation", h.SetLastheard) - // Contacts - router.POST("/user/contact", AuthMiddleware(h.CreateContact)) - router.GET("/user/contact", AuthMiddleware(h.GetContacts)) - //router.GET("/user/:user/contact/:contact", h.GetContact) - //router.DELETE("/user/:user/contact/:contact", h.DeleteContact) - //router.GET("/user/:user/contact/:contact/conversation/", h.GetContactConversations) + router := NewRouter(h) log.Printf("starting server on %s", listen) log.Fatal(http.ListenAndServe(listen, router)) } - -type RawClient struct { - UserId string `json:"userid"` - ClientId string `json:"clientid"` -} - -func AuthMiddleware(next httprouter.Handle) httprouter.Handle { - return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { - ua := r.Header.Get("X-User-Claim") - if ua == "" { - http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) - return - } - - var client RawClient - err := json.Unmarshal([]byte(ua), &client) - - if err != nil { - http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) - return - } - - context := context.WithValue(r.Context(), "user", client.UserId) - next(w, r.WithContext(context), p) - } -} diff --git a/middleware.go b/middleware.go new file mode 100644 index 0000000..728aa3d --- /dev/null +++ b/middleware.go @@ -0,0 +1,34 @@ +package main + +import ( + "context" + "encoding/json" + "net/http" + + "github.com/julienschmidt/httprouter" +) + +type RawClient struct { + UserId string `json:"userid"` + ClientId string `json:"clientid"` +} + +func AuthMiddleware(next httprouter.Handle) httprouter.Handle { + return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + ua := r.Header.Get("X-User-Claim") + if ua == "" { + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + return + } + + var client RawClient + err := json.Unmarshal([]byte(ua), &client) + if err != nil { + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + return + } + + context := context.WithValue(r.Context(), "user", client.UserId) + next(w, r.WithContext(context), p) + } +} diff --git a/router.go b/router.go new file mode 100644 index 0000000..d56b429 --- /dev/null +++ b/router.go @@ -0,0 +1,42 @@ +package main + +import ( + "github.com/julienschmidt/httprouter" +) + +func NewRouter(h *Handler) *httprouter.Router { + router := httprouter.New() + + // Users + router.POST("/user", h.CreateUser) + router.GET("/user", h.GetUserByPhone) + router.GET("/user/id/:user", h.GetUser) + router.GET("/user/username/:username", h.GetUserByUsername) + router.PATCH("/user", h.UpdateUser) + + // Conversations + router.POST("/user/conversation", AuthMiddleware(h.CreateConversation)) + router.GET("/user/conversation", AuthMiddleware(h.GetConversations)) // USER MEMBER CONVERSATION + router.DELETE("/user/conversation/:conversation", AuthMiddleware(h.DeleteConversation)) + //router.GET("/user/:user/conversation/bymembers/", h.GetConversationsByMembers) // TODO + router.GET("/user/conversation/:conversation", AuthMiddleware(h.GetConversation)) // USER MEMBER CONVERSATION + router.PATCH("/user/conversation/:conversation", AuthMiddleware(h.UpdateConversation)) // USER MEMBER CONVERSATION ADMIN=true -> update conversation title + //router.DELETE("/user/:user/conversation/:conversation", h.DeleteConversation) // USER MEMBER CONVERSATION -> delete membership + router.POST("/user/conversation/:conversation/pin", AuthMiddleware(h.PinConversation)) + router.POST("/user/conversation/:conversation/member", AuthMiddleware(h.CreateConversationMember)) // USER MEMBER CONVERSATION ADMIN=true -> create new membership + router.GET("/user/conversation/:conversation/member", AuthMiddleware(h.GetConversationMembers)) // USER MEMBER CONVERSATION + //router.DELETE("/user/:user/conversation/:conversation/member/:member", h.DeleteConversationMember) // USER MEMBER CONVERSATION ADMIN=true -> delete membership + + // Last heard + //router.GET("/user/:user/lastheard/:conversation", h.GetLastheard) + //router.PUT("/user/:user/lastheard/:conversation", h.SetLastheard) + + // Contacts + router.POST("/user/contact", AuthMiddleware(h.CreateContact)) + router.GET("/user/contact", AuthMiddleware(h.GetContacts)) + //router.GET("/user/:user/contact/:contact", h.GetContact) + //router.DELETE("/user/:user/contact/:contact", h.DeleteContact) + //router.GET("/user/:user/contact/:contact/conversation/", h.GetContactConversations) + + return router +}