diff --git a/cmd/auth-demo/main.go b/cmd/auth-demo/main.go index 238ec4e..d05c5b7 100644 --- a/cmd/auth-demo/main.go +++ b/cmd/auth-demo/main.go @@ -16,7 +16,7 @@ func main() { func StartServer() { auth.PrintConfig() - s := jch_http.NewServer(negroni.New()). + s := jch_http.NewServer(negroni.New(), jch_http.NewRouter()). Static("/public/*filepath", http.Dir("public/")). Service("", auth.Service(auth.FromEnv())). GET("/", "", http.HandlerFunc(HomeHandler)) diff --git a/cmd/sub-demo/main.go b/cmd/sub-demo/main.go index ac33afe..bdfe667 100644 --- a/cmd/sub-demo/main.go +++ b/cmd/sub-demo/main.go @@ -20,7 +20,7 @@ func StartServer() { payments.PrintConfig() auth_service := auth.Service(auth.FromEnv()) - s := jch_http.NewServer(negroni.New()). + s := jch_http.NewServer(negroni.New(), jch_http.NewRouter()). Static("/public/*filepath", http.Dir("public/")). Service("", auth_service). Service("", payments.Service(payments.FromEnv(), &auth_service)). diff --git a/http/julienschmidt_router.go b/http/julienschmidt_router.go new file mode 100644 index 0000000..076bb90 --- /dev/null +++ b/http/julienschmidt_router.go @@ -0,0 +1,36 @@ +package http + +import ( + "net/http" + "net/url" + + "github.com/julienschmidt/httprouter" +) + +type JulienschmidtHTTPRouter struct { + httprouter.Router +} + +func NewJulienschmidtHTTPRouter() *JulienschmidtHTTPRouter { + return &JulienschmidtHTTPRouter{ + httprouter.Router{ + RedirectTrailingSlash: true, + RedirectFixedPath: true, + HandleMethodNotAllowed: true, + HandleOPTIONS: true, + }, + } +} + +func (j *JulienschmidtHTTPRouter) AddHandler(method, path string, handler http.Handler) { + j.Handle(method, path, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { + if req.Form == nil { + req.Form = url.Values{} + } + for _, param := range params { + // stuffing values back into request.Form to honor the handler contract + req.Form.Add(param.Key, param.Value) + } + handler.ServeHTTP(w, req) + }) +} diff --git a/http/server.go b/http/server.go index e5b4e7d..e234390 100644 --- a/http/server.go +++ b/http/server.go @@ -3,20 +3,19 @@ package http import ( "log" "net/http" - "net/url" - - "github.com/julienschmidt/httprouter" + go_http "net/http" ) type Middleware interface { UseHandler(handler http.Handler) - ServeHTTP(w http.ResponseWriter, req *http.Request) + ServeHTTP(w go_http.ResponseWriter, req *go_http.Request) } type Router interface { - ServeFiles(path string, root http.FileSystem) - AddHandler(method, path string, handler http.Handler) - ServeHTTP(w http.ResponseWriter, req *http.Request) + go_http.Handler + ServeFiles(path string, root go_http.FileSystem) + AddHandler(method, path string, handler go_http.Handler) + // ServeHTTP(w http.ResponseWriter, req *http.Request) } type Service interface { @@ -32,13 +31,14 @@ func (f ServiceFunc) Register(uriBase string, restServer *Server) { var docString = "%s \t%s\t- %s" type Server struct { - router *httprouter.Router + //router *httprouter.Router + router Router middleware Middleware } -func NewServer(m Middleware) *Server { +func NewServer(m Middleware, r Router) *Server { s := &Server{ - router: httprouter.New(), + router: r, //httprouter.New(), middleware: m, } @@ -47,41 +47,42 @@ func NewServer(m Middleware) *Server { return s } -func (r *Server) GET(path string, documentation string, handle http.Handler) *Server { +func (r *Server) GET(path string, documentation string, handle go_http.Handler) *Server { r.handle("GET", path, documentation, handle) return r } -func (r *Server) PATCH(path string, documentation string, handle http.Handler) *Server { +func (r *Server) PATCH(path string, documentation string, handle go_http.Handler) *Server { r.handle("PATCH", path, documentation, handle) return r } -func (r *Server) POST(path string, documentation string, handle http.Handler) *Server { +func (r *Server) POST(path string, documentation string, handle go_http.Handler) *Server { r.handle("POST", path, documentation, handle) return r } -func (r *Server) PUT(path string, documentation string, handle http.Handler) *Server { +func (r *Server) PUT(path string, documentation string, handle go_http.Handler) *Server { r.handle("PUT", path, documentation, handle) return r } -func (r *Server) DELETE(path string, documentation string, handle http.Handler) *Server { +func (r *Server) DELETE(path string, documentation string, handle go_http.Handler) *Server { r.handle("DELETE", path, documentation, handle) return r } -func (r *Server) handle(method, path string, documentation string, handler http.Handler) { +func (r *Server) handle(method, path string, documentation string, handler go_http.Handler) { log.Printf(docString, method, path, documentation) - r.router.Handle(method, path, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { - if req.Form == nil { - req.Form = url.Values{} - } - for _, param := range params { // stuffing values back into request.Form to honor the handler contract - req.Form.Add(param.Key, param.Value) - } - handler.ServeHTTP(w, req) - }) + r.router.AddHandler(method, path, handler) + // r.router.Handle(method, path, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { + // if req.Form == nil { + // req.Form = url.Values{} + // } + // for _, param := range params { // stuffing values back into request.Form to honor the handler contract + // req.Form.Add(param.Key, param.Value) + // } + // handler.ServeHTTP(w, req) + // }) } func (r *Server) Banner(banner string) *Server { @@ -93,11 +94,11 @@ func (r *Server) Service(basePath string, service Service) *Server { service.Register(basePath, r) return r } -func (r *Server) Static(path string, root http.FileSystem) *Server { +func (r *Server) Static(path string, root go_http.FileSystem) *Server { r.router.ServeFiles(path, root) return r } -func (r *Server) Middleware(handler http.Handler) *Server { +func (r *Server) Middleware(handler go_http.Handler) *Server { r.middleware.UseHandler(handler) return r } @@ -107,6 +108,6 @@ func (r *Server) Run(addr string) { log.Fatal(http.ListenAndServe(addr, r.middleware)) } -func (r *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) { +func (r *Server) ServeHTTP(w go_http.ResponseWriter, req *go_http.Request) { r.middleware.ServeHTTP(w, req) } diff --git a/http/server_test.go b/http/server_test.go index 525dbdb..dafe503 100644 --- a/http/server_test.go +++ b/http/server_test.go @@ -3,6 +3,7 @@ package http_test import ( "os" + "github.com/codegangsta/negroni" "github.com/jchenry/jchenry/http" "github.com/jchenry/jchenry/rest" ) @@ -15,8 +16,14 @@ func ExampleServer() { Email string `json:"emailAddress"` } - s := http.NewServer(). - Service("", rest.Collection(new(contact), crud.NewInMemoryCrudService())) + s := http.NewServer( + negroni.Classic(), + http.NewJulienschmidtHTTPRouter()). + Service("", + rest.Collection(new(contact), + nil, //crud.NewInMemoryCrudService(), + ), + ) port := os.Getenv("PORT") if port == "" {