Prepare Linux deployment with yt-dlp and persistent downloads

This commit is contained in:
Michal Vrba
2026-10-19 17:23:30 +02:00
parent 471010c536
commit a26c584462
15 changed files with 331 additions and 139 deletions
+31 -73
View File
@@ -1,21 +1,29 @@
using TuneRipper.Api.Services;
using Microsoft.AspNetCore.HttpOverrides;
using TuneRipper.Api.Configuration;
using TuneRipper.Api.Health;
using TuneRipper.Api.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Add HttpClient services
builder.Services.AddHttpClient();
// Register services
builder.Services.AddOpenApi();
builder.Services.Configure<YtDlpOptions>(builder.Configuration.GetSection(YtDlpOptions.SectionName));
builder.Services.Configure<StorageOptions>(builder.Configuration.GetSection(StorageOptions.SectionName));
builder.Services.AddScoped<IVideoService, VideoService>();
builder.Services.AddSingleton<ISessionService, SessionService>(); // Register session service as singleton
builder.Services.AddSingleton<ISessionService, SessionService>();
builder.Services.AddHealthChecks().AddCheck<ExternalToolsHealthCheck>("external_tools");
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownIPNetworks.Clear();
options.KnownProxies.Clear();
});
// Add CORS for Blazor WebAssembly client
builder.Services.AddCors(options =>
{
options.AddPolicy("BlazorPolicy", policy =>
options.AddPolicy("DevelopmentClient", policy =>
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
@@ -23,42 +31,24 @@ builder.Services.AddCors(options =>
});
});
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseForwardedHeaders();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseHttpsRedirection();
app.UseCors("DevelopmentClient");
}
app.UseHttpsRedirection();
// Enable static files serving (for downloading MP3 files)
app.UseStaticFiles();
// Enable CORS
app.UseCors("BlazorPolicy");
app.UseAuthorization();
// Add root endpoint with startup info
app.MapGet("/", (IWebHostEnvironment env, HttpContext context) =>
app.MapGet("/api", (IWebHostEnvironment env, HttpContext context) =>
{
// Get the current request's base URL
var request = context.Request;
var baseUrl = $"{request.Scheme}://{request.Host}";
// Determine URLs based on environment
var httpsUrl = app.Environment.IsDevelopment()
? "https://localhost:7160"
: baseUrl;
var httpUrl = app.Environment.IsDevelopment()
? "http://localhost:5112"
: baseUrl.Replace("https://", "http://");
return new
{
@@ -67,58 +57,26 @@ app.MapGet("/", (IWebHostEnvironment env, HttpContext context) =>
status = "running",
timestamp = DateTime.UtcNow,
environment = env.EnvironmentName,
urls = new
{
https = httpsUrl,
http = httpUrl
},
url = baseUrl,
endpoints = new
{
health = "/api/health",
video_details = "/api/video/details?url={youtubeUrl}",
health = "/health",
api_health = "/api/health",
process_video = "/api/video/process",
check_status = "/api/video/status/{jobId}",
get_result = "/api/video/result/{jobId}",
download = "/api/download/{jobId}",
download_by_session = "/api/download/{sessionId}/{jobId}",
download = "/api/download/{sessionId}/{jobId}",
download_session_zip = "/api/download/zip/{sessionId}",
session_zip_info = "/api/download/zip/info/{sessionId}",
session_create = "/api/session/create",
session_validate = "/api/session/validate/{sessionId}",
static_files = "/downloads/{fileName}",
openapi = "/openapi/v1.json"
},
features = new
{
session_management = true,
automatic_zip_creation = true,
session_based_organization = true,
backward_compatibility = true
},
testing = new
{
httpFile = "Use api-tests.http to test endpoints",
startWith = "GET /api/health to verify API is running"
session_create = "/api/session/create"
}
};
});
app.MapControllers();
app.MapHealthChecks("/health");
app.MapFallbackToFile("index.html");
// Log startup information with dynamic URLs
var isDevelopment = app.Environment.IsDevelopment();
var httpsLogUrl = isDevelopment ? "https://localhost:7160" : "[Production HTTPS URL]";
var httpLogUrl = isDevelopment ? "http://localhost:5112" : "[Production HTTP URL]";
app.Logger.LogInformation("🚀 TuneRipper API is starting...");
app.Logger.LogInformation("🌍 Environment: {Environment}", app.Environment.EnvironmentName);
app.Logger.LogInformation("🌐 API will be available at:");
app.Logger.LogInformation(" 🔒 HTTPS: {HttpsUrl}", httpsLogUrl);
app.Logger.LogInformation(" 🔓 HTTP: {HttpUrl}", httpLogUrl);
app.Logger.LogInformation("❤️ Health check: /api/health");
app.Logger.LogInformation("ℹ️ API info: /");
app.Logger.LogInformation("📁 Static files served from: /downloads/*");
app.Logger.LogInformation("🔗 Session-based downloads supported");
app.Logger.LogInformation("📦 Automatic ZIP file creation enabled");
app.Logger.LogInformation("TuneRipper is starting in {Environment}", app.Environment.EnvironmentName);
app.Logger.LogInformation("Health check: /health");
app.Run();