upd(codegen): refactor for handlers/requests separation
This commit is contained in:
@@ -1,23 +1,13 @@
|
||||
<?php
|
||||
|
||||
$templates = array(
|
||||
"http_interfaces.tpl" => function($name, $api) {
|
||||
"http_handlers_inline.tpl" => function($name, $api) {
|
||||
return strtolower($name) . ".go";
|
||||
},
|
||||
"http_request.tpl" => function($name, $api) {
|
||||
return strtolower($name) . "_requests.go";
|
||||
},
|
||||
"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)
|
||||
foreach ($apis as $api) {
|
||||
if (is_array($api['struct'])) {
|
||||
$name = ucfirst($api['interface']);
|
||||
$filename = $dirname . "/" . $fn($name, $api);
|
||||
|
||||
@@ -30,6 +20,7 @@ foreach ($apis as $api) {
|
||||
$tpl->assign("self", strtolower(substr($name, 0, 1)));
|
||||
$tpl->assign("structs", $api['struct']);
|
||||
$imports = array();
|
||||
if (is_array($api['struct']))
|
||||
foreach ($api['struct'] as $struct) {
|
||||
if (isset($struct['imports']))
|
||||
foreach ($struct['imports'] as $import) {
|
||||
@@ -42,5 +33,4 @@ foreach ($apis as $api) {
|
||||
|
||||
file_put_contents($filename, $contents);
|
||||
echo $filename . "\n";
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,13 @@
|
||||
<?php
|
||||
|
||||
$templates = array(
|
||||
"http_interfaces.tpl" => function($name, $api) {
|
||||
"http_request_inline.tpl" => function($name, $api) {
|
||||
return strtolower($name) . ".go";
|
||||
},
|
||||
"http_request.tpl" => function($name, $api) {
|
||||
return strtolower($name) . "_requests.go";
|
||||
},
|
||||
"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)
|
||||
foreach ($apis as $api) {
|
||||
if (is_array($api['struct'])) {
|
||||
$name = ucfirst($api['interface']);
|
||||
$filename = $dirname . "/" . $fn($name, $api);
|
||||
|
||||
@@ -30,6 +20,7 @@ foreach ($apis as $api) {
|
||||
$tpl->assign("self", strtolower(substr($name, 0, 1)));
|
||||
$tpl->assign("structs", $api['struct']);
|
||||
$imports = array();
|
||||
if (is_array($api['struct']))
|
||||
foreach ($api['struct'] as $struct) {
|
||||
if (isset($struct['imports']))
|
||||
foreach ($struct['imports'] as $import) {
|
||||
@@ -42,5 +33,4 @@ foreach ($apis as $api) {
|
||||
|
||||
file_put_contents($filename, $contents);
|
||||
echo $filename . "\n";
|
||||
}
|
||||
}
|
||||
36
codegen/sam/rest/handlers/index.php
Normal file
36
codegen/sam/rest/handlers/index.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
$templates = array(
|
||||
"http_handlers_inline.tpl" => function($name, $api) {
|
||||
return strtolower($name) . ".go";
|
||||
},
|
||||
);
|
||||
|
||||
foreach ($templates as $template => $fn)
|
||||
foreach ($apis as $api) {
|
||||
$name = ucfirst($api['interface']);
|
||||
$filename = $dirname . "/" . $fn($name, $api);
|
||||
|
||||
$tpl->load($template);
|
||||
$tpl->assign($common);
|
||||
$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();
|
||||
if (is_array($api['struct']))
|
||||
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";
|
||||
}
|
||||
36
codegen/sam/rest/request/index.php
Normal file
36
codegen/sam/rest/request/index.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
$templates = array(
|
||||
"http_request_inline.tpl" => function($name, $api) {
|
||||
return strtolower($name) . ".go";
|
||||
},
|
||||
);
|
||||
|
||||
foreach ($templates as $template => $fn)
|
||||
foreach ($apis as $api) {
|
||||
$name = ucfirst($api['interface']);
|
||||
$filename = $dirname . "/" . $fn($name, $api);
|
||||
|
||||
$tpl->load($template);
|
||||
$tpl->assign($common);
|
||||
$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();
|
||||
if (is_array($api['struct']))
|
||||
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";
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package {package}
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/crusttech/crust/sam/rest/server"
|
||||
"github.com/crusttech/crust/{project}/rest/server"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
|
||||
51
codegen/templates/http_handlers_inline.tpl
Normal file
51
codegen/templates/http_handlers_inline.tpl
Normal file
@@ -0,0 +1,51 @@
|
||||
package {package}
|
||||
|
||||
{load warning.tpl}
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"context"
|
||||
"github.com/go-chi/chi"
|
||||
|
||||
"github.com/titpetric/factory/resputil"
|
||||
|
||||
"github.com/crusttech/crust/{project}/rest/request"
|
||||
)
|
||||
|
||||
// Internal API interface
|
||||
type {name}API interface {
|
||||
{foreach $calls as $call}
|
||||
{call.name|capitalize}(context.Context, *request.{name|expose}{call.name|capitalize}) (interface{}, error)
|
||||
{/foreach}
|
||||
}
|
||||
|
||||
// HTTP API interface
|
||||
type {name} struct {
|
||||
{foreach $calls as $call}
|
||||
{call.name|capitalize} func(http.ResponseWriter, *http.Request)
|
||||
{/foreach}
|
||||
}
|
||||
|
||||
func New{name}({self}h {name}API) *{name} {
|
||||
return &{name}{ldelim}{newline}
|
||||
{foreach $calls as $call}
|
||||
{call.name|capitalize}: func(w http.ResponseWriter, r *http.Request) {
|
||||
params := request.New{name|capitalize}{call.name|capitalize}()
|
||||
resputil.JSON(w, params.Fill(r), func() (interface{}, error) {
|
||||
return {self}h.{call.name|capitalize}(r.Context(), params)
|
||||
})
|
||||
},
|
||||
{/foreach}
|
||||
{rdelim}
|
||||
}
|
||||
|
||||
func ({self}h *{name})MountRoutes(r chi.Router, middlewares ...func(http.Handler) http.Handler) {
|
||||
r.Group(func (r chi.Router) {
|
||||
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}", {self}h.{call.name|capitalize})
|
||||
{/foreach}
|
||||
})
|
||||
})
|
||||
}
|
||||
51
codegen/templates/http_request_inline.tpl
Normal file
51
codegen/templates/http_request_inline.tpl
Normal file
@@ -0,0 +1,51 @@
|
||||
package {package}
|
||||
|
||||
{load warning.tpl}
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/go-chi/chi"
|
||||
)
|
||||
|
||||
var _ = chi.URLParam
|
||||
|
||||
{foreach $calls as $call}
|
||||
// {name} {call.name} request parameters
|
||||
type {name|expose}{call.name|capitalize} struct {
|
||||
{foreach $call.parameters as $params}
|
||||
{foreach $params as $method => $param}
|
||||
{param.name|expose} {param.type}{newline}
|
||||
{/foreach}
|
||||
{/foreach}
|
||||
}
|
||||
|
||||
func New{name|expose}{call.name|capitalize}() *{name|expose}{call.name|capitalize} {
|
||||
return &{name|expose}{call.name|capitalize}{}
|
||||
}
|
||||
|
||||
func ({self} *{name|expose}{call.name|capitalize}) Fill(r *http.Request) error {
|
||||
r.ParseForm()
|
||||
get := map[string]string{}
|
||||
post := map[string]string{}
|
||||
urlQuery := r.URL.Query()
|
||||
for name, param := range urlQuery {
|
||||
get[name] = string(param[0])
|
||||
}
|
||||
postVars := r.Form
|
||||
for name, param := range postVars {
|
||||
post[name] = string(param[0])
|
||||
}
|
||||
{foreach $call.parameters as $method => $params}
|
||||
{foreach $params as $param}
|
||||
{if strtolower($method) === "path"}
|
||||
{self}.{param.name|expose} = {if ($param.type !== "string")}{$parsers[$param.type]}({/if}chi.URLParam(r, "{param.name}"){if ($param.type !== "string")}){/if}
|
||||
{elseif substr($param.type, 0, 2) !== '[]'}
|
||||
{self}.{param.name|expose} = {if ($param.type !== "string")}{$parsers[$param.type]}({method|strtolower}["{param.name}"]){else}{method|strtolower}["{param.name}"]{/if}
|
||||
{/if}
|
||||
{/foreach}
|
||||
{/foreach}{newline}
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ RequestFiller = New{name|expose}{call.name|capitalize}()
|
||||
{/foreach}
|
||||
@@ -4,7 +4,7 @@ package {package}
|
||||
|
||||
import (
|
||||
"github.com/go-chi/chi"
|
||||
"net/http"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ({self}h *{name}Handlers)MountRoutes(r chi.Router, middlewares ...func(http.Handler) http.Handler) {
|
||||
|
||||
Reference in New Issue
Block a user