3
0

generate authenticator

This commit is contained in:
Tit Petric 2018-07-13 14:26:17 +02:00
parent c01c75f420
commit 2b3dcf16b5
6 changed files with 48 additions and 11 deletions

19
crm/auth.go Normal file
View File

@ -0,0 +1,19 @@
package crm
import (
"net/http"
)
var pass = func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
}
func (*ModuleHandlers) Authenticator() func(http.Handler) http.Handler {
return pass
}
func (*TypesHandlers) Authenticator() func(http.Handler) http.Handler {
return pass
}

View File

@ -46,6 +46,9 @@ type ModuleHandlersAPI interface {
ContentList(http.ResponseWriter, *http.Request)
ContentEdit(http.ResponseWriter, *http.Request)
ContentDelete(http.ResponseWriter, *http.Request)
// Authenticate API requests
Authenticator() func(http.Handler) http.Handler
}
// Compile time check to see if we implement the interfaces

View File

@ -25,16 +25,22 @@ import (
func MountRoutes(r chi.Router) {
module := ModuleHandlers{}.new()
types := TypesHandlers{}.new()
r.Route("/module", func(r chi.Router) {
r.Get("/list", module.List)
r.Post("/edit", module.Edit)
r.Get("/content/list", module.ContentList)
r.Post("/content/edit", module.ContentEdit)
r.Delete("/content/delete", module.ContentDelete)
r.Group(func(r chi.Router) {
r.Use(module.Authenticator())
r.Route("/module", func(r chi.Router) {
r.Get("/list", module.List)
r.Post("/edit", module.Edit)
r.Get("/content/list", module.ContentList)
r.Post("/content/edit", module.ContentEdit)
r.Delete("/content/delete", module.ContentDelete)
})
})
r.Route("/types", func(r chi.Router) {
r.Get("/list", types.List)
r.Get("/type/{id}", types.Type)
r.Group(func(r chi.Router) {
r.Use(types.Authenticator())
r.Route("/types", func(r chi.Router) {
r.Get("/list", types.List)
r.Get("/type/{id}", types.Type)
})
})
var printRoutes func(chi.Routes, string, string)

View File

@ -29,6 +29,9 @@ type {name}HandlersAPI interface {
{foreach $calls as $call}
{call.name|capitalize}(http.ResponseWriter, *http.Request)
{/foreach}
// Authenticate API requests
Authenticator() func(http.Handler) http.Handler
}
// Compile time check to see if we implement the interfaces

View File

@ -14,10 +14,13 @@ func MountRoutes(r chi.Router) {
{api.interface|strtolower} := {api.interface|capitalize}Handlers{}.new()
{/foreach}
{foreach $apis as $api}
r.Route("{api.path}", func(r chi.Router) {
r.Group(func (r chi.Router) {
r.Use({api.interface|strtolower}.Authenticator())
r.Route("{api.path}", func(r chi.Router) {
{foreach $api.apis as $call}
r.{eval echo capitalize(strtolower($call.method))}("{call.path}", {api.interface|strtolower}.{call.name|capitalize})
r.{eval echo capitalize(strtolower($call.method))}("{call.path}", {api.interface|strtolower}.{call.name|capitalize})
{/foreach}
})
})
{/foreach}

View File

@ -40,6 +40,9 @@ type TypesAPI interface {
type TypesHandlersAPI interface {
List(http.ResponseWriter, *http.Request)
Type(http.ResponseWriter, *http.Request)
// Authenticate API requests
Authenticator() func(http.Handler) http.Handler
}
// Compile time check to see if we implement the interfaces