Program.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Microsoft.AspNetCore.Hosting.StaticWebAssets;
  2. using RackPeek.Domain;
  3. using RackPeek.Domain.Persistence;
  4. using RackPeek.Domain.Persistence.Yaml;
  5. using RackPeek.Domain.Resources;
  6. using RackPeek.Domain.Resources.Hardware;
  7. using RackPeek.Domain.Resources.Services;
  8. using RackPeek.Domain.Resources.SystemResources;
  9. using RackPeek.Web.Components;
  10. using RackPeek.Yaml;
  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 = "./config";
  23. var basePath = Directory.GetCurrentDirectory();
  24. // Resolve yamlDir as relative to basePath
  25. var yamlPath = Path.IsPathRooted(yamlDir)
  26. ? yamlDir
  27. : Path.Combine(basePath, yamlDir);
  28. if (!Directory.Exists(yamlPath))
  29. throw new DirectoryNotFoundException(
  30. $"YAML directory not found: {yamlPath}"
  31. );
  32. builder.Services.AddScoped<ITextFileStore, PhysicalTextFileStore>();
  33. var resources = new ResourceCollection();
  34. builder.Services.AddSingleton(resources);
  35. builder.Services.AddScoped<IResourceCollection>(sp =>
  36. new YamlResourceCollection(
  37. "./config/config.yaml",
  38. sp.GetRequiredService<ITextFileStore>(),
  39. sp.GetRequiredService<ResourceCollection>()));
  40. // Infrastructure
  41. builder.Services.AddScoped<IHardwareRepository, YamlHardwareRepository>();
  42. builder.Services.AddScoped<ISystemRepository, YamlSystemRepository>();
  43. builder.Services.AddScoped<IServiceRepository, YamlServiceRepository>();
  44. builder.Services.AddScoped<IResourceRepository, YamlResourceRepository>();
  45. builder.Services.AddUseCases();
  46. builder.Services.AddCommands();
  47. builder.Services.AddScoped<IConsoleEmulator, ConsoleEmulator>();
  48. // Add services to the container.
  49. builder.Services.AddRazorComponents()
  50. .AddInteractiveServerComponents();
  51. var app = builder.Build();
  52. // Configure the HTTP request pipeline.
  53. if (!app.Environment.IsDevelopment())
  54. {
  55. app.UseExceptionHandler("/Error");
  56. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  57. app.UseHsts();
  58. }
  59. app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
  60. app.UseHttpsRedirection();
  61. app.UseStaticFiles();
  62. app.UseAntiforgery();
  63. app.MapStaticAssets();
  64. app.MapRazorComponents<App>()
  65. .AddInteractiveServerRenderMode();
  66. await app.RunAsync();
  67. }
  68. }