Program.cs 2.5 KB

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