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

4
.gitignore vendored
View File

@ -18,4 +18,6 @@ util/version.go
/public/package-lock.json
!.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)
next.ServeHTTP(w, r)
if (next != nil) {
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 {
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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}

View File

@ -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)
})
}

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 {
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 {
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 {

View File

@ -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)
}
})
}

View File

@ -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)
}
})
}