AppServer.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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.Status;
  8. namespace WebTemplate;
  9. public class AppServer
  10. {
  11. /// <summary>
  12. /// These are all the modules that are loaded. The order is _somewhat_ important since in many situations it is
  13. /// relevant which configuration or middleware is applied first and the modules are applied in the order in
  14. /// which they appear here.
  15. /// </summary>
  16. private readonly IList<IAppConfigurationModule> _modules = new List<IAppConfigurationModule>
  17. {
  18. new JsonModule(),
  19. new AuthModule(),
  20. new CorsModule(),
  21. new SwaggerModule(),
  22. new SpaRoutingModule(),
  23. new StatusEndpointModule(),
  24. new ControllersModule(),
  25. };
  26. public void Start(string[] args)
  27. {
  28. var builder = WebApplication.CreateBuilder(args);
  29. SetupConfiguration(builder.Configuration, builder.Environment);
  30. foreach (var appConfigurationModule in _modules)
  31. {
  32. appConfigurationModule.ConfigureServices(builder.Services, builder.Configuration);
  33. }
  34. var app = builder.Build();
  35. app.UseHttpsRedirection();
  36. foreach (var appConfigurationModule in _modules)
  37. {
  38. appConfigurationModule.ConfigureApplication(app);
  39. }
  40. app.Run();
  41. }
  42. private void SetupConfiguration(ConfigurationManager configuration, IWebHostEnvironment env)
  43. {
  44. configuration.AddJsonFile($"~/{env.ApplicationName}.json", optional: true);
  45. }
  46. }