Program.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Microsoft.AspNetCore.Components;
  2. using Microsoft.AspNetCore.Hosting.StaticWebAssets;
  3. using RackPeek.Domain;
  4. using RackPeek.Domain.Persistence;
  5. using RackPeek.Domain.Persistence.Yaml;
  6. using RackPeek.Web.Components;
  7. using YamlDotNet.Serialization;
  8. using YamlDotNet.Serialization.NamingConventions;
  9. using Shared.Rcl;
  10. namespace RackPeek.Web;
  11. public class Program
  12. {
  13. public static async Task<WebApplication> BuildApp(WebApplicationBuilder builder)
  14. {
  15. StaticWebAssetsLoader.UseStaticWebAssets(
  16. builder.Environment,
  17. builder.Configuration
  18. );
  19. builder.Configuration.AddJsonFile($"appsettings.json", optional: true, reloadOnChange: false);
  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. {
  30. // Create empty file safely
  31. await using var fs = new FileStream(
  32. yamlFilePath,
  33. FileMode.CreateNew,
  34. FileAccess.Write,
  35. FileShare.None);
  36. // optionally write default YAML content
  37. await using var writer = new StreamWriter(fs);
  38. await writer.WriteLineAsync("# default config");
  39. }
  40. builder.Services.AddScoped<ITextFileStore, PhysicalTextFileStore>();
  41. builder.Services.AddScoped(sp =>
  42. {
  43. var nav = sp.GetRequiredService<NavigationManager>();
  44. return new HttpClient
  45. {
  46. BaseAddress = new Uri(nav.BaseUri)
  47. };
  48. });
  49. var resources = new ResourceCollection();
  50. builder.Services.AddSingleton(resources);
  51. builder.Services.AddScoped<RackPeekConfigMigrationDeserializer>();
  52. builder.Services.AddScoped<IResourceCollection>(sp =>
  53. new YamlResourceCollection(
  54. yamlFilePath,
  55. sp.GetRequiredService<ITextFileStore>(),
  56. sp.GetRequiredService<ResourceCollection>(),
  57. sp.GetRequiredService<RackPeekConfigMigrationDeserializer>()));
  58. // Infrastructure
  59. builder.Services.AddYamlRepos();
  60. builder.Services.AddUseCases();
  61. builder.Services.AddCommands();
  62. builder.Services.AddScoped<IConsoleEmulator, ConsoleEmulator>();
  63. // Add services to the container.
  64. builder.Services.AddRazorComponents()
  65. .AddInteractiveServerComponents();
  66. var app = builder.Build();
  67. // Configure the HTTP request pipeline.
  68. if (!app.Environment.IsDevelopment())
  69. {
  70. app.UseExceptionHandler("/Error");
  71. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  72. app.UseHsts();
  73. }
  74. app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
  75. app.UseHttpsRedirection();
  76. app.UseStaticFiles();
  77. app.UseAntiforgery();
  78. app.MapStaticAssets();
  79. app.MapRazorComponents<App>()
  80. .AddInteractiveServerRenderMode();
  81. return app;
  82. }
  83. public static async Task Main(string[] args)
  84. {
  85. var builder = WebApplication.CreateBuilder(args);
  86. var app = await BuildApp(builder);
  87. await app.RunAsync();
  88. }
  89. }