Program.cs 3.3 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.Domain.Resources;
  7. using RackPeek.Domain.Resources.Hardware;
  8. using RackPeek.Domain.Resources.Services;
  9. using RackPeek.Domain.Resources.SystemResources;
  10. using RackPeek.Web.Components;
  11. using Shared.Rcl;
  12. namespace RackPeek.Web;
  13. public class Program
  14. {
  15. public static async Task Main(string[] args)
  16. {
  17. var builder = WebApplication.CreateBuilder(args);
  18. StaticWebAssetsLoader.UseStaticWebAssets(
  19. builder.Environment,
  20. builder.Configuration
  21. );
  22. var yamlDir = builder.Configuration.GetValue<string>("RPK_YAML_DIR") ?? "./config";
  23. var yamlFileName = "config.yaml";
  24. var basePath = Directory.GetCurrentDirectory();
  25. var yamlPath = Path.IsPathRooted(yamlDir)
  26. ? yamlDir
  27. : Path.Combine(basePath, yamlDir);
  28. Directory.CreateDirectory(yamlPath);
  29. var yamlFilePath = Path.Combine(yamlPath, yamlFileName);
  30. if (!File.Exists(yamlFilePath))
  31. {
  32. // Create empty file safely
  33. await using var fs = new FileStream(
  34. yamlFilePath,
  35. FileMode.CreateNew,
  36. FileAccess.Write,
  37. FileShare.None);
  38. // optionally write default YAML content
  39. await using var writer = new StreamWriter(fs);
  40. await writer.WriteLineAsync("# default config");
  41. }
  42. builder.Services.AddScoped<ITextFileStore, PhysicalTextFileStore>();
  43. builder.Services.AddScoped(sp =>
  44. {
  45. var nav = sp.GetRequiredService<NavigationManager>();
  46. return new HttpClient
  47. {
  48. BaseAddress = new Uri(nav.BaseUri)
  49. };
  50. });
  51. var resources = new ResourceCollection();
  52. builder.Services.AddSingleton(resources);
  53. builder.Services.AddScoped<IResourceCollection>(sp =>
  54. new YamlResourceCollection(
  55. yamlFilePath,
  56. sp.GetRequiredService<ITextFileStore>(),
  57. sp.GetRequiredService<ResourceCollection>()));
  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. await app.RunAsync();
  82. }
  83. }