From 7e7ecaf65a438a55ac5fd2b73cd0afe50b444d99 Mon Sep 17 00:00:00 2001 From: Esteban Solano Granados Date: Mon, 13 Jun 2022 20:56:27 +0000 Subject: [PATCH] Enable swagger with flag Signed-off-by: GitHub --- react-aspnet-mongodb/backend/Program.cs | 13 +++++++++---- react-aspnet-mongodb/backend/appsettings.json | 7 ++++--- react-aspnet-mongodb/compose.yaml | 1 + 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/react-aspnet-mongodb/backend/Program.cs b/react-aspnet-mongodb/backend/Program.cs index 1d7a515..a0d39a7 100644 --- a/react-aspnet-mongodb/backend/Program.cs +++ b/react-aspnet-mongodb/backend/Program.cs @@ -14,15 +14,21 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); string connectionString = builder.Configuration.GetConnectionString("DocumentDbConnection"); -string databaseName = builder.Configuration["DocumentDbName"] ?? "BackendMongoDb"; -string collectionName = builder.Configuration["DocumentCollectionName"] ?? "ToDos"; +string databaseName = builder.Configuration.GetConnectionString("DocumentDbName") ?? "BackendMongoDb"; +string collectionName = builder.Configuration.GetConnectionString("DocumentCollectionName") ?? "ToDos"; builder.Services.AddTransient((_provider) => new MongoClient(connectionString)); var app = builder.Build(); +var isSwaggerEnabledFromConfig = bool.TrueString.Equals(builder.Configuration["EnableSwagger"] ?? "", StringComparison.OrdinalIgnoreCase); +if (isSwaggerEnabledFromConfig) +{ + Console.WriteLine("Swagger enabled via appsettings.json"); +} + // Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) +if (app.Environment.IsDevelopment() || isSwaggerEnabledFromConfig) { app.UseSwagger(); app.UseSwaggerUI(); @@ -50,7 +56,6 @@ app.MapGet("/api/todos/{id}", async (string id, MongoClient connection) => { var database = connection.GetDatabase(databaseName); var collection = database.GetCollection(collectionName); - var result = await collection.FindAsync(record => record.Id == id).ConfigureAwait(false) as ToDo; if (result is null) diff --git a/react-aspnet-mongodb/backend/appsettings.json b/react-aspnet-mongodb/backend/appsettings.json index f50d42b..ae60c5f 100644 --- a/react-aspnet-mongodb/backend/appsettings.json +++ b/react-aspnet-mongodb/backend/appsettings.json @@ -6,8 +6,9 @@ } }, "ConnectionStrings": { - "DocumentDbConnection": "mongodb://mongo:27017" + "DocumentDbConnection": "mongodb://mongo:27017", + "DocumentDbName": "BackendMongoDb", + "DocumentCollectionName": "ToDos" }, - "DocumentDbName": "BackendMongoDb", - "DocumentCollectionName": "ToDos2" + "EnableSwagger": "false" } \ No newline at end of file diff --git a/react-aspnet-mongodb/compose.yaml b/react-aspnet-mongodb/compose.yaml index 6c61744..76a7095 100644 --- a/react-aspnet-mongodb/compose.yaml +++ b/react-aspnet-mongodb/compose.yaml @@ -23,6 +23,7 @@ services: - mongo environment: - ASPNETCORE_URLS=http://+:8000 + - EnableSwagger=true networks: - react-backend - react-frontend