Program.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Text.Json.Serialization;
  2. using Microsoft.AspNetCore.Components;
  3. using Microsoft.AspNetCore.Hosting.StaticWebAssets;
  4. using RackPeek.Domain;
  5. using RackPeek.Domain.Git;
  6. using RackPeek.Domain.Persistence;
  7. using RackPeek.Domain.Persistence.Yaml;
  8. using RackPeek.Web.Api;
  9. using RackPeek.Web.Components;
  10. using Shared.Rcl;
  11. using Shared.Rcl.Docs;
  12. using Shared.Rcl.Servers;
  13. namespace RackPeek.Web;
  14. public class Program {
  15. public static async Task<WebApplication> BuildApp(WebApplicationBuilder builder) {
  16. StaticWebAssetsLoader.UseStaticWebAssets(
  17. builder.Environment,
  18. builder.Configuration
  19. );
  20. var yamlDir = builder.Configuration.GetValue<string>("RPK_YAML_DIR") ?? "./config";
  21. var yamlFileName = "config.yaml";
  22. var basePath = Directory.GetCurrentDirectory();
  23. var yamlPath = Path.IsPathRooted(yamlDir)
  24. ? yamlDir
  25. : Path.Combine(basePath, yamlDir);
  26. Directory.CreateDirectory(yamlPath);
  27. var yamlFilePath = Path.Combine(yamlPath, yamlFileName);
  28. if (!File.Exists(yamlFilePath)) {
  29. try {
  30. await using var fs = new FileStream(
  31. yamlFilePath,
  32. FileMode.CreateNew,
  33. FileAccess.Write,
  34. FileShare.None);
  35. await using var writer = new StreamWriter(fs);
  36. await writer.WriteLineAsync("# default config");
  37. }
  38. catch (IOException) when (File.Exists(yamlFilePath)) {
  39. // Another instance created the file between the existence
  40. // check and CreateNew — the config is there, carry on.
  41. }
  42. }
  43. builder.Services.ConfigureHttpJsonOptions(options => {
  44. options.SerializerOptions.Converters.Add(
  45. new JsonStringEnumConverter());
  46. });
  47. builder.Services.AddScoped<ITextFileStore, PhysicalTextFileStore>();
  48. builder.Services.AddScoped<IDocsContentProvider, StaticWebAssetDocsContentProvider>();
  49. builder.Services.AddGitServices(builder.Configuration, yamlPath);
  50. var resources = new ResourceCollection();
  51. builder.Services.AddSingleton(resources);
  52. builder.Services.AddScoped<RackPeekConfigMigrationDeserializer>();
  53. builder.Services.AddScoped<IResourceYamlMigrationService, ResourceYamlMigrationService>();
  54. builder.Services.AddScoped<IResourceCollection>(sp =>
  55. new YamlResourceCollection(
  56. yamlFilePath,
  57. sp.GetRequiredService<ITextFileStore>(),
  58. sp.GetRequiredService<ResourceCollection>(),
  59. sp.GetRequiredService<IResourceYamlMigrationService>()));
  60. // Infrastructure
  61. builder.Services.AddYamlRepos();
  62. builder.Services.AddUseCases();
  63. builder.Services.AddCommands();
  64. builder.Services.AddScoped<IConsoleEmulator, ConsoleEmulator>();
  65. // Razor Components
  66. builder.Services.AddRazorComponents()
  67. .AddInteractiveServerComponents();
  68. WebApplication app = builder.Build();
  69. if (!app.Environment.IsDevelopment()) {
  70. app.UseExceptionHandler("/Error");
  71. app.UseHsts();
  72. }
  73. app.UseHttpsRedirection();
  74. app.UseStaticFiles();
  75. app.UseRouting();
  76. app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
  77. app.UseAntiforgery();
  78. app.MapInventoryApi();
  79. app.MapStaticAssets();
  80. app.MapRazorComponents<App>()
  81. .AddInteractiveServerRenderMode()
  82. .AddAdditionalAssemblies(typeof(ServersListPage).Assembly);
  83. return app;
  84. }
  85. public static async Task Main(string[] args) {
  86. WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
  87. WebApplication app = await BuildApp(builder);
  88. await app.RunAsync();
  89. }
  90. }