Program.cs 3.4 KB

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