Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Sep 20, 2014
0 parents commit 7e00309
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

# mount

Mount middleware. Pretty much just http.StripPrefix but still invokes the upstream.

View the [docs](http://godoc.org/github.com/gohttp/mount).

# License

MIT
20 changes: 20 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "github.com/gohttp/logger"
import "github.com/gohttp/serve"
import "github.com/gohttp/mount"
import "github.com/gohttp/app"
import "net/http"

func main() {
a := app.New()

a.Use(logger.New())
a.Use(mount.New("/examples", serve.New("examples")))

a.Get("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
}))

a.Listen(":3000")
}
24 changes: 24 additions & 0 deletions mount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mount

import "net/http"
import "strings"

// Middleware constructor.
type Constructor func(http.Handler) http.Handler

// New mount middleware.
func New(prefix string, mw Constructor) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
original := r.URL.Path

if s := strings.TrimPrefix(r.URL.Path, prefix); len(s) < len(r.URL.Path) {
r.URL.Path = s
mw(h).ServeHTTP(w, r)
r.URL.Path = original
} else {
h.ServeHTTP(w, r)
}
})
}
}

0 comments on commit 7e00309

Please sign in to comment.