3
0

Implements JWT authorization along with some refactoring

- introduces auth package, for common (sam, crm) auth handlers
 - routes are now generated inside server package, each handler got its own MountRoutes(r chi.Router, middlewares ...func(http.Handler) http.Handler)
 - there MountRoute methods are now called from non-generated code so we can inject services to (rest) controllers
This commit is contained in:
Denis Arh
2018-07-26 20:12:14 +02:00
parent faeec5e21d
commit d886a98740
78 changed files with 2925 additions and 363 deletions
-25
View File
@@ -1,30 +1,5 @@
<?php
$name = ucfirst($api['interface']);
$filename = $dirname . "/router.go";
$tpl->load("http_routes.tpl");
$tpl->assign($common);
$tpl->assign("package", basename($dirname));
$tpl->assign("name", $name);
$tpl->assign("api", $api);
$tpl->assign("apis", $apis);
$tpl->assign("self", strtolower(substr($name, 0, 1)));
$tpl->assign("structs", $api['struct']);
$imports = array();
foreach ($api['struct'] as $struct) {
if (isset($struct['imports']))
foreach ($struct['imports'] as $import) {
$imports[] = $import;
}
}
$tpl->assign("imports", $imports);
$tpl->assign("calls", $api['apis']);
$contents = str_replace("\n\n}", "\n}", $tpl->get());
file_put_contents($filename, $contents);
echo $filename . "\n";
foreach ($apis as $api) {
if (is_array($api['struct'])) {
$name = ucfirst($api['interface']);
+5 -1
View File
@@ -9,7 +9,10 @@ $templates = array(
},
"http_handlers.tpl" => function($name, $api) {
return strtolower($name) . "_handlers.go";
}
},
"http_routes.tpl" => function($name, $api) {
return strtolower($name) . "_routes.go";
}
);
foreach ($templates as $template => $fn)
@@ -23,6 +26,7 @@ foreach ($apis as $api) {
$tpl->assign("package", basename(__DIR__));
$tpl->assign("name", $name);
$tpl->assign("api", $api);
$tpl->assign("apis", $apis);
$tpl->assign("self", strtolower(substr($name, 0, 1)));
$tpl->assign("structs", $api['struct']);
$imports = array();
-3
View File
@@ -17,9 +17,6 @@ type {name}API interface {
{foreach $calls as $call}
{call.name|capitalize}(context.Context, *{name|expose}{call.name|capitalize}Request) (interface{}, error)
{/foreach}
// Authenticate API requests
Authenticator() func(http.Handler) http.Handler
}
// HTTP API interface
+4 -10
View File
@@ -4,22 +4,16 @@ package {package}
import (
"github.com/go-chi/chi"
"github.com/crusttech/crust/{project}/rest/server"
"net/http"
)
func MountRoutes(r chi.Router) {
{foreach $apis as $api}
{api.interface|strtolower} := &server.{api.interface|capitalize}Handlers{{api.interface|capitalize}:{api.interface|capitalize}{ldelim}{rdelim}.New()}
{/foreach}
{foreach $apis as $api}
func ({self}h *{name}Handlers)MountRoutes(r chi.Router, middlewares ...func(http.Handler) http.Handler) {
r.Group(func (r chi.Router) {
r.Use({api.interface|strtolower}.{api.interface}.Authenticator())
r.Use(middlewares...)
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}", {self}h.{call.name|capitalize})
{/foreach}
})
})
{/foreach}
}