Program.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Microsoft.AspNetCore.Hosting.StaticWebAssets;
  2. using RackPeek.Domain;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.Hardware;
  5. using RackPeek.Domain.Resources.Services;
  6. using RackPeek.Domain.Resources.SystemResources;
  7. using RackPeek.Web.Components;
  8. using RackPeek.Yaml;
  9. namespace RackPeek.Web;
  10. public class Program
  11. {
  12. public static void Main(string[] args)
  13. {
  14. var builder = WebApplication.CreateBuilder(args);
  15. StaticWebAssetsLoader.UseStaticWebAssets(
  16. builder.Environment,
  17. builder.Configuration
  18. );
  19. var yamlDir = "./config";
  20. var collection = new YamlResourceCollection(false);
  21. var basePath = Directory.GetCurrentDirectory();
  22. // Resolve yamlDir as relative to basePath
  23. var yamlPath = Path.IsPathRooted(yamlDir)
  24. ? yamlDir
  25. : Path.Combine(basePath, yamlDir);
  26. if (!Directory.Exists(yamlPath))
  27. throw new DirectoryNotFoundException(
  28. $"YAML directory not found: {yamlPath}"
  29. );
  30. // Load all .yml and .yaml files
  31. var yamlFiles = Directory.EnumerateFiles(yamlPath, "*.yml")
  32. .Concat(Directory.EnumerateFiles(yamlPath, "*.yaml"))
  33. .ToArray();
  34. collection.LoadFiles(yamlFiles.Select(f => Path.Combine(basePath, f)));
  35. // Infrastructure
  36. builder.Services.AddScoped<IHardwareRepository>(_ => new YamlHardwareRepository(collection));
  37. builder.Services.AddScoped<ISystemRepository>(_ => new YamlSystemRepository(collection));
  38. builder.Services.AddScoped<IServiceRepository>(_ => new YamlServiceRepository(collection));
  39. builder.Services.AddScoped<IResourceRepository>(_ => new YamlResourceRepository(collection));
  40. builder.Services.AddUseCases();
  41. // Add services to the container.
  42. builder.Services.AddRazorComponents()
  43. .AddInteractiveServerComponents();
  44. var app = builder.Build();
  45. // Configure the HTTP request pipeline.
  46. if (!app.Environment.IsDevelopment())
  47. {
  48. app.UseExceptionHandler("/Error");
  49. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  50. app.UseHsts();
  51. }
  52. app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
  53. app.UseHttpsRedirection();
  54. app.UseStaticFiles();
  55. app.UseAntiforgery();
  56. app.MapStaticAssets();
  57. app.MapRazorComponents<App>()
  58. .AddInteractiveServerRenderMode();
  59. app.Run();
  60. }
  61. }