From f50363975e04587d3bc8f28c05f7870c327ebd82 Mon Sep 17 00:00:00 2001 From: SondreB Date: Sun, 4 Dec 2022 22:08:36 +0100 Subject: [PATCH] Migrate Startup and drop duplicate container registration code --- .../Blockcore.Indexer.Cirrus.Tests.csproj | 2 +- src/Blockcore.Indexer.Cirrus/CirrusStartup.cs | 31 +-- .../Blockcore.Indexer.Core.csproj | 2 +- src/Blockcore.Indexer.Core/Startup.cs | 19 +- .../Sync/ComputeHistoryQueue.cs | 7 +- src/Blockcore.Indexer/BlockcoreStartup.cs | 28 +++ src/Blockcore.Indexer/Program.cs | 2 +- src/Blockcore.Indexer/Startup.cs | 216 ------------------ 8 files changed, 42 insertions(+), 265 deletions(-) create mode 100644 src/Blockcore.Indexer/BlockcoreStartup.cs delete mode 100644 src/Blockcore.Indexer/Startup.cs diff --git a/src/Blockcore.Indexer.Cirrus.Tests/Blockcore.Indexer.Cirrus.Tests.csproj b/src/Blockcore.Indexer.Cirrus.Tests/Blockcore.Indexer.Cirrus.Tests.csproj index cdc7551c..d245fae6 100644 --- a/src/Blockcore.Indexer.Cirrus.Tests/Blockcore.Indexer.Cirrus.Tests.csproj +++ b/src/Blockcore.Indexer.Cirrus.Tests/Blockcore.Indexer.Cirrus.Tests.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/Blockcore.Indexer.Cirrus/CirrusStartup.cs b/src/Blockcore.Indexer.Cirrus/CirrusStartup.cs index bf415299..67fed2ac 100644 --- a/src/Blockcore.Indexer.Cirrus/CirrusStartup.cs +++ b/src/Blockcore.Indexer.Cirrus/CirrusStartup.cs @@ -71,8 +71,6 @@ public void ConfigureServices(IServiceCollection services) services.AddTransient,NonFungibleTokenSmartContractTransactionsLookup>(); services.AddTransient(typeof(ISmartContractTransactionsLookup<>), typeof(SmartContractTransactionsLookup<>)); - - ScanAssemblyAndRegisterTypeByNameAsTransient(services, typeof(ILogReader), typeof(ILogReader).Assembly); ScanAssemblyAndRegisterTypeByNameAsTransient(services, typeof(ILogReader), @@ -117,34 +115,7 @@ where type.GetInterface(typeToRegister.Name) != null && public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { - app.UseExceptionHandler("/error"); - - // Enable Cors - app.UseCors("IndexerPolicy"); - - app.UseResponseCompression(); - - app.UseDefaultFiles(); - - app.UseStaticFiles(); - - app.UseRouting(); - - app.UseSwagger(c => - { - c.RouteTemplate = "docs/{documentName}/openapi.json"; - }); - - app.UseSwaggerUI(c => - { - c.RoutePrefix = "docs"; - c.SwaggerEndpoint("/docs/indexer/openapi.json", "Blockcore Indexer API"); - }); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); + Startup.Configure(app, env); } } } diff --git a/src/Blockcore.Indexer.Core/Blockcore.Indexer.Core.csproj b/src/Blockcore.Indexer.Core/Blockcore.Indexer.Core.csproj index c7605401..046eea70 100644 --- a/src/Blockcore.Indexer.Core/Blockcore.Indexer.Core.csproj +++ b/src/Blockcore.Indexer.Core/Blockcore.Indexer.Core.csproj @@ -4,7 +4,7 @@ - + diff --git a/src/Blockcore.Indexer.Core/Startup.cs b/src/Blockcore.Indexer.Core/Startup.cs index 0f1fe049..f8e7f3f7 100644 --- a/src/Blockcore.Indexer.Core/Startup.cs +++ b/src/Blockcore.Indexer.Core/Startup.cs @@ -32,18 +32,6 @@ namespace Blockcore.Indexer.Core { public class Startup { - public IConfiguration Configuration { get; } - - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public void ConfigureServices(IServiceCollection services) - { - AddIndexerServices(services, Configuration); - } - public static void AddIndexerServices(IServiceCollection services, IConfiguration configuration) { services.Configure(configuration.GetSection("Chain")); @@ -94,9 +82,9 @@ public static void AddIndexerServices(IServiceCollection services, IConfiguratio services.AddScoped(); services.AddScoped(); - services.AddScoped(); + // TODO: Verify that it is OK we add this to shared Startup for Blockcore and Cirrus. services.AddScoped(); services.AddSingleton(); @@ -157,9 +145,12 @@ public static void AddIndexerServices(IServiceCollection services, IConfiguratio services.AddTransient(); services.AddSingleton(); services.AddSingleton(); + + // TODO: Verify that it is OK we add this to shared Startup for Blockcore and Cirrus. + services.AddTransient(); } - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public static void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseExceptionHandler("/error"); diff --git a/src/Blockcore.Indexer.Core/Sync/ComputeHistoryQueue.cs b/src/Blockcore.Indexer.Core/Sync/ComputeHistoryQueue.cs index a1d43db3..d0a63825 100644 --- a/src/Blockcore.Indexer.Core/Sync/ComputeHistoryQueue.cs +++ b/src/Blockcore.Indexer.Core/Sync/ComputeHistoryQueue.cs @@ -20,12 +20,15 @@ public ComputeHistoryQueue(IOptions indexerSettings) public void AddAddressToComputeHistoryQueue(string address) { - if (indexerSettings.MaxItemsInHistoryQueue <= 0 || - collection.Count >= indexerSettings.MaxItemsInHistoryQueue) + if (indexerSettings.MaxItemsInHistoryQueue <= 0 || collection.Count >= indexerSettings.MaxItemsInHistoryQueue) + { return; + } if (!collection.Contains(address)) + { collection.Enqueue(address); + } } public bool GetNextItemFromQueue(out string address) diff --git a/src/Blockcore.Indexer/BlockcoreStartup.cs b/src/Blockcore.Indexer/BlockcoreStartup.cs new file mode 100644 index 00000000..7328af69 --- /dev/null +++ b/src/Blockcore.Indexer/BlockcoreStartup.cs @@ -0,0 +1,28 @@ +using Blockcore.Indexer.Core; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Blockcore.Indexer +{ + public class BlockcoreStartup + { + public IConfiguration Configuration { get; } + + public BlockcoreStartup(IConfiguration configuration) + { + Configuration = configuration; + } + + public void ConfigureServices(IServiceCollection services) + { + Startup.AddIndexerServices(services, Configuration); + } + + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + Startup.Configure(app, env); + } + } +} diff --git a/src/Blockcore.Indexer/Program.cs b/src/Blockcore.Indexer/Program.cs index fc4f45c0..56a2aadf 100644 --- a/src/Blockcore.Indexer/Program.cs +++ b/src/Blockcore.Indexer/Program.cs @@ -38,7 +38,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) => serverOptions.AddServerHeader = false; }); - webBuilder.UseStartup(); + webBuilder.UseStartup(); }); } } diff --git a/src/Blockcore.Indexer/Startup.cs b/src/Blockcore.Indexer/Startup.cs deleted file mode 100644 index ddc5bc24..00000000 --- a/src/Blockcore.Indexer/Startup.cs +++ /dev/null @@ -1,216 +0,0 @@ -using System; -using System.IO; -using System.Reflection; -using System.Text.Json; -using System.Text.Json.Serialization; -using Blockcore.Indexer.Core.Client; -using Blockcore.Indexer.Core.Crypto; -using Blockcore.Indexer.Core.Extensions; -using Blockcore.Indexer.Core.Handlers; -using Blockcore.Indexer.Core.Operations; -using Blockcore.Indexer.Core.Operations.Types; -using Blockcore.Indexer.Core.Paging; -using Blockcore.Indexer.Core.Settings; -using Blockcore.Indexer.Core.Storage; -using Blockcore.Indexer.Core.Storage.Mongo; -using Blockcore.Indexer.Core.Sync; -using Blockcore.Indexer.Core.Sync.SyncTasks; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.ApplicationModels; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; -using Microsoft.Extensions.PlatformAbstractions; -using Microsoft.OpenApi.Models; -using MongoDB.Driver; -using Newtonsoft.Json; - -namespace Blockcore.Indexer -{ - public class Startup - { - public IConfiguration Configuration { get; } - - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public void ConfigureServices(IServiceCollection services) - { - AddIndexerServices(services, Configuration); - } - - public static void AddIndexerServices(IServiceCollection services, IConfiguration configuration) - { - services.Configure(configuration.GetSection("Chain")); - services.Configure(configuration.GetSection("Network")); - services.Configure(configuration.GetSection("Indexer")); - services.Configure(configuration.GetSection("Insight")); - - services.AddSingleton(_ => - { - var indexerConfiguration = _.GetService(typeof(IOptions))as IOptions ;// configuration.GetSection("Indexer") as IndexerSettings; - var chainConfiguration = _.GetService(typeof(IOptions)) as IOptions;// configuration.GetSection("Chain") as ChainSettings; - - var mongoClient = new MongoClient(indexerConfiguration.Value.ConnectionString.Replace("{Symbol}", - chainConfiguration.Value.Symbol.ToLower())); - - string dbName = indexerConfiguration.Value.DatabaseNameSubfix - ? $"Blockchain{chainConfiguration.Value.Symbol}" - : "Blockchain"; - - return mongoClient.GetDatabase(dbName); - }); - - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddTransient(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddScoped(); - - services.AddSingleton(); - services.AddSingleton(); - - - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); // Update peer information every 5 minute. - - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - - services.AddScoped(); - - services.AddScoped(); - - services.AddResponseCompression(); - services.AddMemoryCache(); - services.AddHostedService(); - - services.AddControllers(options => - { - options.ModelBinderProviders.Insert(0, new DateTimeModelBinderProvider()); - options.Conventions.Add(new ActionHidingConvention()); - }).AddJsonOptions(options => - { - options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase, - allowIntegerValues: false)); - }).AddNewtonsoftJson(options => - { - options.SerializerSettings.FloatFormatHandling = FloatFormatHandling.DefaultValue; - options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; // Don't include null fields. - }); - - services.AddSwaggerGen( - options => - { - string assemblyVersion = typeof(Startup).Assembly.GetName().Version.ToString(); - - options.SwaggerDoc("indexer", - new OpenApiInfo - { - Title = "Blockcore Indexer API", - Version = assemblyVersion, - Description = "Blockchain index database that can be used for blockchain based software and services.", - Contact = new OpenApiContact - { - Name = "Blockcore", - Url = new Uri("https://www.blockcore.net/") - } - }); - - // integrate xml comments - if (File.Exists(XmlCommentsFilePath)) - { - options.IncludeXmlComments(XmlCommentsFilePath); - } - - options.EnableAnnotations(); - }); - - services.AddSwaggerGenNewtonsoftSupport(); // explicit opt-in - needs to be placed after AddSwaggerGen() - - services.AddCors(o => o.AddPolicy("IndexerPolicy", builder => - { - builder.AllowAnyOrigin() - .AllowAnyMethod() - .AllowAnyHeader(); - })); - - services.AddTransient(); - services.AddSingleton(); - services.AddSingleton(); - services.AddTransient(); - } - - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - app.UseExceptionHandler("/error"); - - // Enable Cors - app.UseCors("IndexerPolicy"); - - app.UseResponseCompression(); - - //app.UseMvc(); - - app.UseDefaultFiles(); - - app.UseStaticFiles(); - - app.UseRouting(); - - app.UseSwagger(c => - { - c.RouteTemplate = "docs/{documentName}/openapi.json"; - }); - - app.UseSwaggerUI(c => - { - c.RoutePrefix = "docs"; - c.SwaggerEndpoint("/docs/indexer/openapi.json", "Blockcore Indexer API"); - }); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); - } - - private static string XmlCommentsFilePath - { - get - { - string basePath = PlatformServices.Default.Application.ApplicationBasePath; - string fileName = typeof(Startup).GetTypeInfo().Assembly.GetName().Name + ".xml"; - return Path.Combine(basePath, fileName); - } - } - - /// - /// Hide Stratis related endpoints in Swagger shown due to using Nuget packages - /// in WebApi project for serialization. - /// - public class ActionHidingConvention : IActionModelConvention - { - public void Apply(ActionModel action) - { - // Replace with any logic you want - if (!action.Controller.DisplayName.Contains("Blockcore.Indexer")) - { - action.ApiExplorer.IsVisible = false; - } - } - } - } -}