mirror of
https://github.com/dutchcoders/transfer.sh.git
synced 2024-11-27 14:40:18 +01:00
cb6e5cb0c7
* use dep for vendoring * lets encrypt * moved web to transfer.sh-web repo * single command install * added first tests
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
// ChainableHandler is a valid Handler interface, and adds the possibility to
|
|
// chain other handlers.
|
|
type ChainableHandler interface {
|
|
http.Handler
|
|
Chain(http.Handler) ChainableHandler
|
|
ChainFunc(http.HandlerFunc) ChainableHandler
|
|
}
|
|
|
|
// Default implementation of a simple ChainableHandler
|
|
type chainHandler struct {
|
|
http.Handler
|
|
}
|
|
|
|
func (this *chainHandler) ChainFunc(h http.HandlerFunc) ChainableHandler {
|
|
return this.Chain(h)
|
|
}
|
|
|
|
// Implementation of the ChainableHandler interface, calls the chained handler
|
|
// after the current one (sequential).
|
|
func (this *chainHandler) Chain(h http.Handler) ChainableHandler {
|
|
return &chainHandler{
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// Add the chained handler after the call to this handler
|
|
this.ServeHTTP(w, r)
|
|
h.ServeHTTP(w, r)
|
|
}),
|
|
}
|
|
}
|
|
|
|
// Convert a standard http handler to a chainable handler interface.
|
|
func NewChainableHandler(h http.Handler) ChainableHandler {
|
|
return &chainHandler{
|
|
h,
|
|
}
|
|
}
|
|
|
|
// Helper function to chain multiple handler functions in a single call.
|
|
func ChainHandlerFuncs(h ...http.HandlerFunc) ChainableHandler {
|
|
return &chainHandler{
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
for _, v := range h {
|
|
v(w, r)
|
|
}
|
|
}),
|
|
}
|
|
}
|
|
|
|
// Helper function to chain multiple handlers in a single call.
|
|
func ChainHandlers(h ...http.Handler) ChainableHandler {
|
|
return &chainHandler{
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
for _, v := range h {
|
|
v.ServeHTTP(w, r)
|
|
}
|
|
}),
|
|
}
|
|
}
|