Added new db connection and health check endpoint

This commit is contained in:
2026-02-17 21:02:24 +02:00
parent 9b86b95808
commit 7cb5c1aa7e
8 changed files with 85 additions and 11 deletions

0
api/Database/klapi.db Normal file
View File

View File

@@ -1,9 +1,31 @@
using Microsoft.Data.Sqlite;
var builder = WebApplication.CreateBuilder(args); 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(); builder.Services.AddOpenApi();
var app = builder.Build(); var app = builder.Build();
using (var connection = new SqliteConnection(resolvedConnectionString))
{
connection.Open();
}
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.MapOpenApi(); app.MapOpenApi();
@@ -20,4 +42,19 @@ app.MapGet("/", () =>
}) })
.WithName("GetVersion"); .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(); app.Run();

View File

@@ -8,6 +8,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,4 +1,7 @@
{ {
"ConnectionStrings": {
"DefaultConnection": "Data Source=../Database/klapi.db"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",

View File

@@ -1,4 +1,7 @@
{ {
"ConnectionStrings": {
"DefaultConnection": "Data Source=../Database/klapi.db"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",

View File

@@ -0,0 +1,4 @@
@Public_HostAddress = http://localhost:5013
GET {{Public_HostAddress}}/health/db
Accept: application/json

View File

@@ -2,5 +2,3 @@
GET {{Public_HostAddress}}/ GET {{Public_HostAddress}}/
Accept: application/json Accept: application/json
###

View File

@@ -5,23 +5,51 @@ SESSION_NAME="klapi"
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
API_DIR="$ROOT_DIR/api" API_DIR="$ROOT_DIR/api"
UI_DIR="$ROOT_DIR/ui" UI_DIR="$ROOT_DIR/ui"
RESTART=false
if [[ "${1:-}" == "--restart" ]]; then
RESTART=true
elif [[ -n "${1:-}" ]]; then
echo "Usage: $0 [--restart]" >&2
exit 1
fi
if ! command -v tmux >/dev/null 2>&1; then if ! command -v tmux >/dev/null 2>&1; then
echo "tmux is not installed or not in PATH" >&2 echo "tmux is not installed or not in PATH" >&2
exit 1 exit 1
fi fi
ensure_app_window() {
if ! tmux list-windows -t "$SESSION_NAME" -F '#{window_name}' | grep -qx 'app'; then
tmux new-window -t "$SESSION_NAME" -n app -c "$UI_DIR"
fi
local pane_count
pane_count="$(tmux list-panes -t "$SESSION_NAME":app | wc -l | tr -d ' ')"
if [[ "$pane_count" -lt 2 ]]; then
tmux split-window -v -t "$SESSION_NAME":app -c "$API_DIR"
fi
}
start_or_restart_services() {
ensure_app_window
tmux respawn-pane -k -t "$SESSION_NAME":app.0 -c "$UI_DIR" "bun dev"
tmux respawn-pane -k -t "$SESSION_NAME":app.1 -c "$API_DIR" "dotnet run --project $API_DIR/Public/Public.csproj"
tmux select-pane -t "$SESSION_NAME":app.0
tmux select-window -t "$SESSION_NAME":app
}
if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
if [[ "$RESTART" == "true" ]]; then
echo "Restarting services in existing session '$SESSION_NAME'..."
start_or_restart_services
exec tmux attach -t "$SESSION_NAME"
else
echo "Session '$SESSION_NAME' already exists. Attaching..." echo "Session '$SESSION_NAME' already exists. Attaching..."
exec tmux attach -t "$SESSION_NAME" exec tmux attach -t "$SESSION_NAME"
fi
fi fi
tmux new-session -d -s "$SESSION_NAME" -n app -c "$UI_DIR" tmux new-session -d -s "$SESSION_NAME" -n app -c "$UI_DIR"
tmux send-keys -t "$SESSION_NAME":app "bun dev" C-m start_or_restart_services
tmux split-window -v -t "$SESSION_NAME":app -c "$API_DIR"
tmux send-keys -t "$SESSION_NAME":app.1 "dotnet run --project $API_DIR/Public/Public.csproj" C-m
tmux select-pane -t "$SESSION_NAME":app.0
tmux select-window -t "$SESSION_NAME":app
exec tmux attach -t "$SESSION_NAME" exec tmux attach -t "$SESSION_NAME"