tusd/pkg/handler/handler.go

56 lines
1.5 KiB
Go
Raw Normal View History

package handler
import (
"net/http"
"github.com/bmizerany/pat"
)
// Handler is a ready to use handler with routing (using pat)
type Handler struct {
2016-03-12 21:01:12 +00:00
*UnroutedHandler
http.Handler
}
// NewHandler creates a routed tus protocol handler. This is the simplest
// way to use tusd but may not be as configurable as you require. If you are
// integrating this into an existing app you may like to use tusd.NewUnroutedHandler
2015-12-07 20:09:47 +00:00
// instead. Using tusd.NewUnroutedHandler allows the tus handlers to be combined into
// your existing router (aka mux) directly. It also allows the GET and DELETE
// endpoints to be customized. These are not part of the protocol so can be
// changed depending on your needs.
func NewHandler(config Config) (*Handler, error) {
2016-02-21 22:25:35 +00:00
if err := config.validate(); err != nil {
return nil, err
}
handler, err := NewUnroutedHandler(config)
if err != nil {
return nil, err
}
routedHandler := &Handler{
2016-03-12 21:01:12 +00:00
UnroutedHandler: handler,
}
mux := pat.New()
2016-03-12 21:01:12 +00:00
routedHandler.Handler = handler.Middleware(mux)
2015-02-17 13:19:56 +00:00
mux.Post("", http.HandlerFunc(handler.PostFile))
mux.Head(":id", http.HandlerFunc(handler.HeadFile))
mux.Add("PATCH", ":id", http.HandlerFunc(handler.PatchFile))
// Only attach the DELETE handler if the Terminate() method is provided
2016-02-21 22:25:35 +00:00
if config.StoreComposer.UsesTerminater {
mux.Del(":id", http.HandlerFunc(handler.DelFile))
}
2016-01-19 21:32:15 +00:00
// GET handler requires the GetReader() method
2016-02-21 22:25:35 +00:00
if config.StoreComposer.UsesGetReader {
2016-01-19 21:32:15 +00:00
mux.Get(":id", http.HandlerFunc(handler.GetFile))
}
return routedHandler, nil
}