From 3e56170c993fd919260e980ff97014ff6db3ad7c Mon Sep 17 00:00:00 2001 From: Colin Henry Date: Wed, 6 Nov 2019 19:40:38 -0800 Subject: [PATCH] some code cleanup --- auth/service.go | 9 ++++----- http/server.go | 23 ++++++----------------- http/server_test.go | 2 +- payments/service.go | 4 ++-- rest/collection.go | 10 +++++----- 5 files changed, 18 insertions(+), 30 deletions(-) diff --git a/auth/service.go b/auth/service.go index ef01116..ca07180 100644 --- a/auth/service.go +++ b/auth/service.go @@ -18,17 +18,16 @@ type ServiceInstance struct { func (si ServiceInstance) Register(uriBase string, s *jch_http.Server) { - s.GET(uriBase+"/login", "login endpoint", http.HandlerFunc(NewLoginHandler(si.c))) - s.GET(uriBase+"/logout", "logout endpoint", http.HandlerFunc(LogoutHandler)) - s.GET(uriBase+"/callback", "oidc callback", http.HandlerFunc(NewCallbackHandler(si.c))) - s.GET(uriBase+"/user", "user info endpoint", negroni.New( + s.Get(uriBase+"/login", "login endpoint", http.HandlerFunc(NewLoginHandler(si.c))) + s.Get(uriBase+"/logout", "logout endpoint", http.HandlerFunc(LogoutHandler)) + s.Get(uriBase+"/callback", "oidc callback", http.HandlerFunc(NewCallbackHandler(si.c))) + s.Get(uriBase+"/user", "user info endpoint", negroni.New( negroni.HandlerFunc(IsAuthenticated), negroni.Wrap(http.HandlerFunc(UserHandler)), )) } func (si ServiceInstance) UpdateUser(u User) error { - m, err := management.New(si.c.Domain, si.c.ManagementClientID, si.c.ManagementClientSecret) if err != nil { return err diff --git a/http/server.go b/http/server.go index e234390..f3238af 100644 --- a/http/server.go +++ b/http/server.go @@ -15,7 +15,6 @@ type Router interface { go_http.Handler ServeFiles(path string, root go_http.FileSystem) AddHandler(method, path string, handler go_http.Handler) - // ServeHTTP(w http.ResponseWriter, req *http.Request) } type Service interface { @@ -31,14 +30,13 @@ func (f ServiceFunc) Register(uriBase string, restServer *Server) { var docString = "%s \t%s\t- %s" type Server struct { - //router *httprouter.Router router Router middleware Middleware } func NewServer(m Middleware, r Router) *Server { s := &Server{ - router: r, //httprouter.New(), + router: r, middleware: m, } @@ -47,26 +45,26 @@ func NewServer(m Middleware, r Router) *Server { return s } -func (r *Server) GET(path string, documentation string, handle go_http.Handler) *Server { +func (r *Server) Get(path string, documentation string, handle go_http.Handler) *Server { r.handle("GET", path, documentation, handle) return r } -func (r *Server) PATCH(path string, documentation string, handle go_http.Handler) *Server { +func (r *Server) Patch(path string, documentation string, handle go_http.Handler) *Server { r.handle("PATCH", path, documentation, handle) return r } -func (r *Server) POST(path string, documentation string, handle go_http.Handler) *Server { +func (r *Server) Post(path string, documentation string, handle go_http.Handler) *Server { r.handle("POST", path, documentation, handle) return r } -func (r *Server) PUT(path string, documentation string, handle go_http.Handler) *Server { +func (r *Server) Put(path string, documentation string, handle go_http.Handler) *Server { r.handle("PUT", path, documentation, handle) return r } -func (r *Server) DELETE(path string, documentation string, handle go_http.Handler) *Server { +func (r *Server) Delete(path string, documentation string, handle go_http.Handler) *Server { r.handle("DELETE", path, documentation, handle) return r @@ -74,15 +72,6 @@ func (r *Server) DELETE(path string, documentation string, handle go_http.Handle func (r *Server) handle(method, path string, documentation string, handler go_http.Handler) { log.Printf(docString, method, path, documentation) r.router.AddHandler(method, path, handler) - // r.router.Handle(method, path, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { - // if req.Form == nil { - // req.Form = url.Values{} - // } - // for _, param := range params { // stuffing values back into request.Form to honor the handler contract - // req.Form.Add(param.Key, param.Value) - // } - // handler.ServeHTTP(w, req) - // }) } func (r *Server) Banner(banner string) *Server { diff --git a/http/server_test.go b/http/server_test.go index dafe503..f24c007 100644 --- a/http/server_test.go +++ b/http/server_test.go @@ -21,7 +21,7 @@ func ExampleServer() { http.NewJulienschmidtHTTPRouter()). Service("", rest.Collection(new(contact), - nil, //crud.NewInMemoryCrudService(), + nil, ), ) diff --git a/payments/service.go b/payments/service.go index e0fc54e..a1ac145 100644 --- a/payments/service.go +++ b/payments/service.go @@ -33,10 +33,10 @@ type ServiceInstance struct { } func (si ServiceInstance) Register(uriBase string, s *jch_http.Server) { - s.GET(uriBase+"/subscription", "subscription info endpoint", negroni.New( + s.Get(uriBase+"/subscription", "subscription info endpoint", negroni.New( negroni.HandlerFunc(auth.IsAuthenticated), negroni.Wrap(http.HandlerFunc(si.subscriptionHandler)), - )).POST(uriBase+"/subscription", "subscription payment endpoint", negroni.New( + )).Post(uriBase+"/subscription", "subscription payment endpoint", negroni.New( negroni.HandlerFunc(auth.IsAuthenticated), negroni.Wrap(http.HandlerFunc(si.paymentHandler)), )) diff --git a/rest/collection.go b/rest/collection.go index a6e2f9e..4b5f079 100644 --- a/rest/collection.go +++ b/rest/collection.go @@ -53,11 +53,11 @@ func (collection *CollectionInstance) Register(uriBase string, restServer *jch_h urlBase := uriBase + "/" + plural //collection.name + "s" restServer. - POST(urlBase, "create a "+collection.name, http.HandlerFunc(collection.create)). - PUT(urlBase+"/:"+IDPathParameter, "update a "+collection.name, http.HandlerFunc(collection.update)). - DELETE(urlBase+"/:"+IDPathParameter, "delete a "+collection.name, http.HandlerFunc(collection.remove)). - GET(urlBase+"/:"+IDPathParameter, "get a "+collection.name+" by id", http.HandlerFunc(collection.find)). - GET(urlBase, "get "+collection.name+"s", http.HandlerFunc(collection.find)) + Post(urlBase, "create a "+collection.name, http.HandlerFunc(collection.create)). + Put(urlBase+"/:"+IDPathParameter, "update a "+collection.name, http.HandlerFunc(collection.update)). + Delete(urlBase+"/:"+IDPathParameter, "delete a "+collection.name, http.HandlerFunc(collection.remove)). + Get(urlBase+"/:"+IDPathParameter, "get a "+collection.name+" by id", http.HandlerFunc(collection.find)). + Get(urlBase, "get "+collection.name+"s", http.HandlerFunc(collection.find)) } func properPlural(word string) string {