AppServer.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using WebTemplate.ServerAspects.Auth;
  2. using WebTemplate.ServerAspects.Controllers;
  3. using WebTemplate.ServerAspects.Cors;
  4. using WebTemplate.ServerAspects.Json;
  5. using WebTemplate.ServerAspects.Spa;
  6. using WebTemplate.ServerAspects.Swagger;
  7. using WebTemplate.ServerAspects.Validation;
  8. using WebTemplate.Status;
  9. namespace WebTemplate;
  10. public class AppServer
  11. {
  12. /// <summary>
  13. /// These are all the modules that are loaded. The order is _somewhat_ important since in many situations it is
  14. /// relevant which configuration or middleware is applied first and the modules are applied in the order in
  15. /// which they appear here.
  16. /// </summary>
  17. private readonly IList<IAppConfigurationModule> _modules = new List<IAppConfigurationModule>
  18. {
  19. new JsonModule(),
  20. new AuthModule(),
  21. new CorsModule(),
  22. new SwaggerModule(),
  23. new SpaRoutingModule(),
  24. new ValidationModule(),
  25. new StatusEndpointModule(),
  26. new ControllersModule(),
  27. };
  28. public void Start(string[] args)
  29. {
  30. var builder = WebApplication.CreateBuilder(args);
  31. SetupConfiguration(builder.Configuration, builder.Environment);
  32. foreach (var appConfigurationModule in _modules)
  33. {
  34. appConfigurationModule.ConfigureServices(builder.Services, builder.Configuration);
  35. }
  36. var app = builder.Build();
  37. app.UseHttpsRedirection();
  38. if (app.Environment.IsDevelopment())
  39. {
  40. app.UseDeveloperExceptionPage();
  41. }
  42. else
  43. {
  44. app.UseHsts();
  45. }
  46. foreach (var appConfigurationModule in _modules)
  47. {
  48. appConfigurationModule.ConfigureApplication(app);
  49. }
  50. app.Run();
  51. }
  52. private void SetupConfiguration(ConfigurationManager configuration, IWebHostEnvironment env)
  53. {
  54. configuration.AddJsonFile($"~/{env.ApplicationName}.json", optional: true);
  55. }
  56. }