finally removed julienschmidt's http router out of serve, not is an implmentable interface

This commit is contained in:
Colin Henry 2019-11-02 15:29:14 -07:00
parent 055e0f7fe8
commit 958c384f52
5 changed files with 76 additions and 32 deletions

View File

@ -16,7 +16,7 @@ func main() {
func StartServer() { func StartServer() {
auth.PrintConfig() auth.PrintConfig()
s := jch_http.NewServer(negroni.New()). s := jch_http.NewServer(negroni.New(), jch_http.NewRouter()).
Static("/public/*filepath", http.Dir("public/")). Static("/public/*filepath", http.Dir("public/")).
Service("", auth.Service(auth.FromEnv())). Service("", auth.Service(auth.FromEnv())).
GET("/", "", http.HandlerFunc(HomeHandler)) GET("/", "", http.HandlerFunc(HomeHandler))

View File

@ -20,7 +20,7 @@ func StartServer() {
payments.PrintConfig() payments.PrintConfig()
auth_service := auth.Service(auth.FromEnv()) 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/")). Static("/public/*filepath", http.Dir("public/")).
Service("", auth_service). Service("", auth_service).
Service("", payments.Service(payments.FromEnv(), &auth_service)). Service("", payments.Service(payments.FromEnv(), &auth_service)).

View File

@ -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)
})
}

View File

@ -3,20 +3,19 @@ package http
import ( import (
"log" "log"
"net/http" "net/http"
"net/url" go_http "net/http"
"github.com/julienschmidt/httprouter"
) )
type Middleware interface { type Middleware interface {
UseHandler(handler http.Handler) UseHandler(handler http.Handler)
ServeHTTP(w http.ResponseWriter, req *http.Request) ServeHTTP(w go_http.ResponseWriter, req *go_http.Request)
} }
type Router interface { type Router interface {
ServeFiles(path string, root http.FileSystem) go_http.Handler
AddHandler(method, path string, handler http.Handler) ServeFiles(path string, root go_http.FileSystem)
ServeHTTP(w http.ResponseWriter, req *http.Request) AddHandler(method, path string, handler go_http.Handler)
// ServeHTTP(w http.ResponseWriter, req *http.Request)
} }
type Service interface { type Service interface {
@ -32,13 +31,14 @@ func (f ServiceFunc) Register(uriBase string, restServer *Server) {
var docString = "%s \t%s\t- %s" var docString = "%s \t%s\t- %s"
type Server struct { type Server struct {
router *httprouter.Router //router *httprouter.Router
router Router
middleware Middleware middleware Middleware
} }
func NewServer(m Middleware) *Server { func NewServer(m Middleware, r Router) *Server {
s := &Server{ s := &Server{
router: httprouter.New(), router: r, //httprouter.New(),
middleware: m, middleware: m,
} }
@ -47,41 +47,42 @@ func NewServer(m Middleware) *Server {
return s 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) r.handle("GET", path, documentation, handle)
return r 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) r.handle("PATCH", path, documentation, handle)
return r 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) r.handle("POST", path, documentation, handle)
return r 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) r.handle("PUT", path, documentation, handle)
return r 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) r.handle("DELETE", path, documentation, handle)
return r 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) log.Printf(docString, method, path, documentation)
r.router.Handle(method, path, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { r.router.AddHandler(method, path, handler)
if req.Form == nil { // r.router.Handle(method, path, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
req.Form = url.Values{} // 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) // 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) // }
}) // handler.ServeHTTP(w, req)
// })
} }
func (r *Server) Banner(banner string) *Server { func (r *Server) Banner(banner string) *Server {
@ -93,11 +94,11 @@ func (r *Server) Service(basePath string, service Service) *Server {
service.Register(basePath, r) service.Register(basePath, r)
return 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) r.router.ServeFiles(path, root)
return r return r
} }
func (r *Server) Middleware(handler http.Handler) *Server { func (r *Server) Middleware(handler go_http.Handler) *Server {
r.middleware.UseHandler(handler) r.middleware.UseHandler(handler)
return r return r
} }
@ -107,6 +108,6 @@ func (r *Server) Run(addr string) {
log.Fatal(http.ListenAndServe(addr, r.middleware)) 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) r.middleware.ServeHTTP(w, req)
} }

View File

@ -3,6 +3,7 @@ package http_test
import ( import (
"os" "os"
"github.com/codegangsta/negroni"
"github.com/jchenry/jchenry/http" "github.com/jchenry/jchenry/http"
"github.com/jchenry/jchenry/rest" "github.com/jchenry/jchenry/rest"
) )
@ -15,8 +16,14 @@ func ExampleServer() {
Email string `json:"emailAddress"` Email string `json:"emailAddress"`
} }
s := http.NewServer(). s := http.NewServer(
Service("", rest.Collection(new(contact), crud.NewInMemoryCrudService())) negroni.Classic(),
http.NewJulienschmidtHTTPRouter()).
Service("",
rest.Collection(new(contact),
nil, //crud.NewInMemoryCrudService(),
),
)
port := os.Getenv("PORT") port := os.Getenv("PORT")
if port == "" { if port == "" {