Program.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 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. var resources = new ResourceCollection();
  44. builder.Services.AddSingleton(resources);
  45. builder.Services.AddScoped<IResourceCollection>(sp =>
  46. new YamlResourceCollection(
  47. yamlFilePath,
  48. sp.GetRequiredService<ITextFileStore>(),
  49. sp.GetRequiredService<ResourceCollection>()));
  50. // Infrastructure
  51. builder.Services.AddScoped<IHardwareRepository, YamlHardwareRepository>();
  52. builder.Services.AddScoped<ISystemRepository, YamlSystemRepository>();
  53. builder.Services.AddScoped<IServiceRepository, YamlServiceRepository>();
  54. builder.Services.AddScoped<IResourceRepository, YamlResourceRepository>();
  55. builder.Services.AddUseCases();
  56. builder.Services.AddCommands();
  57. builder.Services.AddScoped<IConsoleEmulator, ConsoleEmulator>();
  58. // Add services to the container.
  59. builder.Services.AddRazorComponents()
  60. .AddInteractiveServerComponents();
  61. var app = builder.Build();
  62. // Configure the HTTP request pipeline.
  63. if (!app.Environment.IsDevelopment())
  64. {
  65. app.UseExceptionHandler("/Error");
  66. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  67. app.UseHsts();
  68. }
  69. app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
  70. app.UseHttpsRedirection();
  71. app.UseStaticFiles();
  72. app.UseAntiforgery();
  73. app.MapStaticAssets();
  74. app.MapRazorComponents<App>()
  75. .AddInteractiveServerRenderMode();
  76. await app.RunAsync();
  77. }
  78. }