File size: 3,263 Bytes
8fb6238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356397f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb6238
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using ClipForge.Api.Endpoints;
using ClipForge.Infrastructure;
using ClipForge.Infrastructure.Data;
using ClipForge.Workers.Jobs;
using Hangfire;
using Hangfire.PostgreSql;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add Infrastructure (DI, DbContext, Services)
builder.Services.AddInfrastructure(builder.Configuration);

// Workers
builder.Services.AddScoped<FullPipelineJob>();

// Hangfire
builder.Services.AddHangfire(config =>
    config.UsePostgreSqlStorage(options =>
        options.UseNpgsqlConnection(builder.Configuration.GetConnectionString("DefaultConnection"))));
builder.Services.AddHangfireServer();

var app = builder.Build();

// Apply migrations on startup
using (var scope = app.Services.CreateScope())
{
    var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
    await db.Database.MigrateAsync();
}

// Hangfire Dashboard
app.UseHangfireDashboard("/hangfire");

// Serve static files (frontend)
app.UseDefaultFiles();
app.UseStaticFiles();

// Map API endpoints
app.MapIdeaEndpoints();
app.MapScriptEndpoints();
app.MapVoiceEndpoints();
app.MapVideoEndpoints();
app.MapPipelineEndpoints();

app.MapGet("/api/status", () => Results.Ok(new
{
    Name = "ClipForge API",
    Version = "1.0.0",
    Status = "Running"
}));

// Temporary cleanup endpoint
app.MapDelete("/api/admin/cleanup/{keepVideoId}", async (Guid keepVideoId, ClipForge.Infrastructure.Data.AppDbContext db) =>
{
    var sql = $@"
        DELETE FROM ""PublishJobs"" WHERE ""VideoAssetId"" != '{keepVideoId}';
        DELETE FROM ""CaptionAssets"" WHERE ""VideoAssetId"" != '{keepVideoId}';
        DELETE FROM ""VideoAssets"" WHERE ""Id"" != '{keepVideoId}';
        DELETE FROM ""VoiceAssets"" WHERE ""VideoScriptId"" NOT IN (SELECT ""VideoScriptId"" FROM ""VideoAssets"" WHERE ""Id"" = '{keepVideoId}');
        DELETE FROM ""VideoScripts"" WHERE ""Id"" NOT IN (SELECT ""VideoScriptId"" FROM ""VideoAssets"" WHERE ""Id"" = '{keepVideoId}');
        DELETE FROM ""VideoIdeas"" WHERE ""Id"" NOT IN (SELECT ""VideoIdeaId"" FROM ""VideoScripts"" WHERE ""Id"" IN (SELECT ""VideoScriptId"" FROM ""VideoAssets"" WHERE ""Id"" = '{keepVideoId}'));
    ";
    var rows = await db.Database.ExecuteSqlRawAsync(sql);
    return Results.Ok(new { Message = "Cleanup done", RowsDeleted = rows });
});

app.MapDelete("/api/admin/clear-all", async (ClipForge.Infrastructure.Data.AppDbContext db) =>
{
    var sql = @"
        DELETE FROM ""PublishJobs"";
        DELETE FROM ""CaptionAssets"";
        DELETE FROM ""VideoAssets"";
        DELETE FROM ""VoiceAssets"";
        DELETE FROM ""VideoScripts"";
        DELETE FROM ""VideoIdeas"";
    ";
    var rows = await db.Database.ExecuteSqlRawAsync(sql);

    // Clean output files
    var outputPath = Path.Combine(app.Environment.ContentRootPath, "output");
    if (Directory.Exists(outputPath))
    {
        foreach (var dir in Directory.GetDirectories(outputPath))
            Directory.Delete(dir, true);
        foreach (var dir in new[] { "audio", "videos", "thumbnails", "subtitles", "temp" })
            Directory.CreateDirectory(Path.Combine(outputPath, dir));
    }

    return Results.Ok(new { Message = "All data cleared", RowsDeleted = rows });
});

app.Run();