Added new db connection and health check endpoint
This commit is contained in:
0
api/Database/klapi.db
Normal file
0
api/Database/klapi.db
Normal file
@@ -1,9 +1,31 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
@@ -20,4 +42,19 @@ app.MapGet("/", () =>
|
||||
})
|
||||
.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");
|
||||
|
||||
app.Run();
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Data Source=../Database/klapi.db"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Data Source=../Database/klapi.db"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
|
||||
4
api/Tests/Http/Health-Db.http
Normal file
4
api/Tests/Http/Health-Db.http
Normal file
@@ -0,0 +1,4 @@
|
||||
@Public_HostAddress = http://localhost:5013
|
||||
|
||||
GET {{Public_HostAddress}}/health/db
|
||||
Accept: application/json
|
||||
@@ -2,5 +2,3 @@
|
||||
|
||||
GET {{Public_HostAddress}}/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
|
||||
Reference in New Issue
Block a user