Enable swagger with flag

Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
Esteban Solano Granados 2022-06-13 20:56:27 +00:00 committed by GitHub
parent c0c1e7ca94
commit 7e7ecaf65a
3 changed files with 14 additions and 7 deletions

View File

@ -14,15 +14,21 @@ builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
string connectionString = builder.Configuration.GetConnectionString("DocumentDbConnection"); string connectionString = builder.Configuration.GetConnectionString("DocumentDbConnection");
string databaseName = builder.Configuration["DocumentDbName"] ?? "BackendMongoDb"; string databaseName = builder.Configuration.GetConnectionString("DocumentDbName") ?? "BackendMongoDb";
string collectionName = builder.Configuration["DocumentCollectionName"] ?? "ToDos"; string collectionName = builder.Configuration.GetConnectionString("DocumentCollectionName") ?? "ToDos";
builder.Services.AddTransient<MongoClient>((_provider) => new MongoClient(connectionString)); builder.Services.AddTransient<MongoClient>((_provider) => new MongoClient(connectionString));
var app = builder.Build(); 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. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment() || isSwaggerEnabledFromConfig)
{ {
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
@ -50,7 +56,6 @@ app.MapGet("/api/todos/{id}", async (string id, MongoClient connection) =>
{ {
var database = connection.GetDatabase(databaseName); var database = connection.GetDatabase(databaseName);
var collection = database.GetCollection<ToDo>(collectionName); var collection = database.GetCollection<ToDo>(collectionName);
var result = await collection.FindAsync(record => record.Id == id).ConfigureAwait(false) as ToDo; var result = await collection.FindAsync(record => record.Id == id).ConfigureAwait(false) as ToDo;
if (result is null) if (result is null)

View File

@ -6,8 +6,9 @@
} }
}, },
"ConnectionStrings": { "ConnectionStrings": {
"DocumentDbConnection": "mongodb://mongo:27017" "DocumentDbConnection": "mongodb://mongo:27017",
},
"DocumentDbName": "BackendMongoDb", "DocumentDbName": "BackendMongoDb",
"DocumentCollectionName": "ToDos2" "DocumentCollectionName": "ToDos"
},
"EnableSwagger": "false"
} }

View File

@ -23,6 +23,7 @@ services:
- mongo - mongo
environment: environment:
- ASPNETCORE_URLS=http://+:8000 - ASPNETCORE_URLS=http://+:8000
- EnableSwagger=true
networks: networks:
- react-backend - react-backend
- react-frontend - react-frontend