87 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using MareSynchronosShared.Authentication;
 | |
| using MareSynchronosShared.Data;
 | |
| using MareSynchronosShared.Protos;
 | |
| using Microsoft.AspNetCore.Authentication;
 | |
| using Microsoft.AspNetCore.Authorization;
 | |
| using Microsoft.AspNetCore.Builder;
 | |
| using Microsoft.AspNetCore.Hosting;
 | |
| using Microsoft.EntityFrameworkCore;
 | |
| using Microsoft.Extensions.Configuration;
 | |
| using Microsoft.Extensions.DependencyInjection;
 | |
| using Microsoft.Extensions.FileProviders;
 | |
| using System;
 | |
| 
 | |
| namespace MareSynchronosStaticFilesServer;
 | |
| 
 | |
| public class Startup
 | |
| {
 | |
|     public Startup(IConfiguration configuration)
 | |
|     {
 | |
|         Configuration = configuration;
 | |
|     }
 | |
| 
 | |
|     public IConfiguration Configuration { get; }
 | |
| 
 | |
|     public void ConfigureServices(IServiceCollection services)
 | |
|     {
 | |
|         services.AddHttpContextAccessor();
 | |
| 
 | |
|         services.AddTransient(_ => Configuration);
 | |
| 
 | |
|         var mareSettings = Configuration.GetRequiredSection("MareSynchronos");
 | |
| 
 | |
|         services.AddGrpcClient<AuthService.AuthServiceClient>(c =>
 | |
|         {
 | |
|             c.Address = new Uri(mareSettings.GetValue<string>("ServiceAddress"));
 | |
|         });
 | |
|         services.AddGrpcClient<MetricsService.MetricsServiceClient>(c =>
 | |
|         {
 | |
|             c.Address = new Uri(mareSettings.GetValue<string>("ServiceAddress"));
 | |
|         });
 | |
| 
 | |
|         services.AddDbContextPool<MareDbContext>(options =>
 | |
|         {
 | |
|             options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"), builder =>
 | |
|             {
 | |
|                 builder.MigrationsHistoryTable("_efmigrationshistory", "public");
 | |
|             }).UseSnakeCaseNamingConvention();
 | |
|             options.EnableThreadSafetyChecks(false);
 | |
|         }, mareSettings.GetValue("DbContextPoolSize", 1024));
 | |
| 
 | |
|         services.AddHostedService<CleanupService>();
 | |
| 
 | |
|         services.AddAuthentication(options =>
 | |
|             {
 | |
|                 options.DefaultScheme = SecretKeyGrpcAuthenticationHandler.AuthScheme;
 | |
|             })
 | |
|             .AddScheme<AuthenticationSchemeOptions, SecretKeyGrpcAuthenticationHandler>(SecretKeyGrpcAuthenticationHandler.AuthScheme, options => { });
 | |
|         services.AddAuthorization(options => options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build());
 | |
| 
 | |
|         services.AddGrpc(o =>
 | |
|         {
 | |
|             o.MaxReceiveMessageSize = null;
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
 | |
|     {
 | |
|         app.UseHttpLogging();
 | |
| 
 | |
|         app.UseRouting();
 | |
| 
 | |
|         app.UseAuthentication();
 | |
|         app.UseAuthorization();
 | |
| 
 | |
|         app.UseStaticFiles(new StaticFileOptions()
 | |
|         {
 | |
|             FileProvider = new PhysicalFileProvider(Configuration.GetRequiredSection("MareSynchronos")["CacheDirectory"]),
 | |
|             RequestPath = "/cache",
 | |
|             ServeUnknownFileTypes = true
 | |
|         });
 | |
| 
 | |
|         app.UseEndpoints(e =>
 | |
|         {
 | |
|             e.MapGrpcService<FileService>();
 | |
|         });
 | |
|     }
 | |
| } | 
