Program.cs 3.2 KB

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