Program.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Text.Json.Serialization;
  2. using Microsoft.AspNetCore.Components;
  3. using Microsoft.AspNetCore.Hosting.StaticWebAssets;
  4. using RackPeek.Domain;
  5. using RackPeek.Domain.Git;
  6. using RackPeek.Domain.Persistence;
  7. using RackPeek.Domain.Persistence.Yaml;
  8. using RackPeek.Web.Api;
  9. using RackPeek.Web.Components;
  10. using Shared.Rcl;
  11. namespace RackPeek.Web;
  12. public class Program {
  13. public static async Task<WebApplication> BuildApp(WebApplicationBuilder builder) {
  14. StaticWebAssetsLoader.UseStaticWebAssets(
  15. builder.Environment,
  16. builder.Configuration
  17. );
  18. var yamlDir = builder.Configuration.GetValue<string>("RPK_YAML_DIR") ?? "./config";
  19. var yamlFileName = "config.yaml";
  20. var basePath = Directory.GetCurrentDirectory();
  21. var yamlPath = Path.IsPathRooted(yamlDir)
  22. ? yamlDir
  23. : Path.Combine(basePath, yamlDir);
  24. Directory.CreateDirectory(yamlPath);
  25. var yamlFilePath = Path.Combine(yamlPath, yamlFileName);
  26. if (!File.Exists(yamlFilePath)) {
  27. await using var fs = new FileStream(
  28. yamlFilePath,
  29. FileMode.CreateNew,
  30. FileAccess.Write,
  31. FileShare.None);
  32. await using var writer = new StreamWriter(fs);
  33. await writer.WriteLineAsync("# default config");
  34. }
  35. builder.Services.ConfigureHttpJsonOptions(options => {
  36. options.SerializerOptions.Converters.Add(
  37. new JsonStringEnumConverter());
  38. });
  39. builder.Services.AddScoped<ITextFileStore, PhysicalTextFileStore>();
  40. builder.Services.AddScoped(sp => {
  41. NavigationManager nav = sp.GetRequiredService<NavigationManager>();
  42. return new HttpClient {
  43. BaseAddress = new Uri(nav.BaseUri)
  44. };
  45. });
  46. builder.Services.AddGitServices(builder.Configuration, yamlPath);
  47. var resources = new ResourceCollection();
  48. builder.Services.AddSingleton(resources);
  49. builder.Services.AddScoped<RackPeekConfigMigrationDeserializer>();
  50. builder.Services.AddScoped<IResourceYamlMigrationService, ResourceYamlMigrationService>();
  51. builder.Services.AddScoped<IResourceCollection>(sp =>
  52. new YamlResourceCollection(
  53. yamlFilePath,
  54. sp.GetRequiredService<ITextFileStore>(),
  55. sp.GetRequiredService<ResourceCollection>(),
  56. sp.GetRequiredService<IResourceYamlMigrationService>()));
  57. // Infrastructure
  58. builder.Services.AddYamlRepos();
  59. builder.Services.AddUseCases();
  60. builder.Services.AddCommands();
  61. builder.Services.AddScoped<IConsoleEmulator, ConsoleEmulator>();
  62. // Razor Components
  63. builder.Services.AddRazorComponents()
  64. .AddInteractiveServerComponents();
  65. WebApplication app = builder.Build();
  66. if (!app.Environment.IsDevelopment()) {
  67. app.UseExceptionHandler("/Error");
  68. app.UseHsts();
  69. }
  70. app.UseHttpsRedirection();
  71. app.UseStaticFiles();
  72. app.UseRouting();
  73. app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
  74. app.UseAntiforgery();
  75. app.MapInventoryApi();
  76. app.MapStaticAssets();
  77. app.MapRazorComponents<App>()
  78. .AddInteractiveServerRenderMode();
  79. return app;
  80. }
  81. public static async Task Main(string[] args) {
  82. WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
  83. WebApplication app = await BuildApp(builder);
  84. await app.RunAsync();
  85. }
  86. }