From bb97b8d7571221f91ad8c422d9ed1d4d6a9c5207 Mon Sep 17 00:00:00 2001 From: Stanislav Simovski Date: Tue, 9 Jul 2019 18:55:42 +0300 Subject: [PATCH] Execute next middleware in stack only if present --- .gitignore | 4 +- api/auth.go | 4 +- api/events.go | 8 +- api/login.go | 8 +- api/projects/environment.go | 20 +++-- api/projects/inventory.go | 20 +++-- api/projects/keys.go | 20 +++-- api/projects/project.go | 20 +++-- api/projects/projects.go | 8 +- api/projects/repository.go | 20 +++-- api/projects/templates.go | 20 +++-- api/projects/users.go | 20 +++-- api/router.go | 154 ++++++++++++++++++++++-------------- api/sockets/handler.go | 4 +- api/tasks/http.go | 28 +++++-- api/user.go | 16 +++- api/users.go | 24 ++++-- mulekick/middleware.go | 27 +------ mulekick/router.go | 10 --- mulekick/router_test.go | 4 +- util/util.go | 4 +- util/util_test.go | 4 +- 22 files changed, 290 insertions(+), 157 deletions(-) diff --git a/.gitignore b/.gitignore index e0203585..cd79d263 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,6 @@ util/version.go /public/package-lock.json !.gitkeep -.dredd/compiled_hooks \ No newline at end of file +.dredd/compiled_hooks +.vscode +__debug_bin* diff --git a/api/auth.go b/api/auth.go index bab5946e..836db54e 100644 --- a/api/auth.go +++ b/api/auth.go @@ -84,6 +84,8 @@ func authentication(next http.Handler) http.Handler { } context.Set(r, "user", user) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/events.go b/api/events.go index dcd311c5..c3d24aff 100644 --- a/api/events.go +++ b/api/events.go @@ -76,13 +76,17 @@ func getEvents(w http.ResponseWriter, r *http.Request, limit uint64) { func getLastEvents(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { getEvents(w, r, 200) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } func getAllEvents(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { getEvents(w, r, 0) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/login.go b/api/login.go index 00772626..3f14c3a1 100644 --- a/api/login.go +++ b/api/login.go @@ -199,7 +199,9 @@ func login(next http.Handler) http.Handler { }) w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -213,6 +215,8 @@ func logout(next http.Handler) http.Handler { }) w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/projects/environment.go b/api/projects/environment.go index 1be68c39..1a72605b 100644 --- a/api/projects/environment.go +++ b/api/projects/environment.go @@ -40,7 +40,9 @@ func EnvironmentMiddleware(next http.Handler) http.Handler { context.Set(r, "environment", env) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -79,7 +81,9 @@ func GetEnvironment(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusOK, env) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -106,7 +110,9 @@ func UpdateEnvironment(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -150,7 +156,9 @@ func AddEnvironment(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -196,6 +204,8 @@ func RemoveEnvironment(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/projects/inventory.go b/api/projects/inventory.go index ef5b7743..7888b708 100644 --- a/api/projects/inventory.go +++ b/api/projects/inventory.go @@ -48,7 +48,9 @@ func InventoryMiddleware(next http.Handler) http.Handler { context.Set(r, "inventory", inventory) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -86,7 +88,9 @@ func GetInventory(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusOK, inv) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -146,7 +150,9 @@ func AddInventory(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusCreated, inv) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -217,7 +223,9 @@ func UpdateInventory(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -263,6 +271,8 @@ func RemoveInventory(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/projects/keys.go b/api/projects/keys.go index e85a3301..9668aeaf 100644 --- a/api/projects/keys.go +++ b/api/projects/keys.go @@ -32,7 +32,9 @@ func KeyMiddleware(next http.Handler) http.Handler { context.Set(r, "accessKey", key) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -79,7 +81,9 @@ func GetKeys(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusOK, keys) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -134,7 +138,9 @@ func AddKey(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -191,7 +197,9 @@ func UpdateKey(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -242,6 +250,8 @@ func RemoveKey(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/projects/project.go b/api/projects/project.go index 38dbe846..753141c5 100644 --- a/api/projects/project.go +++ b/api/projects/project.go @@ -40,7 +40,9 @@ func ProjectMiddleware(next http.Handler) http.Handler { } context.Set(r, "project", project) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -49,7 +51,9 @@ func GetProject(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { mulekick.WriteJSON(w, http.StatusOK, context.Get(r, "project")) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -69,7 +73,9 @@ func MustBeAdmin(next http.Handler) http.Handler { return } - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -93,7 +99,9 @@ func UpdateProject(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -134,6 +142,8 @@ func DeleteProject(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/projects/projects.go b/api/projects/projects.go index 2f8d63f3..aef32849 100644 --- a/api/projects/projects.go +++ b/api/projects/projects.go @@ -32,7 +32,9 @@ func GetProjects(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusOK, projects) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -69,6 +71,8 @@ func AddProject(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusCreated, body) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/projects/repository.go b/api/projects/repository.go index 3641ba95..cd9464d4 100644 --- a/api/projects/repository.go +++ b/api/projects/repository.go @@ -44,7 +44,9 @@ func RepositoryMiddleware(next http.Handler) http.Handler { context.Set(r, "repository", repository) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -91,7 +93,9 @@ func GetRepositories(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusOK, repos) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -131,7 +135,9 @@ func AddRepository(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -169,7 +175,9 @@ func UpdateRepository(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -217,6 +225,8 @@ func RemoveRepository(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/projects/templates.go b/api/projects/templates.go index 7ea4be0f..94dc8ffb 100644 --- a/api/projects/templates.go +++ b/api/projects/templates.go @@ -33,7 +33,9 @@ func TemplatesMiddleware(next http.Handler) http.Handler { context.Set(r, "template", template) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -96,7 +98,9 @@ func GetTemplates(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusOK, templates) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -135,7 +139,9 @@ func AddTemplate(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusCreated, template) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -170,7 +176,9 @@ func UpdateTemplate(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -193,6 +201,8 @@ func RemoveTemplate(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/projects/users.go b/api/projects/users.go index c1f80871..53921729 100644 --- a/api/projects/users.go +++ b/api/projects/users.go @@ -33,7 +33,9 @@ func UserMiddleware(next http.Handler) http.Handler { context.Set(r, "projectUser", user) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -75,7 +77,9 @@ func GetUsers(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusOK, users) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -110,7 +114,9 @@ func AddUser(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -137,7 +143,9 @@ func RemoveUser(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -159,6 +167,8 @@ func MakeUserAdmin(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/router.go b/api/router.go index 9c59e835..8d085cc2 100644 --- a/api/router.go +++ b/api/router.go @@ -1,6 +1,7 @@ package api import ( + "fmt" "net/http" "strings" @@ -20,7 +21,9 @@ var publicAssets = packr.NewBox("../web/public") func JSONMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "application/json") - next.ServeHTTP(w, r) + if next != nil { + next.ServeHTTP(w, r) + } }) } @@ -28,30 +31,59 @@ func JSONMiddleware(next http.Handler) http.Handler { func PlainTextMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "text/plain; charset=utf-8") - next.ServeHTTP(w, r) + if next != nil { + next.ServeHTTP(w, r) + } }) } +func printRegisteredRoutes(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error { + pathTemplate, err := route.GetPathTemplate() + if err == nil && len(pathTemplate) > 0 { + fmt.Println("ROUTE:", pathTemplate) + } + pathRegexp, err := route.GetPathRegexp() + if err == nil && len(pathRegexp) > 0 { + fmt.Println("Path regexp:", pathRegexp) + } + queriesTemplates, err := route.GetQueriesTemplates() + if err == nil && len(queriesTemplates) > 0 { + fmt.Println("Queries templates:", strings.Join(queriesTemplates, ",")) + } + queriesRegexps, err := route.GetQueriesRegexp() + if err == nil && len(queriesRegexps) > 0 { + fmt.Println("Queries regexps:", strings.Join(queriesRegexps, ",")) + } + methods, err := route.GetMethods() + if err == nil && len(methods) > 0 { + fmt.Println("Methods:", strings.Join(methods, ",")) + } + fmt.Println() + return nil +} + // Route declares all routes func Route() mulekick.Router { r := mulekick.New(mux.NewRouter()) - r.NotFoundHandler = servePublic(nil) - r.Use(mulekick.CorsMiddleware, JSONMiddleware) + + r.Use(mux.CORSMethodMiddleware(r.Router)) webPath := "/" if util.WebHostURL != nil { webPath = util.WebHostURL.RequestURI() } + r.NotFoundHandler = servePublic(nil) + r.Handle(webPath, servePublic(nil)) + + r.Use(JSONMiddleware) + r.Get(webPath+"api/ping", PlainTextMiddleware, mulekick.PongHandler) // set up the namespace - api := r.Group(webPath + "api") - - func(api mulekick.Router) { - api.Post("/login", login) - api.Post("/logout", logout) - }(api.Group("/auth")) + api := mulekick.New(r.Path(webPath + "api").Subrouter()) + api.Post("/login", login) + api.Post("/logout", logout) api.Use(authentication) @@ -61,14 +93,12 @@ func Route() mulekick.Router { api.Get("/upgrade", checkUpgrade) api.Post("/upgrade", doUpgrade) - func(api mulekick.Router) { - api.Get("", getUser) - // api.PUT("/user", misc.UpdateUser) + api.Get("", getUser) + // api.PUT("/user", misc.UpdateUser) - api.Get("/tokens", getAPITokens) - api.Post("/tokens", createAPIToken) - api.Delete("/tokens/{token_id}", expireAPIToken) - }(api.Group("/user")) + api.Get("/tokens", getAPITokens) + api.Post("/tokens", createAPIToken) + api.Delete("/tokens/{token_id}", expireAPIToken) api.Get("/projects", projects.GetProjects) api.Post("/projects", projects.AddProject) @@ -82,54 +112,54 @@ func Route() mulekick.Router { api.Post("/users/{user_id}/password", getUserMiddleware, updateUserPassword) api.Delete("/users/{user_id}", getUserMiddleware, deleteUser) - func(api mulekick.Router) { - api.Use(projects.ProjectMiddleware) + project := mulekick.New(api.Path("/project/{project_id}").Subrouter()) - api.Get("", projects.GetProject) - api.Put("", projects.MustBeAdmin, projects.UpdateProject) - api.Delete("", projects.MustBeAdmin, projects.DeleteProject) + project.Use(projects.ProjectMiddleware) - api.Get("/events", getAllEvents) - api.Get("/events/last", getLastEvents) + project.Get("", projects.GetProject) + project.Put("", projects.MustBeAdmin, projects.UpdateProject) + project.Delete("", projects.MustBeAdmin, projects.DeleteProject) - api.Get("/users", projects.GetUsers) - api.Post("/users", projects.MustBeAdmin, projects.AddUser) - api.Post("/users/{user_id}/admin", projects.MustBeAdmin, projects.UserMiddleware, projects.MakeUserAdmin) - api.Delete("/users/{user_id}/admin", projects.MustBeAdmin, projects.UserMiddleware, projects.MakeUserAdmin) - api.Delete("/users/{user_id}", projects.MustBeAdmin, projects.UserMiddleware, projects.RemoveUser) + project.Get("/events", getAllEvents) + project.Get("/events/last", getLastEvents) - api.Get("/keys", projects.GetKeys) - api.Post("/keys", projects.AddKey) - api.Put("/keys/{key_id}", projects.KeyMiddleware, projects.UpdateKey) - api.Delete("/keys/{key_id}", projects.KeyMiddleware, projects.RemoveKey) + project.Get("/users", projects.GetUsers) + project.Post("/users", projects.MustBeAdmin, projects.AddUser) + project.Post("/users/{user_id}/admin", projects.MustBeAdmin, projects.UserMiddleware, projects.MakeUserAdmin) + project.Delete("/users/{user_id}/admin", projects.MustBeAdmin, projects.UserMiddleware, projects.MakeUserAdmin) + project.Delete("/users/{user_id}", projects.MustBeAdmin, projects.UserMiddleware, projects.RemoveUser) - api.Get("/repositories", projects.GetRepositories) - api.Post("/repositories", projects.AddRepository) - api.Put("/repositories/{repository_id}", projects.RepositoryMiddleware, projects.UpdateRepository) - api.Delete("/repositories/{repository_id}", projects.RepositoryMiddleware, projects.RemoveRepository) + project.Get("/keys", projects.GetKeys) + project.Post("/keys", projects.AddKey) + project.Put("/keys/{key_id}", projects.KeyMiddleware, projects.UpdateKey) + project.Delete("/keys/{key_id}", projects.KeyMiddleware, projects.RemoveKey) - api.Get("/inventory", projects.GetInventory) - api.Post("/inventory", projects.AddInventory) - api.Put("/inventory/{inventory_id}", projects.InventoryMiddleware, projects.UpdateInventory) - api.Delete("/inventory/{inventory_id}", projects.InventoryMiddleware, projects.RemoveInventory) + project.Get("/repositories", projects.GetRepositories) + project.Post("/repositories", projects.AddRepository) + project.Put("/repositories/{repository_id}", projects.RepositoryMiddleware, projects.UpdateRepository) + project.Delete("/repositories/{repository_id}", projects.RepositoryMiddleware, projects.RemoveRepository) - api.Get("/environment", projects.GetEnvironment) - api.Post("/environment", projects.AddEnvironment) - api.Put("/environment/{environment_id}", projects.EnvironmentMiddleware, projects.UpdateEnvironment) - api.Delete("/environment/{environment_id}", projects.EnvironmentMiddleware, projects.RemoveEnvironment) + project.Get("/inventory", projects.GetInventory) + project.Post("/inventory", projects.AddInventory) + project.Put("/inventory/{inventory_id}", projects.InventoryMiddleware, projects.UpdateInventory) + project.Delete("/inventory/{inventory_id}", projects.InventoryMiddleware, projects.RemoveInventory) - api.Get("/templates", projects.GetTemplates) - api.Post("/templates", projects.AddTemplate) - api.Put("/templates/{template_id}", projects.TemplatesMiddleware, projects.UpdateTemplate) - api.Delete("/templates/{template_id}", projects.TemplatesMiddleware, projects.RemoveTemplate) + project.Get("/environment", projects.GetEnvironment) + project.Post("/environment", projects.AddEnvironment) + project.Put("/environment/{environment_id}", projects.EnvironmentMiddleware, projects.UpdateEnvironment) + project.Delete("/environment/{environment_id}", projects.EnvironmentMiddleware, projects.RemoveEnvironment) - api.Get("/tasks", tasks.GetAllTasks) - api.Get("/tasks/last", tasks.GetLastTasks) - api.Post("/tasks", tasks.AddTask) - api.Get("/tasks/{task_id}/output", tasks.GetTaskMiddleware, tasks.GetTaskOutput) - api.Get("/tasks/{task_id}", tasks.GetTaskMiddleware, tasks.GetTask) - api.Delete("/tasks/{task_id}", tasks.GetTaskMiddleware, tasks.RemoveTask) - }(api.Group("/project/{project_id}")) + project.Get("/templates", projects.GetTemplates) + project.Post("/templates", projects.AddTemplate) + project.Put("/templates/{template_id}", projects.TemplatesMiddleware, projects.UpdateTemplate) + project.Delete("/templates/{template_id}", projects.TemplatesMiddleware, projects.RemoveTemplate) + + project.Get("/tasks", tasks.GetAllTasks) + project.Get("/tasks/last", tasks.GetLastTasks) + project.Post("/tasks", tasks.AddTask) + project.Get("/tasks/{task_id}/output", tasks.GetTaskMiddleware, tasks.GetTaskOutput) + project.Get("/tasks/{task_id}", tasks.GetTaskMiddleware, tasks.GetTask) + project.Delete("/tasks/{task_id}", tasks.GetTaskMiddleware, tasks.RemoveTask) return r } @@ -222,7 +252,9 @@ func getSystemInfo(next http.Handler) http.Handler { } mulekick.WriteJSON(w, http.StatusOK, body) - next.ServeHTTP(w, r) + if next != nil { + next.ServeHTTP(w, r) + } }) } @@ -239,7 +271,9 @@ func checkUpgrade(next http.Handler) http.Handler { } w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if next != nil { + next.ServeHTTP(w, r) + } }) } @@ -247,6 +281,8 @@ func doUpgrade(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { util.LogError(util.DoUpgrade(util.Version)) - next.ServeHTTP(w, r) + if next != nil { + next.ServeHTTP(w, r) + } }) } diff --git a/api/sockets/handler.go b/api/sockets/handler.go index fc393b02..8933e944 100644 --- a/api/sockets/handler.go +++ b/api/sockets/handler.go @@ -121,7 +121,9 @@ func Handler(next http.Handler) http.Handler { go c.writePump() c.readPump() - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/tasks/http.go b/api/tasks/http.go index af0418c1..8a4359f0 100644 --- a/api/tasks/http.go +++ b/api/tasks/http.go @@ -52,7 +52,9 @@ func AddTask(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusCreated, taskObj) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -94,7 +96,9 @@ func GetAllTasks(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { GetTasksList(w, r, 0) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -103,7 +107,9 @@ func GetLastTasks(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { GetTasksList(w, r, 200) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -113,7 +119,9 @@ func GetTask(next http.Handler) http.Handler { task := context.Get(r, taskTypeID).(db.Task) mulekick.WriteJSON(w, http.StatusOK, task) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -132,7 +140,9 @@ func GetTaskMiddleware(next http.Handler) http.Handler { context.Set(r, taskTypeID, task) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -150,7 +160,9 @@ func GetTaskOutput(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusOK, output) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -182,6 +194,8 @@ func RemoveTask(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/user.go b/api/user.go index 5e811940..80cb3f25 100644 --- a/api/user.go +++ b/api/user.go @@ -23,7 +23,9 @@ func getUser(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusOK, context.Get(r, "user")) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -38,7 +40,9 @@ func getAPITokens(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusOK, tokens) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -63,7 +67,9 @@ func createAPIToken(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusCreated, token) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -89,6 +95,8 @@ func expireAPIToken(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/api/users.go b/api/users.go index 96ffbaf2..004c804f 100644 --- a/api/users.go +++ b/api/users.go @@ -22,7 +22,9 @@ func getUsers(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusOK, users) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -49,7 +51,9 @@ func addUser(next http.Handler) http.Handler { mulekick.WriteJSON(w, http.StatusCreated, user) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -79,7 +83,9 @@ func getUserMiddleware(next http.Handler) http.Handler { context.Set(r, "_user", user) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -117,7 +123,9 @@ func updateUser(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -154,7 +162,9 @@ func updateUserPassword(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } @@ -178,6 +188,8 @@ func deleteUser(next http.Handler) http.Handler { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/mulekick/middleware.go b/mulekick/middleware.go index 76d26566..eecabcd3 100644 --- a/mulekick/middleware.go +++ b/mulekick/middleware.go @@ -7,17 +7,14 @@ import ( func PongHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("pong")) - next.ServeHTTP(w, r) + if next != nil { + next.ServeHTTP(w, r) + } }) } func NotFoundHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method == "OPTIONS" { - CorsMiddleware(next).ServeHTTP(w, r) - return - } - w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin")) w.Header().Set("Access-Control-Allow-Credentials", "true") @@ -25,21 +22,3 @@ func NotFoundHandler(next http.Handler) http.Handler { w.Write([]byte("404 not found")) }) } - -func CorsMiddleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin")) - w.Header().Set("Access-Control-Allow-Credentials", "true") - - if r.Method != "OPTIONS" { - return - } - - w.Header().Set("Access-Control-Max-Age", "1728000") - w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS") - w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") - - w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) - }) -} diff --git a/mulekick/router.go b/mulekick/router.go index 296e9ea8..ca2cd6ac 100644 --- a/mulekick/router.go +++ b/mulekick/router.go @@ -38,13 +38,3 @@ func (r Router) Patch(endpoint string, middleware ...mux.MiddlewareFunc) *mux.Ro func (r Router) Options(endpoint string, middleware ...mux.MiddlewareFunc) *mux.Route { return r.makeRouteWithMiddleware(endpoint, middleware).Methods("OPTIONS") } - -// Group creates a new sub-router, enabling you to group handlers -func (r Router) Group(str string, middleware ...mux.MiddlewareFunc) Router { - subR := r.PathPrefix(str).Subrouter() - for _, md := range middleware { - subR.Use(md) - } - - return Router{subR} -} diff --git a/mulekick/router_test.go b/mulekick/router_test.go index a56730d6..5dc867d8 100644 --- a/mulekick/router_test.go +++ b/mulekick/router_test.go @@ -8,7 +8,9 @@ func ExampleRouter_Use() { r.Get("/hello", func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) }) r.Use(func(next http.Handler) http.Handler { diff --git a/util/util.go b/util/util.go index 4f16f4a8..125f55fe 100644 --- a/util/util.go +++ b/util/util.go @@ -25,7 +25,9 @@ func AuthFailed(next http.Handler) http.Handler { w.WriteHeader(http.StatusUnauthorized) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) } diff --git a/util/util_test.go b/util/util_test.go index f4ff0474..a2c03fbb 100644 --- a/util/util_test.go +++ b/util/util_test.go @@ -30,6 +30,8 @@ func mockParam(next http.Handler) http.Handler { w.WriteHeader(200) - next.ServeHTTP(w, r) + if (next != nil) { + next.ServeHTTP(w, r) + } }) }