Refactoring API structure
This commit is contained in:
31
api/Public/Endpoints/PublicEndpoints.cs
Normal file
31
api/Public/Endpoints/PublicEndpoints.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
|
||||||
|
public static class PublicEndpoints
|
||||||
|
{
|
||||||
|
public static void MapPublicEndpoints(WebApplication app)
|
||||||
|
{
|
||||||
|
app.MapGet("/", () =>
|
||||||
|
{
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
Version = "1.0.0"
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.WithName("GetVersion");
|
||||||
|
|
||||||
|
app.MapGet("/health/db", async (SqliteConnection connection) =>
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
await using var command = connection.CreateCommand();
|
||||||
|
command.CommandText = "SELECT 1";
|
||||||
|
var result = await command.ExecuteScalarAsync();
|
||||||
|
|
||||||
|
return Results.Ok(new
|
||||||
|
{
|
||||||
|
Database = "ok",
|
||||||
|
Result = result
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.WithName("GetDatabaseHealth");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,60 +1,44 @@
|
|||||||
using Microsoft.Data.Sqlite;
|
using Microsoft.Data.Sqlite;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
public class Program
|
||||||
|
|
||||||
var configuredConnectionString = builder.Configuration.GetConnectionString("DefaultConnection")
|
|
||||||
?? throw new InvalidOperationException("Connection string 'DefaultConnection' was not found.");
|
|
||||||
|
|
||||||
var sqliteConnectionStringBuilder = new SqliteConnectionStringBuilder(configuredConnectionString);
|
|
||||||
var databasePath = Path.GetFullPath(Path.Combine(builder.Environment.ContentRootPath, sqliteConnectionStringBuilder.DataSource));
|
|
||||||
var databaseDirectory = Path.GetDirectoryName(databasePath)
|
|
||||||
?? throw new InvalidOperationException("Could not determine database directory.");
|
|
||||||
|
|
||||||
Directory.CreateDirectory(databaseDirectory);
|
|
||||||
|
|
||||||
sqliteConnectionStringBuilder.DataSource = databasePath;
|
|
||||||
var resolvedConnectionString = sqliteConnectionStringBuilder.ToString();
|
|
||||||
|
|
||||||
builder.Services.AddScoped(_ => new SqliteConnection(resolvedConnectionString));
|
|
||||||
|
|
||||||
builder.Services.AddOpenApi();
|
|
||||||
|
|
||||||
var app = builder.Build();
|
|
||||||
|
|
||||||
using (var connection = new SqliteConnection(resolvedConnectionString))
|
|
||||||
{
|
{
|
||||||
connection.Open();
|
public static void Main(string[] args)
|
||||||
}
|
|
||||||
|
|
||||||
if (app.Environment.IsDevelopment())
|
|
||||||
{
|
|
||||||
app.MapOpenApi();
|
|
||||||
}
|
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
|
||||||
|
|
||||||
app.MapGet("/", () =>
|
|
||||||
{
|
|
||||||
return new
|
|
||||||
{
|
{
|
||||||
Version = "1.0.0"
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
};
|
|
||||||
})
|
|
||||||
.WithName("GetVersion");
|
|
||||||
|
|
||||||
app.MapGet("/health/db", async (SqliteConnection connection) =>
|
var configuredConnectionString = builder.Configuration.GetConnectionString("DefaultConnection")
|
||||||
{
|
?? throw new InvalidOperationException("Connection string 'DefaultConnection' was not found.");
|
||||||
await connection.OpenAsync();
|
|
||||||
await using var command = connection.CreateCommand();
|
|
||||||
command.CommandText = "SELECT 1";
|
|
||||||
var result = await command.ExecuteScalarAsync();
|
|
||||||
|
|
||||||
return Results.Ok(new
|
var sqliteConnectionStringBuilder = new SqliteConnectionStringBuilder(configuredConnectionString);
|
||||||
{
|
var databasePath = Path.GetFullPath(Path.Combine(builder.Environment.ContentRootPath, sqliteConnectionStringBuilder.DataSource));
|
||||||
Database = "ok",
|
var databaseDirectory = Path.GetDirectoryName(databasePath)
|
||||||
Result = result
|
?? throw new InvalidOperationException("Could not determine database directory.");
|
||||||
});
|
|
||||||
})
|
|
||||||
.WithName("GetDatabaseHealth");
|
|
||||||
|
|
||||||
app.Run();
|
Directory.CreateDirectory(databaseDirectory);
|
||||||
|
|
||||||
|
sqliteConnectionStringBuilder.DataSource = databasePath;
|
||||||
|
var resolvedConnectionString = sqliteConnectionStringBuilder.ToString();
|
||||||
|
|
||||||
|
builder.Services.AddScoped(_ => new SqliteConnection(resolvedConnectionString));
|
||||||
|
|
||||||
|
builder.Services.AddOpenApi();
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
using (var connection = new SqliteConnection(resolvedConnectionString))
|
||||||
|
{
|
||||||
|
connection.Open();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (app.Environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.MapOpenApi();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
PublicEndpoints.MapPublicEndpoints(app);
|
||||||
|
|
||||||
|
app.Run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user