From 3e830d2488d6ae33de5f4128da27dea71a8dac6c Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 28 May 2025 03:32:28 +0200 Subject: [PATCH] Add standalone OpenAPI mode and code generation setup Introduced a "--controllers-only" mode in the API for serving OpenAPI endpoints exclusively. Added scripts, Docker configurations, and run settings to enable automated code generation for the Palantir client. Removed obsolete EF metadata target file. --- docker/.gitignore | 2 + docker/code-gen/docker-compose.yml | 42 +++++++++++++++++++ obj/Data.EntityFrameworkCore.targets | 28 ------------- scripts/code-gen/gen-palantir-client.cmd | 10 +++++ src/dotnet/.run/Gandalf.run.xml | 7 ++++ .../Suspectus.Gandalf.Palantir.Api/Program.cs | 22 +++++++++- .../Properties/launchSettings.json | 11 +++++ 7 files changed, 92 insertions(+), 30 deletions(-) create mode 100644 docker/.gitignore create mode 100644 docker/code-gen/docker-compose.yml delete mode 100644 obj/Data.EntityFrameworkCore.targets create mode 100644 scripts/code-gen/gen-palantir-client.cmd create mode 100644 src/dotnet/.run/Gandalf.run.xml diff --git a/docker/.gitignore b/docker/.gitignore new file mode 100644 index 0000000..31447cc --- /dev/null +++ b/docker/.gitignore @@ -0,0 +1,2 @@ +code-gen/ +!code-gen/**/docker-compose.yml \ No newline at end of file diff --git a/docker/code-gen/docker-compose.yml b/docker/code-gen/docker-compose.yml new file mode 100644 index 0000000..b77e381 --- /dev/null +++ b/docker/code-gen/docker-compose.yml @@ -0,0 +1,42 @@ +services: + + palantir: + image: mcr.microsoft.com/dotnet/sdk:9.0 + container_name: codegen-backend + volumes: + - ./dotnet:/app/ + working_dir: /app + restart: no + command: > + dotnet + run + --project Suspectus.Gandalf.Palantir.Api + -v n + --urls "http://0.0.0.0:5035" + -- --controllers-only + healthcheck: + test: [ "CMD", "curl", "-f", "http://localhost:5035/openapi/v1.json" ] + interval: 5s + timeout: 3s + retries: 100 + start_period: 40s + start_interval: 20s + + typescript-gen: + image: swaggerapi/swagger-codegen-cli-v3 + container_name: codegen-cli + command: > + config-help + -l typescript-angular +# generate +# -i http://palantir:5035/openapi/v1.json +# -l csharp +# -o /local +# -c /options.json + restart: no + depends_on: + palantir: + condition: service_healthy + volumes: + - ./palantir-client:/local +# - ./options.json:/options.json \ No newline at end of file diff --git a/obj/Data.EntityFrameworkCore.targets b/obj/Data.EntityFrameworkCore.targets deleted file mode 100644 index 7d6485d..0000000 --- a/obj/Data.EntityFrameworkCore.targets +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/scripts/code-gen/gen-palantir-client.cmd b/scripts/code-gen/gen-palantir-client.cmd new file mode 100644 index 0000000..87b56f7 --- /dev/null +++ b/scripts/code-gen/gen-palantir-client.cmd @@ -0,0 +1,10 @@ +@echo off +pushd "../../docker/code-gen" +rmdir /s /q "palantir-client" +rmdir /s /q "dotnet" +xcopy "../../src/dotnet" "./dotnet\" /E/H +cmd /c "docker-compose up --abort-on-container-exit --force-recreate" +cmd /c "docker-compose down" +rmdir /s /q "../../src/dotnet/Suspectus.Gandalf.Palantir.Client/Generated" +xcopy "./palantir-client" "../../src/dotnet/Suspectus.Gandalf.Palantir.Client/Generated\" /E/H +popd diff --git a/src/dotnet/.run/Gandalf.run.xml b/src/dotnet/.run/Gandalf.run.xml new file mode 100644 index 0000000..990965c --- /dev/null +++ b/src/dotnet/.run/Gandalf.run.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/src/dotnet/Suspectus.Gandalf.Palantir.Api/Program.cs b/src/dotnet/Suspectus.Gandalf.Palantir.Api/Program.cs index 17d4676..51d9815 100644 --- a/src/dotnet/Suspectus.Gandalf.Palantir.Api/Program.cs +++ b/src/dotnet/Suspectus.Gandalf.Palantir.Api/Program.cs @@ -11,16 +11,34 @@ using Suspectus.Gandalf.Palantir.Data.Database.Repositories; var builder = WebApplication.CreateBuilder(args); - builder.Services.AddSingleton(TimeProvider.System); builder.Services.AddEndpointsApiExplorer(); + +var onlyControllers = args.Contains("--controllers-only"); +if (onlyControllers) +{ + builder.Services.AddOpenApi(); + builder.Services.AddRouting(options => options.LowercaseUrls = true); + builder.Services.AddControllers().AddJsonOptions(options => + { + options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; + options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; + options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); + }); + var onlyControllersApp = builder.Build(); + onlyControllersApp.MapOpenApi(); + onlyControllersApp.UseHttpsRedirection(); + onlyControllersApp.MapControllers(); + onlyControllersApp.Run(); + return; +} + builder.Services.AddDbContext(cfg => cfg .UseNpgsql( builder.Configuration.GetConnectionString("DefaultConnection"), x => x.MigrationsAssembly("Suspectus.Gandalf.Palantir.Data") ) ); - builder.Services.AddAutoMapper(typeof(ApplicationContext).Assembly); builder.Services.Configure(opt => diff --git a/src/dotnet/Suspectus.Gandalf.Palantir.Api/Properties/launchSettings.json b/src/dotnet/Suspectus.Gandalf.Palantir.Api/Properties/launchSettings.json index d2ca3c6..380857d 100644 --- a/src/dotnet/Suspectus.Gandalf.Palantir.Api/Properties/launchSettings.json +++ b/src/dotnet/Suspectus.Gandalf.Palantir.Api/Properties/launchSettings.json @@ -20,6 +20,17 @@ "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } + }, + "http-openapi-only": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5035", + "commandLineArgs": "--controllers-only", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } } } }