From 419744d0364d24b015f109ecc3f022b700f4f59b Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Wed, 23 Dec 2020 01:40:27 +0200 Subject: [PATCH] agent infrastructure ready --- NetworkResurrector.Agent/Program.cs | 78 ++++++++++++++++++++--- NetworkResurrector.Agent/Startup.cs | 57 +++++++++++++---- NetworkResurrector.Agent/appsettings.json | 5 +- NetworkResurrector.Api/Program.cs | 1 - 4 files changed, 117 insertions(+), 24 deletions(-) diff --git a/NetworkResurrector.Agent/Program.cs b/NetworkResurrector.Agent/Program.cs index fe10858..066127f 100644 --- a/NetworkResurrector.Agent/Program.cs +++ b/NetworkResurrector.Agent/Program.cs @@ -1,11 +1,14 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Core; +using Serilog.Events; +using Serilog.Sinks.MSSqlServer; using System; -using System.Collections.Generic; +using System.Diagnostics; +using System.IO; using System.Linq; -using System.Threading.Tasks; namespace NetworkResurrector.Agent { @@ -13,14 +16,69 @@ namespace NetworkResurrector.Agent { public static void Main(string[] args) { - CreateHostBuilder(args).Build().Run(); + var isConsole = Debugger.IsAttached || args.Contains("--console"); + if (!isConsole) + { + var pathToExe = Process.GetCurrentProcess().MainModule.FileName; + var pathToContentRoot = Path.GetDirectoryName(pathToExe); + Directory.SetCurrentDirectory(pathToContentRoot); + } + + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .Build(); + + var connectionString = configuration.GetConnectionString("DatabaseConnection"); + var loggingLevelParam = configuration.GetValue("Logging:LogLevel:Default"); + + Enum.TryParse(loggingLevelParam, out LogEventLevel loggingLevel); + var loggingLevelSwitch = new LoggingLevelSwitch(loggingLevel); + + var columnOptions = new ColumnOptions(); + columnOptions.Store.Remove(StandardColumn.Properties); + columnOptions.Store.Remove(StandardColumn.MessageTemplate); + columnOptions.Store.Add(StandardColumn.LogEvent); + Log.Logger = new LoggerConfiguration() + .MinimumLevel.ControlledBy(loggingLevelSwitch) + .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) + .Enrich.FromLogContext() + .WriteTo.Console() + .WriteTo.MSSqlServer(connectionString, "__Logs", autoCreateSqlTable: true, columnOptions: columnOptions) + .CreateLogger(); + + try + { + var urls = configuration.GetValue("urls"); + Log.Information("Starting network resurrector agent..."); + Log.Information($"Agent API listening on {urls}"); + Console.WriteLine("Application started. Press Ctrl+C to shut down."); + CreateHostBuilder(args, configuration, !isConsole).Build().Run(); + } + catch (Exception ex) + { + Log.Fatal(ex, "Network resurrector agent host terminated unexpectedly"); + } + finally + { + Log.CloseAndFlush(); + } } - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); + public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration, bool useWindowsService) + { + var builder = Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup() + .UseConfiguration(configuration) + .UseSerilog(); + }); + + if (useWindowsService) + builder.UseWindowsService(); + + return builder; + } } } diff --git a/NetworkResurrector.Agent/Startup.cs b/NetworkResurrector.Agent/Startup.cs index 6ded2be..0e74282 100644 --- a/NetworkResurrector.Agent/Startup.cs +++ b/NetworkResurrector.Agent/Startup.cs @@ -1,48 +1,81 @@ +using AutoMapper; +using MediatR; +using MediatR.Pipeline; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using NDB.Extensions.Swagger; +using NDB.Security.Authentication.Identity; +using NetworkResurrector.Agent.Application; +using Newtonsoft.Json; +using System.Reflection; namespace NetworkResurrector.Agent { public class Startup { + private readonly IConfiguration _configuration; + public Startup(IConfiguration configuration) { - Configuration = configuration; + _configuration = configuration; } - public IConfiguration Configuration { get; } - // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddControllers(); + services.AddControllers() + .AddNewtonsoftJson(o => o.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc); + + // Add basic authentication + services.AddBasicAuthentication(_configuration.GetSection("IdentityServer")["BaseAddress"]); + + // MediatR + services.AddMediatR(GetMediatRAssemblies()); + services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>)); + services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>)); + + // AutoMapper + services.AddAutoMapper( + typeof(Application.Mappings.MappingProfile).Assembly); + + // Swagger + services.AddSwagger("NetworkResurrectorAgent API"); + + // Application + services.AddApplicationServices(); + } + + private Assembly[] GetMediatRAssemblies() + { + var assembly = typeof(Application.Queries.GetMachines).Assembly; + return new Assembly[] { assembly }; } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { + // global cors policy + app.UseCors(x => x + .AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader()); + if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); - + app.UseAuthentication(); app.UseAuthorization(); - app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); + app.ConfigureSwagger("NetworkResurrectorAgent API"); } } } diff --git a/NetworkResurrector.Agent/appsettings.json b/NetworkResurrector.Agent/appsettings.json index 96ecf3e..128e5f7 100644 --- a/NetworkResurrector.Agent/appsettings.json +++ b/NetworkResurrector.Agent/appsettings.json @@ -10,5 +10,8 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "IdentityServer": { + "BaseAddress": "http://localhost:5063/" + } } diff --git a/NetworkResurrector.Api/Program.cs b/NetworkResurrector.Api/Program.cs index 728d626..698d6b8 100644 --- a/NetworkResurrector.Api/Program.cs +++ b/NetworkResurrector.Api/Program.cs @@ -63,7 +63,6 @@ namespace NetworkResurrector.Api { Log.CloseAndFlush(); } - } public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration, bool useWindowsService)