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 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();
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
{
|
{
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"DefaultConnection": "Data Source=../Database/klapi.db"
|
||||||
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
{
|
{
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"DefaultConnection": "Data Source=../Database/klapi.db"
|
||||||
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"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}}/
|
GET {{Public_HostAddress}}/
|
||||||
Accept: application/json
|
Accept: application/json
|
||||||
|
|
||||||
###
|
|
||||||
|
|||||||
46
start.sh
46
start.sh
@@ -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
|
||||||
echo "Session '$SESSION_NAME' already exists. Attaching..."
|
if [[ "$RESTART" == "true" ]]; then
|
||||||
exec tmux attach -t "$SESSION_NAME"
|
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..."
|
||||||
|
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"
|
||||||
|
|||||||
Reference in New Issue
Block a user