some code cleanup

This commit is contained in:
Colin Henry 2019-11-06 19:40:38 -08:00
parent 958c384f52
commit 3e56170c99
5 changed files with 18 additions and 30 deletions

View File

@ -18,17 +18,16 @@ type ServiceInstance struct {
func (si ServiceInstance) Register(uriBase string, s *jch_http.Server) { func (si ServiceInstance) Register(uriBase string, s *jch_http.Server) {
s.GET(uriBase+"/login", "login endpoint", http.HandlerFunc(NewLoginHandler(si.c))) s.Get(uriBase+"/login", "login endpoint", http.HandlerFunc(NewLoginHandler(si.c)))
s.GET(uriBase+"/logout", "logout endpoint", http.HandlerFunc(LogoutHandler)) s.Get(uriBase+"/logout", "logout endpoint", http.HandlerFunc(LogoutHandler))
s.GET(uriBase+"/callback", "oidc callback", http.HandlerFunc(NewCallbackHandler(si.c))) s.Get(uriBase+"/callback", "oidc callback", http.HandlerFunc(NewCallbackHandler(si.c)))
s.GET(uriBase+"/user", "user info endpoint", negroni.New( s.Get(uriBase+"/user", "user info endpoint", negroni.New(
negroni.HandlerFunc(IsAuthenticated), negroni.HandlerFunc(IsAuthenticated),
negroni.Wrap(http.HandlerFunc(UserHandler)), negroni.Wrap(http.HandlerFunc(UserHandler)),
)) ))
} }
func (si ServiceInstance) UpdateUser(u User) error { func (si ServiceInstance) UpdateUser(u User) error {
m, err := management.New(si.c.Domain, si.c.ManagementClientID, si.c.ManagementClientSecret) m, err := management.New(si.c.Domain, si.c.ManagementClientID, si.c.ManagementClientSecret)
if err != nil { if err != nil {
return err return err

View File

@ -15,7 +15,6 @@ type Router interface {
go_http.Handler go_http.Handler
ServeFiles(path string, root go_http.FileSystem) ServeFiles(path string, root go_http.FileSystem)
AddHandler(method, path string, handler go_http.Handler) AddHandler(method, path string, handler go_http.Handler)
// ServeHTTP(w http.ResponseWriter, req *http.Request)
} }
type Service interface { type Service interface {
@ -31,14 +30,13 @@ 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 Router router Router
middleware Middleware middleware Middleware
} }
func NewServer(m Middleware, r Router) *Server { func NewServer(m Middleware, r Router) *Server {
s := &Server{ s := &Server{
router: r, //httprouter.New(), router: r,
middleware: m, middleware: m,
} }
@ -47,26 +45,26 @@ func NewServer(m Middleware, r Router) *Server {
return s return s
} }
func (r *Server) GET(path string, documentation string, handle go_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 go_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 go_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 go_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 go_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
@ -74,15 +72,6 @@ func (r *Server) DELETE(path string, documentation string, handle go_http.Handle
func (r *Server) handle(method, path string, documentation string, handler go_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.AddHandler(method, path, handler) 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 { func (r *Server) Banner(banner string) *Server {

View File

@ -21,7 +21,7 @@ func ExampleServer() {
http.NewJulienschmidtHTTPRouter()). http.NewJulienschmidtHTTPRouter()).
Service("", Service("",
rest.Collection(new(contact), rest.Collection(new(contact),
nil, //crud.NewInMemoryCrudService(), nil,
), ),
) )

View File

@ -33,10 +33,10 @@ type ServiceInstance struct {
} }
func (si ServiceInstance) Register(uriBase string, s *jch_http.Server) { func (si ServiceInstance) Register(uriBase string, s *jch_http.Server) {
s.GET(uriBase+"/subscription", "subscription info endpoint", negroni.New( s.Get(uriBase+"/subscription", "subscription info endpoint", negroni.New(
negroni.HandlerFunc(auth.IsAuthenticated), negroni.HandlerFunc(auth.IsAuthenticated),
negroni.Wrap(http.HandlerFunc(si.subscriptionHandler)), negroni.Wrap(http.HandlerFunc(si.subscriptionHandler)),
)).POST(uriBase+"/subscription", "subscription payment endpoint", negroni.New( )).Post(uriBase+"/subscription", "subscription payment endpoint", negroni.New(
negroni.HandlerFunc(auth.IsAuthenticated), negroni.HandlerFunc(auth.IsAuthenticated),
negroni.Wrap(http.HandlerFunc(si.paymentHandler)), negroni.Wrap(http.HandlerFunc(si.paymentHandler)),
)) ))

View File

@ -53,11 +53,11 @@ func (collection *CollectionInstance) Register(uriBase string, restServer *jch_h
urlBase := uriBase + "/" + plural //collection.name + "s" urlBase := uriBase + "/" + plural //collection.name + "s"
restServer. restServer.
POST(urlBase, "create a "+collection.name, http.HandlerFunc(collection.create)). Post(urlBase, "create a "+collection.name, http.HandlerFunc(collection.create)).
PUT(urlBase+"/:"+IDPathParameter, "update a "+collection.name, http.HandlerFunc(collection.update)). Put(urlBase+"/:"+IDPathParameter, "update a "+collection.name, http.HandlerFunc(collection.update)).
DELETE(urlBase+"/:"+IDPathParameter, "delete a "+collection.name, http.HandlerFunc(collection.remove)). Delete(urlBase+"/:"+IDPathParameter, "delete a "+collection.name, http.HandlerFunc(collection.remove)).
GET(urlBase+"/:"+IDPathParameter, "get a "+collection.name+" by id", http.HandlerFunc(collection.find)). Get(urlBase+"/:"+IDPathParameter, "get a "+collection.name+" by id", http.HandlerFunc(collection.find)).
GET(urlBase, "get "+collection.name+"s", http.HandlerFunc(collection.find)) Get(urlBase, "get "+collection.name+"s", http.HandlerFunc(collection.find))
} }
func properPlural(word string) string { func properPlural(word string) string {