AppServer.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 AuthModule(),
  20. new CorsModule(),
  21. new JsonModule(),
  22. new SwaggerModule(),
  23. new ValidationModule(),
  24. new StatusEndpointModule(_ => new GitVersionInfo()),
  25. new ControllersModule(),
  26. // Keep the SpaRoutingModule at the very end
  27. new SpaRoutingModule(),
  28. };
  29. public void Start(string[] args)
  30. {
  31. var builder = WebApplication.CreateBuilder(args);
  32. SetupConfiguration(builder.Configuration, builder.Environment);
  33. foreach (var appConfigurationModule in _modules)
  34. {
  35. appConfigurationModule.ConfigureServices(builder.Services, builder.Configuration);
  36. }
  37. var app = builder.Build();
  38. app.UseHttpsRedirection();
  39. if (app.Environment.IsDevelopment())
  40. {
  41. app.UseDeveloperExceptionPage();
  42. }
  43. else
  44. {
  45. app.UseHsts();
  46. }
  47. foreach (var appConfigurationModule in _modules)
  48. {
  49. appConfigurationModule.ConfigureApplication(app);
  50. }
  51. app.Run();
  52. }
  53. private void SetupConfiguration(ConfigurationManager configuration, IWebHostEnvironment env)
  54. {
  55. configuration.AddJsonFile($"~/{env.ApplicationName}.json", optional: true);
  56. }
  57. }