Extract handler with routing into separate struct

This commit is contained in:
Max Brosnahan 2015-11-04 16:46:22 -07:00
parent 604d4e35a8
commit a65da4944d
11 changed files with 54 additions and 30 deletions

View File

@ -37,7 +37,7 @@ func (s concatPartialStore) GetInfo(id string) (FileInfo, error) {
}
func TestConcatPartial(t *testing.T) {
handler, _ := NewHandler(Config{
handler, _ := NewRoutedHandler(Config{
MaxSize: 400,
BasePath: "files",
DataStore: concatPartialStore{
@ -150,7 +150,7 @@ func (s concatFinalStore) WriteChunk(id string, offset int64, src io.Reader) (in
}
func TestConcatFinal(t *testing.T) {
handler, _ := NewHandler(Config{
handler, _ := NewRoutedHandler(Config{
MaxSize: 400,
BasePath: "files",
DataStore: concatFinalStore{
@ -193,7 +193,7 @@ func TestConcatFinal(t *testing.T) {
Code: http.StatusBadRequest,
}).Run(handler, t)
handler, _ = NewHandler(Config{
handler, _ = NewRoutedHandler(Config{
MaxSize: 9,
BasePath: "files",
DataStore: concatFinalStore{

View File

@ -6,7 +6,7 @@ import (
)
func TestCORS(t *testing.T) {
handler, _ := NewHandler(Config{})
handler, _ := NewRoutedHandler(Config{})
(&httpTest{
Name: "Preflight request",

View File

@ -42,7 +42,7 @@ var reader = &closingStringReader{
}
func TestGet(t *testing.T) {
handler, _ := NewHandler(Config{
handler, _ := NewRoutedHandler(Config{
DataStore: getStore{},
})

View File

@ -11,8 +11,6 @@ import (
"regexp"
"strconv"
"strings"
"github.com/bmizerany/pat"
)
var reExtractFileID = regexp.MustCompile(`([^/]+)\/?$`)
@ -73,7 +71,6 @@ type Handler struct {
dataStore DataStore
isBasePathAbs bool
basePath string
routeHandler http.Handler
locks map[string]bool
logger *log.Logger
@ -105,8 +102,6 @@ func NewHandler(config Config) (*Handler, error) {
base = "/" + base
}
mux := pat.New()
handler := &Handler{
config: config,
dataStore: config.DataStore,
@ -117,22 +112,9 @@ func NewHandler(config Config) (*Handler, error) {
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))
mux.Del(":id", http.HandlerFunc(handler.delFile))
mux.Add("PATCH", ":id", http.HandlerFunc(handler.patchFile))
return handler, nil
}
// 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

View File

@ -26,7 +26,7 @@ func (s headStore) GetInfo(id string) (FileInfo, error) {
}
func TestHead(t *testing.T) {
handler, _ := NewHandler(Config{
handler, _ := NewRoutedHandler(Config{
BasePath: "https://buy.art/",
DataStore: headStore{},
})

View File

@ -6,7 +6,7 @@ import (
)
func TestOptions(t *testing.T) {
handler, _ := NewHandler(Config{
handler, _ := NewRoutedHandler(Config{
MaxSize: 400,
})

View File

@ -49,7 +49,7 @@ func (s patchStore) WriteChunk(id string, offset int64, src io.Reader) (int64, e
}
func TestPatch(t *testing.T) {
handler, _ := NewHandler(Config{
handler, _ := NewRoutedHandler(Config{
MaxSize: 100,
DataStore: patchStore{
t: t,
@ -178,7 +178,7 @@ func (r *noEOFReader) Write(src []byte) (int, error) {
}
func TestPatchOverflow(t *testing.T) {
handler, _ := NewHandler(Config{
handler, _ := NewRoutedHandler(Config{
MaxSize: 100,
DataStore: overflowPatchStore{
t: t,

View File

@ -32,7 +32,7 @@ func (s postStore) NewUpload(info FileInfo) (string, error) {
}
func TestPost(t *testing.T) {
handler, _ := NewHandler(Config{
handler, _ := NewRoutedHandler(Config{
MaxSize: 400,
BasePath: "files",
DataStore: postStore{

42
routed_handler.go Normal file
View File

@ -0,0 +1,42 @@
package tusd
import (
"net/http"
"github.com/bmizerany/pat"
)
type RoutedHandler struct {
handler *Handler
routeHandler http.Handler
CompleteUploads chan FileInfo
}
func NewRoutedHandler(config Config) (*RoutedHandler, error) {
handler, err := NewHandler(config)
if err != nil {
return nil, err
}
routedHandler := &RoutedHandler{
handler: handler,
CompleteUploads: handler.CompleteUploads,
}
mux := pat.New()
routedHandler.routeHandler = handler.TusMiddleware(mux)
mux.Post("", http.HandlerFunc(handler.postFile))
mux.Head(":id", http.HandlerFunc(handler.headFile))
mux.Get(":id", http.HandlerFunc(handler.getFile))
mux.Del(":id", http.HandlerFunc(handler.delFile))
mux.Add("PATCH", ":id", http.HandlerFunc(handler.patchFile))
return routedHandler, nil
}
// ServeHTTP Implements the http.Handler interface.
func (rHandler *RoutedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rHandler.routeHandler.ServeHTTP(w, r)
}

View File

@ -106,7 +106,7 @@ func TestMethodOverride(t *testing.T) {
store := &methodOverrideStore{
t: t,
}
handler, _ := NewHandler(Config{
handler, _ := NewRoutedHandler(Config{
DataStore: store,
})

View File

@ -18,7 +18,7 @@ func (s terminateStore) Terminate(id string) error {
}
func TestTerminate(t *testing.T) {
handler, _ := NewHandler(Config{
handler, _ := NewRoutedHandler(Config{
DataStore: terminateStore{
t: t,
},