Program.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. try {
  28. await using var fs = new FileStream(
  29. yamlFilePath,
  30. FileMode.CreateNew,
  31. FileAccess.Write,
  32. FileShare.None);
  33. await using var writer = new StreamWriter(fs);
  34. await writer.WriteLineAsync("# default config");
  35. }
  36. catch (IOException) when (File.Exists(yamlFilePath)) {
  37. // Another instance created the file between the existence
  38. // check and CreateNew — the config is there, carry on.
  39. }
  40. }
  41. builder.Services.ConfigureHttpJsonOptions(options => {
  42. options.SerializerOptions.Converters.Add(
  43. new JsonStringEnumConverter());
  44. });
  45. builder.Services.AddScoped<ITextFileStore, PhysicalTextFileStore>();
  46. builder.Services.AddScoped(sp => {
  47. NavigationManager nav = sp.GetRequiredService<NavigationManager>();
  48. return new HttpClient {
  49. BaseAddress = new Uri(nav.BaseUri)
  50. };
  51. });
  52. builder.Services.AddGitServices(builder.Configuration, yamlPath);
  53. var resources = new ResourceCollection();
  54. builder.Services.AddSingleton(resources);
  55. builder.Services.AddScoped<RackPeekConfigMigrationDeserializer>();
  56. builder.Services.AddScoped<IResourceYamlMigrationService, ResourceYamlMigrationService>();
  57. builder.Services.AddScoped<IResourceCollection>(sp =>
  58. new YamlResourceCollection(
  59. yamlFilePath,
  60. sp.GetRequiredService<ITextFileStore>(),
  61. sp.GetRequiredService<ResourceCollection>(),
  62. sp.GetRequiredService<IResourceYamlMigrationService>()));
  63. // Infrastructure
  64. builder.Services.AddYamlRepos();
  65. builder.Services.AddUseCases();
  66. builder.Services.AddCommands();
  67. builder.Services.AddScoped<IConsoleEmulator, ConsoleEmulator>();
  68. // Razor Components
  69. builder.Services.AddRazorComponents()
  70. .AddInteractiveServerComponents();
  71. WebApplication app = builder.Build();
  72. if (!app.Environment.IsDevelopment()) {
  73. app.UseExceptionHandler("/Error");
  74. app.UseHsts();
  75. }
  76. app.UseHttpsRedirection();
  77. app.UseStaticFiles();
  78. app.UseRouting();
  79. app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
  80. app.UseAntiforgery();
  81. app.MapInventoryApi();
  82. app.MapStaticAssets();
  83. app.MapRazorComponents<App>()
  84. .AddInteractiveServerRenderMode();
  85. return app;
  86. }
  87. public static async Task Main(string[] args) {
  88. WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
  89. WebApplication app = await BuildApp(builder);
  90. await app.RunAsync();
  91. }
  92. }