serve original openapi json

This commit is contained in:
Wolfgang Hottgenroth 2021-11-26 16:36:07 +01:00
parent 238821aed8
commit b0b286a5e7
Signed by: wn
GPG Key ID: E49AF3B9EF6DD469
5 changed files with 37 additions and 3 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
output/
__pycache__
regular.cs
OpenAPIDocCtrl.cs

3
ENV
View File

@ -1,6 +1,9 @@
PACKAGE_NAME=de.hottis.genericdatabaseapiservice
export PACKAGE_NAME
ROUTE_PREFIX="pdb/v2/api"
export ROUTE_PREFIX
Database__User=apiservicetestdb
Database__Host=172.16.10.18
# Database__Password=xxx

24
OpenAPIDocCtrl.cs.tmpl Normal file
View File

@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Threading.Tasks;
namespace de.hottis.genericdatabaseapiservice.Controllers {
[Route("/${env['routeprefix']}/openapi/JSON")]
public class OpenAPIDocCtrl : ControllerBase {
private IWebHostEnvironment _hostingEnvironment;
public OpenAPIDocCtrl(IWebHostEnvironment environment) {
_hostingEnvironment = environment;
}
[HttpGet]
public async Task<IActionResult> Index() {
return Content(
await System.IO.File.ReadAllTextAsync(Path.Combine(_hostingEnvironment.WebRootPath, "openapi-original.json")),
"text/plain"
);
}
}
}

View File

@ -56,8 +56,11 @@ apiDefinition["GENERATED_CS_COMMENT"] = """
// -----------------------------------------
"""
packageName = os.environ["PACKAGE_NAME"]
apiDefinition["env"] = { "packagename": packageName }
os.environ["PACKAGE_NAME"]
apiDefinition["env"] = {
"packagename": os.environ["PACKAGE_NAME"],
"routeprefix": os.environ["ROUTE_PREFIX"]
}
statementFinder = re.compile('STATEMENTBEGIN (.*) STATEMENTEND')
statementChecker = {
'get': re.compile('^select', re.IGNORECASE),

View File

@ -85,7 +85,9 @@ if [ "$STAGE2" = "1" ]; then
echo "patch DbService registering into generated startup code"
sed -i output/src/$PACKAGE_NAME/Startup.cs \
-e 's#\(using '$PACKAGE_NAME'.OpenApi;\)#\1\n\n// added by post-processor\nusing '$PACKAGE_NAME'.Services;\n#' \
-e 's#^\([[:space:]]*\)\(// Add framework services.\)#\1// added by post-processor\n\1services.AddTransient<IDbService, DbService>();\n\n\1\2#'
-e 's#^\([[:space:]]*\)\(// Add framework services.\)#\1// added by post-processor\n\1services.AddTransient<IDbService, DbService>();\n\n\1\2#' \
-e 's#\(c.RoutePrefix = "\)openapi\(";\)#\1'$ROUTE_PREFIX'/doc\2#' \
-e 's#\(app.UseHttpsRedirection();\)#// \1#'
echo "disable documentation warnings in generated code"
sed -i output/src/$PACKAGE_NAME/$PACKAGE_NAME.csproj \
@ -101,6 +103,7 @@ if [ "$STAGE2" = "1" ]; then
if [ -f regular.cs ]; then
echo "copy endpoint code into source code tree"
cp regular.cs output/src/$PACKAGE_NAME/Implementations
cp OpenAPIDocCtrl.cs output/src/$PACKAGE_NAME/Controllers
else
echo "implementation not available, forgot to run stage 1?"
fi