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() {
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))

View File

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

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 (
"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)
}

View File

@ -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 == "" {