mirror of
https://github.com/docker/awesome-compose.git
synced 2025-04-29 07:38:02 +02:00
Added backend
Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
parent
c4c4136352
commit
718df2a62d
8 changed files with 473 additions and 22 deletions
33
react-aspnet-mongodb/backend/Program.cs
Normal file
33
react-aspnet-mongodb/backend/Program.cs
Normal file
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Driver;
|
||||
using Models;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
string connectionString = File.ReadAllText("/run/secrets/db-connection");
|
||||
builder.Services.AddTransient<MongoClient>((_provider) => new MongoClient(connectionString));
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapGet("/api", async (MongoClient connection) => {
|
||||
try
|
||||
{
|
||||
var client = new MongoClient(connectionString);
|
||||
var database = client.GetDatabase("BackendMongoDb");
|
||||
|
||||
var collection = database.GetCollection<ToDo>("ToDos");
|
||||
var results = await collection.Find(_ => true).ToListAsync().ConfigureAwait(false);
|
||||
|
||||
return Results.Ok(results);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Results.Problem(detail: ex.ToString());
|
||||
}
|
||||
});
|
||||
|
||||
app.Run();
|
Loading…
Add table
Add a link
Reference in a new issue