Program.cs 3.3 KB

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