-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serilog + Make more shit thread safe + unfuck search
- Loading branch information
Showing
5 changed files
with
128 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,107 @@ | ||
using System.Net; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.EntityFrameworkCore; | ||
using Serilog; | ||
using Server; | ||
using Server.Api; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
Log.Logger = new LoggerConfiguration() | ||
.WriteTo.Console() | ||
.WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day, fileSizeLimitBytes: null) | ||
.CreateLogger(); | ||
|
||
builder.WebHost.UseKestrel(); | ||
|
||
// Add services to the container. | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
// Load in appsettings.json, and appsettings.Development.json if the environment is development. | ||
// Additionally, load in appsettings.Secret.json if it exists. | ||
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); | ||
builder.Configuration.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true); | ||
builder.Configuration.AddJsonFile("appsettings.Secret.json", optional: true, reloadOnChange: true); | ||
|
||
builder.Services.AddControllersWithViews(); | ||
builder.Services.AddDbContext<ReplayDbContext>(options => | ||
try | ||
{ | ||
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")); | ||
}); | ||
|
||
ReplayParser.Context = builder.Services.BuildServiceProvider().GetService<ReplayDbContext>(); | ||
|
||
builder.Services.AddCors(options => | ||
{ | ||
options.AddDefaultPolicy(builder => | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Host.UseSerilog(); | ||
|
||
builder.WebHost.UseKestrel(); | ||
|
||
// Add services to the container. | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
// Load in appsettings.json, and appsettings.Development.json if the environment is development. | ||
// Additionally, load in appsettings.Secret.json if it exists. | ||
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); | ||
builder.Configuration.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true); | ||
builder.Configuration.AddJsonFile("appsettings.Secret.json", optional: true, reloadOnChange: true); | ||
|
||
builder.Services.AddControllersWithViews(); | ||
builder.Services.AddDbContext<ReplayDbContext>(options => | ||
{ | ||
builder.AllowAnyOrigin() | ||
.AllowAnyMethod() | ||
.AllowAnyHeader(); | ||
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")); | ||
}); | ||
}); | ||
|
||
builder.Services.AddMvc(); | ||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseRouting(); | ||
app.MapControllerRoute( | ||
name: "default", | ||
pattern: "api/{controller=Home}/{action=Index}/{id?}"); | ||
|
||
// Run FetchReplays in a new thread. | ||
var tokens = new List<CancellationTokenSource>(); | ||
var URLs = builder.Configuration.GetSection("ReplayUrls").Get<string[]>(); | ||
if (URLs == null) | ||
{ | ||
throw new Exception("No replay URLs found in appsettings.json. Please set ReplayUrls to an array of URLs."); | ||
} | ||
foreach (var url in URLs) | ||
{ | ||
|
||
builder.Services.AddDbContext<ReplayParserDbContext>(options => | ||
{ | ||
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")); | ||
}); | ||
|
||
ReplayParser.Context = builder.Services.BuildServiceProvider().GetService<ReplayParserDbContext>(); // GOD THIS IS STUPID | ||
Check warning on line 43 in Server/Program.cs GitHub Actions / deploy
Check warning on line 43 in Server/Program.cs GitHub Actions / deploy
|
||
|
||
builder.Services.AddCors(options => | ||
{ | ||
options.AddDefaultPolicy(builder => | ||
{ | ||
builder.AllowAnyOrigin() | ||
.AllowAnyMethod() | ||
.AllowAnyHeader(); | ||
}); | ||
}); | ||
|
||
builder.Services.AddMvc(); | ||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseRouting(); | ||
app.MapControllerRoute( | ||
name: "default", | ||
pattern: "api/{controller=Home}/{action=Index}/{id?}"); | ||
|
||
// Run FetchReplays in a new thread. | ||
var tokens = new List<CancellationTokenSource>(); | ||
var URLs = builder.Configuration.GetSection("ReplayUrls").Get<string[]>(); | ||
if (URLs == null) | ||
{ | ||
throw new Exception("No replay URLs found in appsettings.json. Please set ReplayUrls to an array of URLs."); | ||
} | ||
|
||
var tokenSource = new CancellationTokenSource(); | ||
tokens.Add(tokenSource); | ||
var thread = new Thread(() => ReplayParser.FetchReplays(tokenSource.Token, url)); | ||
var thread = new Thread(() => ReplayParser.FetchReplays(tokenSource.Token, URLs)); | ||
Check warning on line 82 in Server/Program.cs GitHub Actions / deploy
|
||
thread.Start(); | ||
|
||
var token = new CancellationTokenSource(); | ||
tokens.Add(token); | ||
var thread2 = new Thread(() => ReplayParser.ConsumeQueue(token.Token)); | ||
Check warning on line 87 in Server/Program.cs GitHub Actions / deploy
|
||
thread2.Start(); | ||
|
||
app.Lifetime.ApplicationStopping.Register(() => | ||
{ | ||
foreach (var token in tokens) | ||
{ | ||
token.Cancel(); | ||
} | ||
}); | ||
|
||
app.Run(); | ||
} | ||
|
||
var token = new CancellationTokenSource(); | ||
tokens.Add(token); | ||
var thread2 = new Thread(() => ReplayParser.ConsumeQueue(token.Token)); | ||
thread2.Start(); | ||
|
||
app.Lifetime.ApplicationStopping.Register(() => | ||
catch (Exception e) | ||
{ | ||
foreach (var token in tokens) | ||
{ | ||
token.Cancel(); | ||
} | ||
}); | ||
|
||
app.Run(); | ||
Log.Fatal(e, "An error occurred while starting the server."); | ||
} | ||
finally | ||
{ | ||
Log.CloseAndFlush(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters