From bb0cee70b77a235ebc10ecec7b533690dc562b5a Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Mon, 6 Dec 2021 13:04:02 +0200 Subject: [PATCH] NetworkResurrector.Agent.Application --- NetworkResurrector.sln | 9 ++- .../Mappings/MappingProfile.cs | 12 ++++ ...etworkResurrector.Agent.Application.csproj | 16 +++++ .../Queries/GetSystemVersion.cs | 40 ++++++++++++ .../NetworkResurrector.Agent.csproj | 4 ++ src/agent/NetworkResurrector.Agent/Startup.cs | 64 +++++++++++++------ 6 files changed, 126 insertions(+), 19 deletions(-) create mode 100644 src/agent/NetworkResurrector.Agent.Application/Mappings/MappingProfile.cs create mode 100644 src/agent/NetworkResurrector.Agent.Application/NetworkResurrector.Agent.Application.csproj create mode 100644 src/agent/NetworkResurrector.Agent.Application/Queries/GetSystemVersion.cs diff --git a/NetworkResurrector.sln b/NetworkResurrector.sln index 3cedfdf..89e5d9e 100644 --- a/NetworkResurrector.sln +++ b/NetworkResurrector.sln @@ -52,7 +52,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "api", "api", "{C5096F61-731 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "agent", "agent", "{C04663A1-E0CD-41D6-A8D7-FADDF9DA8302}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetworkResurrector.Agent", "src\agent\NetworkResurrector.Agent\NetworkResurrector.Agent.csproj", "{C8C4CA6F-39E2-46FE-89E2-0A81D2F4161E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Agent", "src\agent\NetworkResurrector.Agent\NetworkResurrector.Agent.csproj", "{C8C4CA6F-39E2-46FE-89E2-0A81D2F4161E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetworkResurrector.Agent.Application", "src\agent\NetworkResurrector.Agent.Application\NetworkResurrector.Agent.Application.csproj", "{3795AB02-7F2A-424B-BFD2-1B915E155829}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -104,6 +106,10 @@ Global {C8C4CA6F-39E2-46FE-89E2-0A81D2F4161E}.Debug|Any CPU.Build.0 = Debug|Any CPU {C8C4CA6F-39E2-46FE-89E2-0A81D2F4161E}.Release|Any CPU.ActiveCfg = Release|Any CPU {C8C4CA6F-39E2-46FE-89E2-0A81D2F4161E}.Release|Any CPU.Build.0 = Release|Any CPU + {3795AB02-7F2A-424B-BFD2-1B915E155829}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3795AB02-7F2A-424B-BFD2-1B915E155829}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3795AB02-7F2A-424B-BFD2-1B915E155829}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3795AB02-7F2A-424B-BFD2-1B915E155829}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -125,6 +131,7 @@ Global {C5096F61-7319-453B-AA28-7A1A854A6220} = {DF5CBFF4-B348-4D4C-A6D4-B65BA1D64CEF} {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} = {B0C5F0C1-0BF8-4651-AAFC-BE01F516D7B8} {C8C4CA6F-39E2-46FE-89E2-0A81D2F4161E} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} + {3795AB02-7F2A-424B-BFD2-1B915E155829} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {351D76E9-FE02-4C30-A464-7B29AFC64BC7} diff --git a/src/agent/NetworkResurrector.Agent.Application/Mappings/MappingProfile.cs b/src/agent/NetworkResurrector.Agent.Application/Mappings/MappingProfile.cs new file mode 100644 index 0000000..ea18817 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/Mappings/MappingProfile.cs @@ -0,0 +1,12 @@ +using AutoMapper; + +namespace NetworkResurrector.Agent.Application.Mappings +{ + public class MappingProfile : Profile + { + public MappingProfile() + { + //CreateMap(); + } + } +} diff --git a/src/agent/NetworkResurrector.Agent.Application/NetworkResurrector.Agent.Application.csproj b/src/agent/NetworkResurrector.Agent.Application/NetworkResurrector.Agent.Application.csproj new file mode 100644 index 0000000..7aeb85a --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/NetworkResurrector.Agent.Application.csproj @@ -0,0 +1,16 @@ + + + + net5.0 + + + + + + + + + + + + diff --git a/src/agent/NetworkResurrector.Agent.Application/Queries/GetSystemVersion.cs b/src/agent/NetworkResurrector.Agent.Application/Queries/GetSystemVersion.cs new file mode 100644 index 0000000..1e50f34 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/Queries/GetSystemVersion.cs @@ -0,0 +1,40 @@ +using MediatR; +using NDB.Application.DataContracts; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace NetworkResurrector.Agent.Application.Queries +{ + public class GetSystemVersion + { + public class Query : Query + { + public Query() { } + } + + public class Model + { + public string Version { get; set; } + public DateTime LastUpdateDate { get; set; } + } + + public class QueryHandler : IRequestHandler + { + public QueryHandler() + { + } + + public async Task Handle(Query request, CancellationToken cancellationToken) + { + var result = new Model() + { + Version = "1.0.0", + LastUpdateDate = DateTime.Now + }; + + return await Task.FromResult(result); + } + } + } +} diff --git a/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj b/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj index 3c60e0d..091714a 100644 --- a/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj +++ b/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj @@ -21,4 +21,8 @@ + + + + diff --git a/src/agent/NetworkResurrector.Agent/Startup.cs b/src/agent/NetworkResurrector.Agent/Startup.cs index 3aab1cf..1fc6f6f 100644 --- a/src/agent/NetworkResurrector.Agent/Startup.cs +++ b/src/agent/NetworkResurrector.Agent/Startup.cs @@ -1,56 +1,84 @@ +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 Microsoft.OpenApi.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using NDB.Extensions.Swagger; +using NDB.Extensions.Swagger.Constants; +using NDB.Security.Authentication.Identity; +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; } + private string AgentCode => _configuration.GetValue("Agent:Code"); // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + services.AddControllers() + .AddNewtonsoftJson(o => o.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc); - services.AddControllers(); - services.AddSwaggerGen(c => - { - c.SwaggerDoc("v1", new OpenApiInfo { Title = "NetworkResurrector.Agent", Version = "v1" }); - }); + // Add basic authentication + services.AddIdentityAuthentication(_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 {AgentCode}", AuthorizationType.InhouseIdentity); + + // Data access + // services.AddDataAccess(); + + // Application + // services.AddApplicationServices(); + } + + private Assembly[] GetMediatRAssemblies() + { + var assembly = typeof(Application.Queries.GetSystemVersion).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.UseSwagger(); - app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "NetworkResurrector.Agent v1")); } app.UseRouting(); - + app.UseAuthentication(); app.UseAuthorization(); - app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); + app.ConfigureSwagger($"NetworkResurrectorAgent {AgentCode}"); } } }