Execute next middleware in stack only if present

This commit is contained in:
Stanislav Simovski 2019-07-09 18:55:42 +03:00
parent 23d65ba0c2
commit bb97b8d757
22 changed files with 290 additions and 157 deletions

2
.gitignore vendored
View File

@ -19,3 +19,5 @@ util/version.go
!.gitkeep !.gitkeep
.dredd/compiled_hooks .dredd/compiled_hooks
.vscode
__debug_bin*

View File

@ -84,6 +84,8 @@ func authentication(next http.Handler) http.Handler {
} }
context.Set(r, "user", user) context.Set(r, "user", user)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -76,13 +76,17 @@ func getEvents(w http.ResponseWriter, r *http.Request, limit uint64) {
func getLastEvents(next http.Handler) http.Handler { func getLastEvents(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
getEvents(w, r, 200) getEvents(w, r, 200)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
func getAllEvents(next http.Handler) http.Handler { func getAllEvents(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
getEvents(w, r, 0) getEvents(w, r, 0)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -199,7 +199,9 @@ func login(next http.Handler) http.Handler {
}) })
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -213,6 +215,8 @@ func logout(next http.Handler) http.Handler {
}) })
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -40,7 +40,9 @@ func EnvironmentMiddleware(next http.Handler) http.Handler {
context.Set(r, "environment", env) context.Set(r, "environment", env)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -79,7 +81,9 @@ func GetEnvironment(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusOK, env) mulekick.WriteJSON(w, http.StatusOK, env)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -106,7 +110,9 @@ func UpdateEnvironment(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -150,7 +156,9 @@ func AddEnvironment(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -196,6 +204,8 @@ func RemoveEnvironment(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -48,7 +48,9 @@ func InventoryMiddleware(next http.Handler) http.Handler {
context.Set(r, "inventory", inventory) context.Set(r, "inventory", inventory)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -86,7 +88,9 @@ func GetInventory(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusOK, inv) mulekick.WriteJSON(w, http.StatusOK, inv)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -146,7 +150,9 @@ func AddInventory(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusCreated, inv) mulekick.WriteJSON(w, http.StatusCreated, inv)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -217,7 +223,9 @@ func UpdateInventory(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -263,6 +271,8 @@ func RemoveInventory(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -32,7 +32,9 @@ func KeyMiddleware(next http.Handler) http.Handler {
context.Set(r, "accessKey", key) context.Set(r, "accessKey", key)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -79,7 +81,9 @@ func GetKeys(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusOK, keys) mulekick.WriteJSON(w, http.StatusOK, keys)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -134,7 +138,9 @@ func AddKey(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -191,7 +197,9 @@ func UpdateKey(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -242,6 +250,8 @@ func RemoveKey(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -40,7 +40,9 @@ func ProjectMiddleware(next http.Handler) http.Handler {
} }
context.Set(r, "project", project) context.Set(r, "project", project)
if (next != nil) {
next.ServeHTTP(w, r) 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mulekick.WriteJSON(w, http.StatusOK, context.Get(r, "project")) mulekick.WriteJSON(w, http.StatusOK, context.Get(r, "project"))
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -69,7 +73,9 @@ func MustBeAdmin(next http.Handler) http.Handler {
return return
} }
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -93,7 +99,9 @@ func UpdateProject(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -134,6 +142,8 @@ func DeleteProject(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -32,7 +32,9 @@ func GetProjects(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusOK, projects) mulekick.WriteJSON(w, http.StatusOK, projects)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -69,6 +71,8 @@ func AddProject(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusCreated, body) mulekick.WriteJSON(w, http.StatusCreated, body)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -44,7 +44,9 @@ func RepositoryMiddleware(next http.Handler) http.Handler {
context.Set(r, "repository", repository) context.Set(r, "repository", repository)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -91,7 +93,9 @@ func GetRepositories(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusOK, repos) mulekick.WriteJSON(w, http.StatusOK, repos)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -131,7 +135,9 @@ func AddRepository(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -169,7 +175,9 @@ func UpdateRepository(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -217,6 +225,8 @@ func RemoveRepository(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -33,7 +33,9 @@ func TemplatesMiddleware(next http.Handler) http.Handler {
context.Set(r, "template", template) context.Set(r, "template", template)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -96,7 +98,9 @@ func GetTemplates(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusOK, templates) mulekick.WriteJSON(w, http.StatusOK, templates)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -135,7 +139,9 @@ func AddTemplate(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusCreated, template) mulekick.WriteJSON(w, http.StatusCreated, template)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -170,7 +176,9 @@ func UpdateTemplate(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -193,6 +201,8 @@ func RemoveTemplate(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -33,7 +33,9 @@ func UserMiddleware(next http.Handler) http.Handler {
context.Set(r, "projectUser", user) context.Set(r, "projectUser", user)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -75,7 +77,9 @@ func GetUsers(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusOK, users) mulekick.WriteJSON(w, http.StatusOK, users)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -110,7 +114,9 @@ func AddUser(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -137,7 +143,9 @@ func RemoveUser(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -159,6 +167,8 @@ func MakeUserAdmin(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -1,6 +1,7 @@
package api package api
import ( import (
"fmt"
"net/http" "net/http"
"strings" "strings"
@ -20,7 +21,9 @@ var publicAssets = packr.NewBox("../web/public")
func JSONMiddleware(next http.Handler) http.Handler { func JSONMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json") w.Header().Set("content-type", "application/json")
if next != nil {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -28,30 +31,59 @@ func JSONMiddleware(next http.Handler) http.Handler {
func PlainTextMiddleware(next http.Handler) http.Handler { func PlainTextMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/plain; charset=utf-8") w.Header().Set("content-type", "text/plain; charset=utf-8")
if next != nil {
next.ServeHTTP(w, r) 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 // Route declares all routes
func Route() mulekick.Router { func Route() mulekick.Router {
r := mulekick.New(mux.NewRouter()) r := mulekick.New(mux.NewRouter())
r.NotFoundHandler = servePublic(nil)
r.Use(mulekick.CorsMiddleware, JSONMiddleware) r.Use(mux.CORSMethodMiddleware(r.Router))
webPath := "/" webPath := "/"
if util.WebHostURL != nil { if util.WebHostURL != nil {
webPath = util.WebHostURL.RequestURI() webPath = util.WebHostURL.RequestURI()
} }
r.NotFoundHandler = servePublic(nil)
r.Handle(webPath, servePublic(nil))
r.Use(JSONMiddleware)
r.Get(webPath+"api/ping", PlainTextMiddleware, mulekick.PongHandler) r.Get(webPath+"api/ping", PlainTextMiddleware, mulekick.PongHandler)
// set up the namespace // set up the namespace
api := r.Group(webPath + "api") api := mulekick.New(r.Path(webPath + "api").Subrouter())
func(api mulekick.Router) {
api.Post("/login", login) api.Post("/login", login)
api.Post("/logout", logout) api.Post("/logout", logout)
}(api.Group("/auth"))
api.Use(authentication) api.Use(authentication)
@ -61,14 +93,12 @@ func Route() mulekick.Router {
api.Get("/upgrade", checkUpgrade) api.Get("/upgrade", checkUpgrade)
api.Post("/upgrade", doUpgrade) api.Post("/upgrade", doUpgrade)
func(api mulekick.Router) {
api.Get("", getUser) api.Get("", getUser)
// api.PUT("/user", misc.UpdateUser) // api.PUT("/user", misc.UpdateUser)
api.Get("/tokens", getAPITokens) api.Get("/tokens", getAPITokens)
api.Post("/tokens", createAPIToken) api.Post("/tokens", createAPIToken)
api.Delete("/tokens/{token_id}", expireAPIToken) api.Delete("/tokens/{token_id}", expireAPIToken)
}(api.Group("/user"))
api.Get("/projects", projects.GetProjects) api.Get("/projects", projects.GetProjects)
api.Post("/projects", projects.AddProject) api.Post("/projects", projects.AddProject)
@ -82,54 +112,54 @@ func Route() mulekick.Router {
api.Post("/users/{user_id}/password", getUserMiddleware, updateUserPassword) api.Post("/users/{user_id}/password", getUserMiddleware, updateUserPassword)
api.Delete("/users/{user_id}", getUserMiddleware, deleteUser) api.Delete("/users/{user_id}", getUserMiddleware, deleteUser)
func(api mulekick.Router) { project := mulekick.New(api.Path("/project/{project_id}").Subrouter())
api.Use(projects.ProjectMiddleware)
api.Get("", projects.GetProject) project.Use(projects.ProjectMiddleware)
api.Put("", projects.MustBeAdmin, projects.UpdateProject)
api.Delete("", projects.MustBeAdmin, projects.DeleteProject)
api.Get("/events", getAllEvents) project.Get("", projects.GetProject)
api.Get("/events/last", getLastEvents) project.Put("", projects.MustBeAdmin, projects.UpdateProject)
project.Delete("", projects.MustBeAdmin, projects.DeleteProject)
api.Get("/users", projects.GetUsers) project.Get("/events", getAllEvents)
api.Post("/users", projects.MustBeAdmin, projects.AddUser) project.Get("/events/last", getLastEvents)
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)
api.Get("/keys", projects.GetKeys) project.Get("/users", projects.GetUsers)
api.Post("/keys", projects.AddKey) project.Post("/users", projects.MustBeAdmin, projects.AddUser)
api.Put("/keys/{key_id}", projects.KeyMiddleware, projects.UpdateKey) project.Post("/users/{user_id}/admin", projects.MustBeAdmin, projects.UserMiddleware, projects.MakeUserAdmin)
api.Delete("/keys/{key_id}", projects.KeyMiddleware, projects.RemoveKey) 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) project.Get("/keys", projects.GetKeys)
api.Post("/repositories", projects.AddRepository) project.Post("/keys", projects.AddKey)
api.Put("/repositories/{repository_id}", projects.RepositoryMiddleware, projects.UpdateRepository) project.Put("/keys/{key_id}", projects.KeyMiddleware, projects.UpdateKey)
api.Delete("/repositories/{repository_id}", projects.RepositoryMiddleware, projects.RemoveRepository) project.Delete("/keys/{key_id}", projects.KeyMiddleware, projects.RemoveKey)
api.Get("/inventory", projects.GetInventory) project.Get("/repositories", projects.GetRepositories)
api.Post("/inventory", projects.AddInventory) project.Post("/repositories", projects.AddRepository)
api.Put("/inventory/{inventory_id}", projects.InventoryMiddleware, projects.UpdateInventory) project.Put("/repositories/{repository_id}", projects.RepositoryMiddleware, projects.UpdateRepository)
api.Delete("/inventory/{inventory_id}", projects.InventoryMiddleware, projects.RemoveInventory) project.Delete("/repositories/{repository_id}", projects.RepositoryMiddleware, projects.RemoveRepository)
api.Get("/environment", projects.GetEnvironment) project.Get("/inventory", projects.GetInventory)
api.Post("/environment", projects.AddEnvironment) project.Post("/inventory", projects.AddInventory)
api.Put("/environment/{environment_id}", projects.EnvironmentMiddleware, projects.UpdateEnvironment) project.Put("/inventory/{inventory_id}", projects.InventoryMiddleware, projects.UpdateInventory)
api.Delete("/environment/{environment_id}", projects.EnvironmentMiddleware, projects.RemoveEnvironment) project.Delete("/inventory/{inventory_id}", projects.InventoryMiddleware, projects.RemoveInventory)
api.Get("/templates", projects.GetTemplates) project.Get("/environment", projects.GetEnvironment)
api.Post("/templates", projects.AddTemplate) project.Post("/environment", projects.AddEnvironment)
api.Put("/templates/{template_id}", projects.TemplatesMiddleware, projects.UpdateTemplate) project.Put("/environment/{environment_id}", projects.EnvironmentMiddleware, projects.UpdateEnvironment)
api.Delete("/templates/{template_id}", projects.TemplatesMiddleware, projects.RemoveTemplate) project.Delete("/environment/{environment_id}", projects.EnvironmentMiddleware, projects.RemoveEnvironment)
api.Get("/tasks", tasks.GetAllTasks) project.Get("/templates", projects.GetTemplates)
api.Get("/tasks/last", tasks.GetLastTasks) project.Post("/templates", projects.AddTemplate)
api.Post("/tasks", tasks.AddTask) project.Put("/templates/{template_id}", projects.TemplatesMiddleware, projects.UpdateTemplate)
api.Get("/tasks/{task_id}/output", tasks.GetTaskMiddleware, tasks.GetTaskOutput) project.Delete("/templates/{template_id}", projects.TemplatesMiddleware, projects.RemoveTemplate)
api.Get("/tasks/{task_id}", tasks.GetTaskMiddleware, tasks.GetTask)
api.Delete("/tasks/{task_id}", tasks.GetTaskMiddleware, tasks.RemoveTask) project.Get("/tasks", tasks.GetAllTasks)
}(api.Group("/project/{project_id}")) 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 return r
} }
@ -222,7 +252,9 @@ func getSystemInfo(next http.Handler) http.Handler {
} }
mulekick.WriteJSON(w, http.StatusOK, body) mulekick.WriteJSON(w, http.StatusOK, body)
if next != nil {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -239,7 +271,9 @@ func checkUpgrade(next http.Handler) http.Handler {
} }
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if next != nil {
next.ServeHTTP(w, r) 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
util.LogError(util.DoUpgrade(util.Version)) util.LogError(util.DoUpgrade(util.Version))
if next != nil {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -121,7 +121,9 @@ func Handler(next http.Handler) http.Handler {
go c.writePump() go c.writePump()
c.readPump() c.readPump()
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -52,7 +52,9 @@ func AddTask(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusCreated, taskObj) mulekick.WriteJSON(w, http.StatusCreated, taskObj)
if (next != nil) {
next.ServeHTTP(w, r) 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
GetTasksList(w, r, 0) GetTasksList(w, r, 0)
if (next != nil) {
next.ServeHTTP(w, r) 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
GetTasksList(w, r, 200) GetTasksList(w, r, 200)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -113,7 +119,9 @@ func GetTask(next http.Handler) http.Handler {
task := context.Get(r, taskTypeID).(db.Task) task := context.Get(r, taskTypeID).(db.Task)
mulekick.WriteJSON(w, http.StatusOK, task) mulekick.WriteJSON(w, http.StatusOK, task)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -132,7 +140,9 @@ func GetTaskMiddleware(next http.Handler) http.Handler {
context.Set(r, taskTypeID, task) context.Set(r, taskTypeID, task)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -150,7 +160,9 @@ func GetTaskOutput(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusOK, output) mulekick.WriteJSON(w, http.StatusOK, output)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -182,6 +194,8 @@ func RemoveTask(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -23,7 +23,9 @@ func getUser(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusOK, context.Get(r, "user")) mulekick.WriteJSON(w, http.StatusOK, context.Get(r, "user"))
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -38,7 +40,9 @@ func getAPITokens(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusOK, tokens) mulekick.WriteJSON(w, http.StatusOK, tokens)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -63,7 +67,9 @@ func createAPIToken(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusCreated, token) mulekick.WriteJSON(w, http.StatusCreated, token)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -89,6 +95,8 @@ func expireAPIToken(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -22,7 +22,9 @@ func getUsers(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusOK, users) mulekick.WriteJSON(w, http.StatusOK, users)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -49,7 +51,9 @@ func addUser(next http.Handler) http.Handler {
mulekick.WriteJSON(w, http.StatusCreated, user) mulekick.WriteJSON(w, http.StatusCreated, user)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -79,7 +83,9 @@ func getUserMiddleware(next http.Handler) http.Handler {
context.Set(r, "_user", user) context.Set(r, "_user", user)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -117,7 +123,9 @@ func updateUser(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -154,7 +162,9 @@ func updateUserPassword(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
@ -178,6 +188,8 @@ func deleteUser(next http.Handler) http.Handler {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -7,17 +7,14 @@ import (
func PongHandler(next http.Handler) http.Handler { func PongHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong")) w.Write([]byte("pong"))
if next != nil {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }
func NotFoundHandler(next http.Handler) http.Handler { func NotFoundHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true") 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")) 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)
})
}

View File

@ -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 { func (r Router) Options(endpoint string, middleware ...mux.MiddlewareFunc) *mux.Route {
return r.makeRouteWithMiddleware(endpoint, middleware).Methods("OPTIONS") 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}
}

View File

@ -8,7 +8,9 @@ func ExampleRouter_Use() {
r.Get("/hello", func(next http.Handler) http.Handler { r.Get("/hello", func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
}) })
r.Use(func(next http.Handler) http.Handler { r.Use(func(next http.Handler) http.Handler {

View File

@ -25,7 +25,9 @@ func AuthFailed(next http.Handler) http.Handler {
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }

View File

@ -30,6 +30,8 @@ func mockParam(next http.Handler) http.Handler {
w.WriteHeader(200) w.WriteHeader(200)
if (next != nil) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}
}) })
} }