Skip to content

Instantly share code, notes, and snippets.

@rossedman
Last active May 8, 2020 21:38
Show Gist options
  • Save rossedman/179b976abde3fbb2db2b718b469ca29f to your computer and use it in GitHub Desktop.
Save rossedman/179b976abde3fbb2db2b718b469ca29f to your computer and use it in GitHub Desktop.

Revisions

  1. rossedman revised this gist May 8, 2020. 1 changed file with 8 additions and 6 deletions.
    14 changes: 8 additions & 6 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,12 @@ func main() {
    }

    // mount will take routers from a handler and add it with a pathprefix
    func mount(r *mux.Router, path string, handler http.Handler, middleware ...mux.MiddlewareFunc) {
    s := r.PathPrefix(path).Subrouter()
    s.Handle("/", handler)
    if len(middleware) > 0 {
    s.Use(middleware...)
    }
    // mount will take routers from a handler and add it with a pathprefix
    func mount(r *mux.Router, path string, handler http.Handler, middleware mux.MiddlewareFunc) {
    r.PathPrefix(path).Handler(
    http.StripPrefix(
    strings.TrimSuffix(path, "/"),
    middleware(handler),
    ),
    )
    }
  2. rossedman revised this gist May 8, 2020. 1 changed file with 3 additions and 7 deletions.
    10 changes: 3 additions & 7 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -8,13 +8,9 @@ func main() {

    // mount will take routers from a handler and add it with a pathprefix
    func mount(r *mux.Router, path string, handler http.Handler, middleware ...mux.MiddlewareFunc) {
    r.PathPrefix(path).Handler(
    http.StripPrefix(
    strings.TrimSuffix(path, "/"),
    handler,
    ),
    )
    s := r.PathPrefix(path).Subrouter()
    s.Handle("/", handler)
    if len(middleware) > 0 {
    r.Use(middleware...)
    s.Use(middleware...)
    }
    }
  3. rossedman created this gist May 8, 2020.
    20 changes: 20 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    package main

    func main() {
    r := mux.NewRouter()
    mount(r, "/api/v1", handler.Router(), auth)
    ...
    }

    // mount will take routers from a handler and add it with a pathprefix
    func mount(r *mux.Router, path string, handler http.Handler, middleware ...mux.MiddlewareFunc) {
    r.PathPrefix(path).Handler(
    http.StripPrefix(
    strings.TrimSuffix(path, "/"),
    handler,
    ),
    )
    if len(middleware) > 0 {
    r.Use(middleware...)
    }
    }