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

View File

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

View File

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

View File

@ -11,8 +11,6 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"github.com/bmizerany/pat"
) )
var reExtractFileID = regexp.MustCompile(`([^/]+)\/?$`) var reExtractFileID = regexp.MustCompile(`([^/]+)\/?$`)
@ -73,7 +71,6 @@ type Handler struct {
dataStore DataStore dataStore DataStore
isBasePathAbs bool isBasePathAbs bool
basePath string basePath string
routeHandler http.Handler
locks map[string]bool locks map[string]bool
logger *log.Logger logger *log.Logger
@ -105,8 +102,6 @@ func NewHandler(config Config) (*Handler, error) {
base = "/" + base base = "/" + base
} }
mux := pat.New()
handler := &Handler{ handler := &Handler{
config: config, config: config,
dataStore: config.DataStore, dataStore: config.DataStore,
@ -117,22 +112,9 @@ func NewHandler(config Config) (*Handler, error) {
logger: logger, 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 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 { func (handler *Handler) TusMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Allow overriding the HTTP method. The reason for this is // 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) { func TestHead(t *testing.T) {
handler, _ := NewHandler(Config{ handler, _ := NewRoutedHandler(Config{
BasePath: "https://buy.art/", BasePath: "https://buy.art/",
DataStore: headStore{}, DataStore: headStore{},
}) })

View File

@ -6,7 +6,7 @@ import (
) )
func TestOptions(t *testing.T) { func TestOptions(t *testing.T) {
handler, _ := NewHandler(Config{ handler, _ := NewRoutedHandler(Config{
MaxSize: 400, 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) { func TestPatch(t *testing.T) {
handler, _ := NewHandler(Config{ handler, _ := NewRoutedHandler(Config{
MaxSize: 100, MaxSize: 100,
DataStore: patchStore{ DataStore: patchStore{
t: t, t: t,
@ -178,7 +178,7 @@ func (r *noEOFReader) Write(src []byte) (int, error) {
} }
func TestPatchOverflow(t *testing.T) { func TestPatchOverflow(t *testing.T) {
handler, _ := NewHandler(Config{ handler, _ := NewRoutedHandler(Config{
MaxSize: 100, MaxSize: 100,
DataStore: overflowPatchStore{ DataStore: overflowPatchStore{
t: t, t: t,

View File

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

View File

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