wabbajack/Wabbajack.Server/Startup.cs

245 lines
9.5 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System.Diagnostics;
using System.IO;
2021-11-27 18:31:35 +00:00
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using cesi.DTOs;
using CouchDB.Driver;
using CouchDB.Driver.Options;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Features;
2021-02-17 05:46:05 +00:00
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
2021-12-20 16:31:10 +00:00
using Nettle;
using Nettle.Compiler;
using Newtonsoft.Json;
2021-11-27 20:03:14 +00:00
using Octokit;
using Wabbajack.BuildServer;
using Wabbajack.Downloaders;
using Wabbajack.DTOs;
2021-11-27 18:31:35 +00:00
using Wabbajack.DTOs.JsonConverters;
using Wabbajack.DTOs.Logins;
2021-11-27 20:03:14 +00:00
using Wabbajack.Networking.GitHub;
using Wabbajack.Networking.Http;
using Wabbajack.Networking.Http.Interfaces;
using Wabbajack.Networking.NexusApi;
using Wabbajack.Paths;
using Wabbajack.RateLimiter;
2021-11-27 18:31:35 +00:00
using Wabbajack.Server.DataModels;
2021-12-20 16:31:10 +00:00
using Wabbajack.Server.Extensions;
using Wabbajack.Server.Services;
using Wabbajack.Services.OSIntegrated.TokenProviders;
2022-04-02 16:01:38 +00:00
using Wabbajack.Networking.WabbajackClientApi;
using Wabbajack.Paths.IO;
2022-06-22 01:38:42 +00:00
using Wabbajack.Server.DTOs;
using Wabbajack.VFS;
using YamlDotNet.Serialization.NamingConventions;
2022-04-02 16:01:38 +00:00
using Client = Wabbajack.Networking.GitHub.Client;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Server;
public class TestStartup : Startup
{
2021-10-23 16:51:17 +00:00
public TestStartup(IConfiguration configuration) : base(configuration)
{
}
2021-10-23 16:51:17 +00:00
}
public class Startup
{
public Startup(IConfiguration configuration)
{
2021-10-23 16:51:17 +00:00
Configuration = configuration;
}
2021-10-23 16:51:17 +00:00
public IConfiguration Configuration { get; }
2021-10-23 16:51:17 +00:00
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
2021-10-23 16:51:17 +00:00
options.DefaultAuthenticateScheme = ApiKeyAuthenticationOptions.DefaultScheme;
options.DefaultChallengeScheme = ApiKeyAuthenticationOptions.DefaultScheme;
})
.AddApiKeySupport(options => { });
services.Configure<FormOptions>(x =>
{
2021-10-23 16:51:17 +00:00
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue;
});
services.AddSingleton<AppSettings>();
services.AddSingleton<QuickSync>();
services.AddSingleton<GlobalInformation>();
services.AddSingleton<DiscordWebHook>();
2021-11-27 18:31:35 +00:00
services.AddSingleton<Metrics>();
services.AddSingleton<HttpClient>();
services.AddSingleton<AuthorFiles>();
services.AddSingleton<AuthorKeys>();
2021-11-27 20:03:14 +00:00
services.AddSingleton<Client>();
services.AddSingleton<NexusCacheManager>();
services.AddSingleton<NexusApi>();
2022-01-18 22:58:54 +00:00
services.AddSingleton<DiscordBackend>();
2022-02-09 18:30:55 +00:00
services.AddSingleton<TarLog>();
services.AddAllSingleton<IHttpDownloader, SingleThreadedDownloader>();
services.AddDownloadDispatcher(useLoginDownloaders:false, useProxyCache:false);
services.AddTransient(s =>
2022-06-08 04:00:16 +00:00
{
var settings = s.GetRequiredService<AppSettings>();
return new TemporaryFileManager(settings.TempPath.Combine(Environment.ProcessId + "_" + Guid.NewGuid()));
});
services.AddAllSingleton<ITokenProvider<WabbajackApiState>, WabbajackApiTokenProvider>();
services.AddAllSingleton<IResource, IResource<DownloadDispatcher>>(s => new Resource<DownloadDispatcher>("Downloads", 12));
services.AddAllSingleton<IResource, IResource<FileHashCache>>(s => new Resource<FileHashCache>("File Hashing", 12));
services.AddAllSingleton<IResource, IResource<Wabbajack.Networking.WabbajackClientApi.Client>>(s =>
new Resource<Wabbajack.Networking.WabbajackClientApi.Client>("Wabbajack Client", 4));
services.AddSingleton(s =>
new FileHashCache(KnownFolders.AppDataLocal.Combine("Wabbajack", "GlobalHashCache.sqlite"),
s.GetService<IResource<FileHashCache>>()!));
services.AddAllSingleton<ITokenProvider<NexusApiState>, NexusApiTokenProvider>();
services.AddAllSingleton<IResource, IResource<HttpClient>>(s => new Resource<HttpClient>("Web Requests", 12));
// Application Info
var version =
$"{ThisAssembly.Git.SemVer.Major}.{ThisAssembly.Git.SemVer.Major}.{ThisAssembly.Git.SemVer.Patch}{ThisAssembly.Git.SemVer.DashLabel}";
services.AddSingleton(s => new ApplicationInfo
{
ApplicationSlug = "Wabbajack",
ApplicationName = Environment.ProcessPath?.ToAbsolutePath().FileName.ToString() ?? "Wabbajack",
ApplicationSha = ThisAssembly.Git.Sha,
Platform = RuntimeInformation.ProcessArchitecture.ToString(),
OperatingSystemDescription = RuntimeInformation.OSDescription,
RuntimeIdentifier = RuntimeInformation.RuntimeIdentifier,
OSVersion = Environment.OSVersion.VersionString,
Version = version
});
2021-12-01 06:06:00 +00:00
services.AddResponseCaching();
2021-11-27 20:15:07 +00:00
services.AddSingleton(s =>
{
var settings = s.GetService<AppSettings>()!;
if (string.IsNullOrWhiteSpace(settings.GitHubKey))
return new GitHubClient(new ProductHeaderValue("wabbajack"));
var creds = new Credentials(settings.GitHubKey);
return new GitHubClient(new ProductHeaderValue("wabbajack")) {Credentials = creds};
});
2021-11-27 18:31:35 +00:00
services.AddDTOSerializer();
services.AddDTOConverters();
2021-10-23 16:51:17 +00:00
services.AddResponseCompression(options =>
{
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
options.MimeTypes = new[] {"application/json"};
});
// CouchDB
services.AddSingleton(s =>
{
var settings = s.GetRequiredService<AppSettings>().CesiDB;
var client = new CouchClient(settings.Endpoint, b =>
{
b.UseBasicAuthentication(settings.Username, settings.Password);
b.SetPropertyCase(PropertyCaseType.None);
b.SetJsonNullValueHandling(NullValueHandling.Ignore);
});
return client.GetDatabase<Analyzed>(settings.Database);
});
2022-06-22 01:38:42 +00:00
services.AddSingleton(s =>
{
var settings = s.GetRequiredService<AppSettings>().MetricsDB;
var client = new CouchClient(settings.Endpoint, b =>
2022-06-22 01:38:42 +00:00
{
b.UseBasicAuthentication(settings.Username, settings.Password);
2022-06-22 01:38:42 +00:00
b.SetPropertyCase(PropertyCaseType.None);
b.SetJsonNullValueHandling(NullValueHandling.Ignore);
});
return client.GetDatabase<Metric>(settings.Database);
2022-06-22 01:38:42 +00:00
});
2021-10-23 16:51:17 +00:00
services.AddMvc();
services
.AddControllers()
.AddJsonOptions(j =>
{
j.JsonSerializerOptions.PropertyNamingPolicy = null;
j.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault;
});
2021-12-20 16:31:10 +00:00
NettleEngine.GetCompiler().RegisterWJFunctions();
2021-10-23 16:51:17 +00:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
app.UseDeveloperExceptionPage();
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".rar"] = "application/x-rar-compressed";
provider.Mappings[".7z"] = "application/x-7z-compressed";
provider.Mappings[".zip"] = "application/zip";
provider.Mappings[".wabbajack"] = "application/zip";
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseResponseCompression();
app.UseService<DiscordWebHook>();
2021-12-01 06:06:00 +00:00
app.UseResponseCaching();
2021-10-23 16:51:17 +00:00
app.Use(next =>
{
return async context =>
{
2021-10-23 16:51:17 +00:00
var stopWatch = new Stopwatch();
stopWatch.Start();
context.Response.OnStarting(() =>
{
2021-10-23 16:51:17 +00:00
stopWatch.Stop();
var headers = context.Response.Headers;
headers.Add("Access-Control-Allow-Origin", "*");
headers.Add("Access-Control-Allow-Methods", "POST, GET");
headers.Add("Access-Control-Allow-Headers", "Accept, Origin, Content-type");
headers.Add("X-ResponseTime-Ms", stopWatch.ElapsedMilliseconds.ToString());
if (!headers.ContainsKey("Cache-Control"))
headers.Add("Cache-Control", "no-cache");
return Task.CompletedTask;
});
await next(context);
};
});
2021-10-23 16:51:17 +00:00
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "public")),
StaticFileOptions = {ServeUnknownFileTypes = true}
});
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
// Trigger the internal update code
2022-01-18 22:58:54 +00:00
app.ApplicationServices.GetRequiredService<NexusCacheManager>();
app.ApplicationServices.GetRequiredService<DiscordBackend>();
}
2021-10-23 16:51:17 +00:00
}