mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 09:56:31 +00:00
9e7dc290db
Replace the WPF desktop application (Azaion.Suite, Azaion.Annotator, Azaion.Common, Azaion.Inference, Azaion.Loader, Azaion.LoaderUI, Azaion.Dataset, Azaion.Test) with a standalone .NET Web API in src/. Made-with: Cursor
86 lines
2.9 KiB
C#
86 lines
2.9 KiB
C#
using LinqToDB;
|
|
using LinqToDB.Data;
|
|
using Azaion.Annotations.Auth;
|
|
using Azaion.Annotations.Database;
|
|
using Azaion.Annotations.Middleware;
|
|
using Azaion.Annotations.Services;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
var databaseUrl = builder.Configuration["DATABASE_URL"]
|
|
?? Environment.GetEnvironmentVariable("DATABASE_URL")
|
|
?? "Host=localhost;Database=azaion;Username=postgres;Password=changeme";
|
|
|
|
var connectionString = databaseUrl.StartsWith("postgresql://")
|
|
? ConvertPostgresUrl(databaseUrl)
|
|
: databaseUrl;
|
|
|
|
var jwtSecret = builder.Configuration["JWT_SECRET"]
|
|
?? Environment.GetEnvironmentVariable("JWT_SECRET")
|
|
?? "development-secret-key-min-32-chars!!";
|
|
|
|
builder.Services.AddScoped(_ =>
|
|
{
|
|
var options = new DataOptions()
|
|
.UsePostgreSQL(connectionString);
|
|
return new AppDataConnection(options);
|
|
});
|
|
|
|
builder.Services.AddScoped<AnnotationService>();
|
|
builder.Services.AddScoped<MediaService>();
|
|
builder.Services.AddScoped<DatasetService>();
|
|
builder.Services.AddScoped<SettingsService>();
|
|
builder.Services.AddScoped<PathResolver>();
|
|
builder.Services.AddSingleton<AnnotationEventService>();
|
|
builder.Services.AddSingleton(new TokenService(jwtSecret));
|
|
|
|
var rabbitMqConfig = new RabbitMqConfig
|
|
{
|
|
Host = Environment.GetEnvironmentVariable("RABBITMQ_HOST") ?? "127.0.0.1",
|
|
Port = int.TryParse(Environment.GetEnvironmentVariable("RABBITMQ_STREAM_PORT"), out var rmqPort) ? rmqPort : 5552,
|
|
Username = Environment.GetEnvironmentVariable("RABBITMQ_PRODUCER_USER") ?? "azaion_producer",
|
|
Password = Environment.GetEnvironmentVariable("RABBITMQ_PRODUCER_PASS") ?? "producer_pass",
|
|
StreamName = Environment.GetEnvironmentVariable("RABBITMQ_STREAM_NAME") ?? "azaion-annotations"
|
|
};
|
|
builder.Services.AddSingleton(rabbitMqConfig);
|
|
builder.Services.AddHostedService<FailsafeProducer>();
|
|
|
|
builder.Services.AddJwtAuth(jwtSecret);
|
|
builder.Services.AddCors(options =>
|
|
options.AddDefaultPolicy(policy =>
|
|
policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));
|
|
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
var app = builder.Build();
|
|
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDataConnection>();
|
|
DatabaseMigrator.Migrate(db);
|
|
}
|
|
|
|
app.UseMiddleware<ErrorHandlingMiddleware>();
|
|
app.UseCors();
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
|
|
app.MapControllers();
|
|
app.MapGet("/health", () => Results.Ok(new { status = "healthy" }));
|
|
|
|
app.Run();
|
|
|
|
static string ConvertPostgresUrl(string url)
|
|
{
|
|
var uri = new Uri(url);
|
|
var userInfo = uri.UserInfo.Split(':');
|
|
var host = uri.Host;
|
|
var port = uri.Port > 0 ? uri.Port : 5432;
|
|
var database = uri.AbsolutePath.TrimStart('/');
|
|
return $"Host={host};Port={port};Database={database};Username={userInfo[0]};Password={userInfo.ElementAtOrDefault(1) ?? ""}";
|
|
}
|