Extract middleware into function

This commit is contained in:
Max Brosnahan 2015-11-04 15:15:23 -07:00
parent 376b73ae4b
commit 604d4e35a8
1 changed files with 61 additions and 54 deletions

View File

@ -112,12 +112,13 @@ func NewHandler(config Config) (*Handler, error) {
dataStore: config.DataStore,
basePath: base,
isBasePathAbs: uri.IsAbs(),
routeHandler: mux,
locks: make(map[string]bool),
CompleteUploads: make(chan FileInfo),
logger: logger,
}
handler.routeHandler = handler.TusMiddleware(mux)
mux.Post("", http.HandlerFunc(handler.postFile))
mux.Head(":id", http.HandlerFunc(handler.headFile))
mux.Get(":id", http.HandlerFunc(handler.getFile))
@ -129,6 +130,11 @@ func NewHandler(config Config) (*Handler, error) {
// Implement the http.Handler interface.
func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handler.routeHandler.ServeHTTP(w, r)
}
func (handler *Handler) TusMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Allow overriding the HTTP method. The reason for this is
// that some libraries/environments to not support PATCH and
// DELETE requests, e.g. Flash in a browser and parts of Java
@ -181,7 +187,8 @@ func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
// Proceed with routing the request
handler.routeHandler.ServeHTTP(w, r)
h.ServeHTTP(w, r)
})
}
// Create a new file upload using the datastore after validating the length