-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
67 lines (52 loc) · 2.49 KB
/
Program.cs
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
using AIMaestroProxy.Logging;
using AIMaestroProxy.Middleware;
using AIMaestroProxy.Models;
using AIMaestroProxy.Services;
using Microsoft.Extensions.Logging.Console;
using MySql.Data.MySqlClient;
using StackExchange.Redis;
var builder = WebApplication.CreateBuilder(args);
Console.WriteLine($"Environment: {builder.Environment.EnvironmentName}");
// Load Config values
builder.Configuration
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
// Configure services
builder.Services.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddConfiguration(builder.Configuration.GetSection("Logging"));
loggingBuilder.AddConsole(options => options.FormatterName = "custom");
loggingBuilder.AddConsoleFormatter<CustomConsoleFormatter, ConsoleFormatterOptions>();
});
var redisConnectionString = builder.Configuration.GetConnectionString("Redis");
ArgumentException.ThrowIfNullOrWhiteSpace(redisConnectionString);
// Add Singleton services for the database/redis clients
builder.Services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(redisConnectionString));
builder.Services.AddSingleton<MySqlConnection>(_ => new(builder.Configuration.GetConnectionString("MariaDb")));
// Add Singleton services for Services
builder.Services.AddSingleton<CacheService>();
builder.Services.AddSingleton<DatabaseService>();
builder.Services.AddSingleton<DataService>();
builder.Services.AddSingleton<GpuManagerService>();
builder.Services.AddControllers();
// Load up endpoints from config. Dynamic for idk why, someone can use this with any backend.
builder.Services.Configure<PathCategories>(builder.Configuration.GetSection("PathCategories"));
// Add transient HttpClient service for proxied requests
builder.Services.AddHttpClient<ProxiedRequestService>();
var app = builder.Build();
// Middleware to handle errors globally
app.UseMiddleware<ErrorHandlingMiddleware>();
// Middleware to log request trace ID and handle stopwatch
app.UseMiddleware<TraceIdLoggingMiddleware>();
app.UseMiddleware<StopwatchMiddleware>();
// Middleware to handle not found responses
app.UseMiddleware<NotFoundLoggingMiddleware>();
// Define the default route
app.MapControllerRoute(
name: "default",
pattern: "{*path}",
defaults: new { controller = "Proxy", action = "HandleRequest" });
app.Run();