From 44a52f116436404197e53308e12337f7a25ae5e8 Mon Sep 17 00:00:00 2001 From: Stefan Koelle Date: Sun, 9 Aug 2026 14:05:44 +0200 Subject: [PATCH] cleanup 2 --- .dockerignore | 1 - .env.example | 5 +--- Controllers/FocusTasksController.cs | 24 ---------------- FocusApp.csproj | 1 - Models/Dtos.cs | 25 +++++++++++++++++ Program.cs | 43 +++++++++++++++++++++++++---- README.md | 13 ++++----- init-db.sh | 41 --------------------------- 8 files changed, 70 insertions(+), 83 deletions(-) create mode 100644 Models/Dtos.cs delete mode 100755 init-db.sh diff --git a/.dockerignore b/.dockerignore index d752f28..58f5247 100644 --- a/.dockerignore +++ b/.dockerignore @@ -40,4 +40,3 @@ temp/ README.md LICENSE init-db.sql -init-db.sh diff --git a/.env.example b/.env.example index 00129bc..626a43d 100644 --- a/.env.example +++ b/.env.example @@ -3,7 +3,4 @@ DB_HOST=maria-db-server.domain.local DB_PORT=3306 DB_NAME=focusapp DB_USER=focusapp -DB_PASSWORD=change-password - -# MariaDB Root (fuer init-db.sh) -DB_ROOT_PASSWORD=change-root-password +DB_PASSWORD= diff --git a/Controllers/FocusTasksController.cs b/Controllers/FocusTasksController.cs index 9dd4e95..143844f 100644 --- a/Controllers/FocusTasksController.cs +++ b/Controllers/FocusTasksController.cs @@ -128,27 +128,3 @@ public class FocusTasksController : ControllerBase return NoContent(); } } - -public class CreateTaskDto -{ - public string Title { get; set; } = string.Empty; - public string? Description { get; set; } -} - -public class UpdateTaskDto -{ - public string? Title { get; set; } - public string? Description { get; set; } - public int? Order { get; set; } -} - -public class ReorderDto -{ - public List Orders { get; set; } = new(); -} - -public class OrderItem -{ - public int Id { get; set; } - public int Order { get; set; } -} diff --git a/FocusApp.csproj b/FocusApp.csproj index 3c9eddd..71d64b8 100644 --- a/FocusApp.csproj +++ b/FocusApp.csproj @@ -14,7 +14,6 @@ - diff --git a/Models/Dtos.cs b/Models/Dtos.cs new file mode 100644 index 0000000..1e26f6a --- /dev/null +++ b/Models/Dtos.cs @@ -0,0 +1,25 @@ +namespace FocusApp.Models; + +public class CreateTaskDto +{ + public string Title { get; set; } = string.Empty; + public string? Description { get; set; } +} + +public class UpdateTaskDto +{ + public string? Title { get; set; } + public string? Description { get; set; } + public int? Order { get; set; } +} + +public class ReorderDto +{ + public List Orders { get; set; } = new(); +} + +public class OrderItem +{ + public int Id { get; set; } + public int Order { get; set; } +} diff --git a/Program.cs b/Program.cs index 404b512..5b5e952 100644 --- a/Program.cs +++ b/Program.cs @@ -4,11 +4,23 @@ using Microsoft.Extensions.FileProviders; var builder = WebApplication.CreateBuilder(args); +// Connection-String aus ENV-Variablen aufbauen (appsettings.json ist Fallback) +static string BuildConnectionString(IConfiguration config) +{ + return $"Server={config["DB_HOST"] ?? "localhost"};" + + $"Port={config["DB_PORT"] ?? "3306"};" + + $"Database={config["DB_NAME"] ?? "focusapp"};" + + $"User={config["DB_USER"] ?? "focusapp"};" + + $"Password={config["DB_PASSWORD"] ?? "change-password"}"; +} + +var connectionString = BuildConnectionString(builder.Configuration); + // Add services builder.Services.AddDbContext(options => options.UseMySql( - builder.Configuration.GetConnectionString("DefaultConnection"), - ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("DefaultConnection")) + connectionString, + ServerVersion.AutoDetect(connectionString) ) ); @@ -30,11 +42,32 @@ builder.WebHost.UseUrls("http://0.0.0.0:5000"); var app = builder.Build(); -// Ensure database is created +// Ensure database is created (mit Retry fuer gestartete MariaDB) using (var scope = app.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); - db.Database.EnsureCreated(); + var maxRetries = 5; + for (int i = 1; i <= maxRetries; i++) + { + try + { + db.Database.EnsureCreated(); + Console.WriteLine("Database connected!"); + break; + } + catch (Exception ex) + { + Console.WriteLine($"Database connection attempt {i}/{maxRetries} failed: {ex.Message}"); + if (i == maxRetries) + { + Console.WriteLine("Could not connect to database after max retries. Exiting."); + throw; + } + var delay = Math.Min(i * 5, 30); + Console.WriteLine($"Retrying in {delay}s..."); + Thread.Sleep(delay * 1000); + } + } } if (app.Environment.IsDevelopment()) @@ -73,7 +106,7 @@ else app.UseAuthorization(); app.MapControllers(); -// FALLBACK fόr SPA (alle nicht-API Routes >> index.html) +// FALLBACK fοΏ½r SPA (alle nicht-API Routes >> index.html) app.MapFallback(async context => { var indexPath = Path.Combine(clientPath, "index.html"); diff --git a/README.md b/README.md index 2b71853..821ceee 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,8 @@ Die FocusApp benΓΆtigt eine MariaDB Datenbank. Das Init-Script erstellt die Date # .env editieren - echte MariaDB-Zugangsdaten eintragen vim .env -# Init-Script ausfuehren (benoetigt MariaDB Root-Zugang) -chmod +x init-db.sh -./init-db.sh +# Init-Script als MariaDB Root ausfuehren +mysql -h $DB_HOST -u root -p < init-db.sql ``` Die `.env` enthalt: @@ -180,15 +179,15 @@ FocusApp/ β”œβ”€β”€ Data/ β”‚ └── FocusContext.cs # EF Core DbContext β”œβ”€β”€ Models/ -β”‚ └── FocusTask.cs # Domain Model +β”‚ β”œβ”€β”€ FocusTask.cs # Domain Model +β”‚ └── Dtos.cs # API Data Transfer Objects β”œβ”€β”€ Program.cs # ASP.NET Startup β”œβ”€β”€ FocusApp.csproj # Projekt-Datei β”œβ”€β”€ appsettings.json # Config β”œβ”€β”€ Dockerfile # Multi-Stage Docker Build β”œβ”€β”€ docker-compose.yml # Docker Compose Konfiguration β”œβ”€β”€ .env.example # Environment Template -β”œβ”€β”€ init-db.sql # MariaDB Init Script -└── init-db.sh # DB Init Shell Script +└── init-db.sql # MariaDB Init Script ``` ## πŸ› Troubleshooting @@ -210,7 +209,7 @@ docker compose ps mysql -h $DB_HOST -u $DB_USER -p # DB neu initialisieren -./init-db.sh +mysql -h $DB_HOST -u root -p < init-db.sql ``` ### Port bereits belegt diff --git a/init-db.sh b/init-db.sh deleted file mode 100755 index e0762a3..0000000 --- a/init-db.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash -# FocusApp Datenbank Initialisierung -# Fuehrt init-db.sql gegen den externen MariaDB Server aus - -set -e - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - -# .env laden -if [ -f "$SCRIPT_DIR/.env" ]; then - export $(grep -v '^#' "$SCRIPT_DIR/.env" | xargs) -else - echo "Fehler: .env Datei nicht gefunden unter $SCRIPT_DIR/.env" - exit 1 -fi - -# Pruefen ob notwendige Variablen gesetzt sind -if [ -z "$DB_HOST" ] || [ -z "$DB_ROOT_PASSWORD" ]; then - echo "Fehler: DB_HOST und DB_ROOT_PASSWORD muessen in .env gesetzt sein" - exit 1 -fi - -DB_PORT=${DB_PORT:-3306} - -echo "Verbinde zu MariaDB auf $DB_HOST:$DB_PORT..." - -# Pruefen ob mysql/mariadb Client verfuegbar ist -if command -v mariadb &> /dev/null; then - MYSQL_CMD="mariadb" -elif command -v mysql &> /dev/null; then - MYSQL_CMD="mysql" -else - echo "Fehler: weder 'mariadb' noch 'mysql' Client gefunden" - echo "Installieren Sie: sudo apt install mariadb-client" - exit 1 -fi - -# SQL ausfuehren -$MYSQL_CMD -h "$DB_HOST" -P "$DB_PORT" -u root -p"$DB_ROOT_PASSWORD" < "$SCRIPT_DIR/init-db.sql" - -echo "Datenbank focusapp erfolgreich initialisiert!"