summary refs log tree commit diff
path: root/ReferenceClientProxyImplementation/Program.cs
blob: c35248b227631f04d6348d73b22a7c5e02dec126 (plain) (blame)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
using ArcaneLibs;
using Microsoft.AspNetCore.HttpLogging;
using Microsoft.Extensions.Hosting.Systemd;
using ReferenceClientProxyImplementation.Configuration;
using ReferenceClientProxyImplementation.Patches;
using ReferenceClientProxyImplementation.Patches.Implementations;
using ReferenceClientProxyImplementation.Services;
using ReferenceClientProxyImplementation.Tasks;

// using Spacebar.API.Tasks.Startup;

if (!Directory.Exists("cache_formatted")) Directory.CreateDirectory("cache_formatted");
if (!Directory.Exists("cache")) Directory.CreateDirectory("cache");
/*foreach (var file in Directory.GetFiles("cache").Where(x => x.EndsWith(".js")))
{
    //JsFormatter.FormatJsFile(File.OpenRead(file), File.OpenWrite(file.Replace("cache", "cache_formatted")));
}*/

/*var processes = Directory.GetFiles("cache").Where(x => x.EndsWith(".js")).Select(file => JsFormatter.SafeFormat(file, file.Replace("cache", "cache_formatted"))).ToList();
while (processes.Any(x => !x.HasExited))
{
    Thread.Sleep(100);
}*/

//Environment.Exit(0);
// Tasks.RunStartup();

var builder = WebApplication.CreateBuilder(args);

builder.Host.ConfigureHostOptions(host => {
    host.ServicesStartConcurrently = true;
    host.ServicesStopConcurrently = true;
    host.ShutdownTimeout = TimeSpan.FromSeconds(5);
});

// builder.Services.AddHostedService<TemporaryTestJob>();

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpLogging(o => { o.LoggingFields = HttpLoggingFields.All; });
builder.Services.AddLogging(o => {
    if (SystemdHelpers.IsSystemdService())
        o.AddSystemdConsole();
    else o.AddConsole();
});

builder.Services.AddSingleton<ProxyConfiguration>();
// builder.Services.AddSingleton<BuildDownloadService>();

foreach (var taskType in ClassCollector<ITask>.ResolveFromAllAccessibleAssemblies())
{
    builder.Services.AddSingleton(typeof(ITask), taskType);
}
builder.Services.AddHostedService<Tasks>();

foreach (var taskType in ClassCollector<IPatch>.ResolveFromAllAccessibleAssemblies())
{
    builder.Services.AddSingleton(typeof(IPatch), taskType);
}
builder.Services.AddSingleton<PatchSet>();

builder.Services.AddSingleton<ClientStoreService>();

var app = builder.Build();

//
if (app.Environment.IsDevelopment()) {
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpLogging();
app.UseRouting();
// app.UseSentryTracing();

// app.UseEndpoints(endpoints => { endpoints.MapControllers(); });

app.Use((context, next) => {
    context.Response.Headers["Content-Type"] += "; charset=utf-8";
    context.Response.Headers["Access-Control-Allow-Origin"] = "*";
    return next.Invoke();
});
app.UseCors("*");

app.MapControllers();
app.UseDeveloperExceptionPage();

//
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute("default", "{controller=FrontendController}/{action=Index}/{id?}"); });

Console.WriteLine("Starting web server!");
if (args.Contains("--exit-on-modified")) {
    Console.WriteLine("[WARN] --exit-on-modified enabled, exiting on source file change!");
    new FileSystemWatcher {
        Path = Environment.CurrentDirectory,
        Filter = "*.cs",
        NotifyFilter = NotifyFilters.LastWrite,
        EnableRaisingEvents = true
    }.Changed += async (sender, args) => {
        Console.WriteLine("Source modified. Exiting...");
        await app.StopAsync();
        Environment.Exit(0);
    };
}

app.Run();