Program.cs 4.4 KB

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