agent infrastructure ready
parent
98e0a9a830
commit
419744d036
|
@ -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<string>("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<string>("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<Startup>();
|
||||
});
|
||||
public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration, bool useWindowsService)
|
||||
{
|
||||
var builder = Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>()
|
||||
.UseConfiguration(configuration)
|
||||
.UseSerilog();
|
||||
});
|
||||
|
||||
if (useWindowsService)
|
||||
builder.UseWindowsService();
|
||||
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,5 +10,8 @@
|
|||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"IdentityServer": {
|
||||
"BaseAddress": "http://localhost:5063/"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -63,7 +63,6 @@ namespace NetworkResurrector.Api
|
|||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration, bool useWindowsService)
|
||||
|
|
Loading…
Reference in New Issue