使用WebApplication
This commit is contained in:
parent
50b1c85b50
commit
7321ff145d
@ -52,7 +52,7 @@ namespace FastGithub.PacketIntercept.Dns
|
|||||||
using var streamReader = new StreamReader(fileStream);
|
using var streamReader = new StreamReader(fileStream);
|
||||||
while (streamReader.EndOfStream == false)
|
while (streamReader.EndOfStream == false)
|
||||||
{
|
{
|
||||||
var line = await streamReader.ReadLineAsync();
|
var line = await streamReader.ReadLineAsync(cancellationToken);
|
||||||
if (this.IsConflictingLine(line))
|
if (this.IsConflictingLine(line))
|
||||||
{
|
{
|
||||||
hasConflicting = true;
|
hasConflicting = true;
|
||||||
|
|||||||
@ -1,14 +1,11 @@
|
|||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using Serilog;
|
|
||||||
using Serilog.Sinks.Network;
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Linq;
|
||||||
|
|
||||||
namespace FastGithub
|
namespace FastGithub
|
||||||
{
|
{
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -18,68 +15,37 @@ namespace FastGithub
|
|||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
ConsoleUtil.DisableQuickEdit();
|
ConsoleUtil.DisableQuickEdit();
|
||||||
CreateHostBuilder(args).Build().Run();
|
|
||||||
|
var contentRoot = Path.GetDirectoryName(Environment.GetCommandLineArgs().First());
|
||||||
|
if (string.IsNullOrEmpty(contentRoot) == false)
|
||||||
|
{
|
||||||
|
Environment.CurrentDirectory = contentRoot;
|
||||||
|
}
|
||||||
|
var options = new WebApplicationOptions
|
||||||
|
{
|
||||||
|
Args = args,
|
||||||
|
ContentRootPath = contentRoot
|
||||||
|
};
|
||||||
|
CreateWebApplication(options).Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建host
|
/// 创建host
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="args"></param>
|
/// <param name="options"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IHostBuilder CreateHostBuilder(string[] args)
|
private static WebApplication CreateWebApplication(WebApplicationOptions options)
|
||||||
{
|
{
|
||||||
return Host
|
var builder = WebApplication.CreateBuilder(options);
|
||||||
.CreateDefaultBuilder(args)
|
builder.ConfigureHost();
|
||||||
.UseSystemd()
|
builder.ConfigureWebHost();
|
||||||
.UseWindowsService()
|
builder.ConfigureConfiguration();
|
||||||
.UseDefaultServiceProvider(c =>
|
builder.ConfigureServices();
|
||||||
{
|
|
||||||
c.ValidateOnBuild = false;
|
|
||||||
})
|
|
||||||
.ConfigureAppConfiguration(c =>
|
|
||||||
{
|
|
||||||
const string APPSETTINGS = "appsettings";
|
|
||||||
if (Directory.Exists(APPSETTINGS) == true)
|
|
||||||
{
|
|
||||||
foreach (var file in Directory.GetFiles(APPSETTINGS, "appsettings.*.json"))
|
|
||||||
{
|
|
||||||
var jsonFile = Path.Combine(APPSETTINGS, Path.GetFileName(file));
|
|
||||||
c.AddJsonFile(jsonFile, true, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.UseSerilog((hosting, logger) =>
|
|
||||||
{
|
|
||||||
var template = "{Timestamp:O} [{Level:u3}]{NewLine}{SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}";
|
|
||||||
logger
|
|
||||||
.ReadFrom.Configuration(hosting.Configuration)
|
|
||||||
.Enrich.FromLogContext()
|
|
||||||
.WriteTo.Console(outputTemplate: template)
|
|
||||||
.WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day, outputTemplate: template);
|
|
||||||
|
|
||||||
var udpLoggerPort = hosting.Configuration.GetValue(nameof(AppOptions.UdpLoggerPort), 38457);
|
var app = builder.Build();
|
||||||
logger.WriteTo.UDPSink(IPAddress.Loopback, udpLoggerPort);
|
app.ConfigureApp();
|
||||||
})
|
return app;
|
||||||
.ConfigureWebHostDefaults(webBuilder =>
|
|
||||||
{
|
|
||||||
webBuilder.UseStartup<Startup>();
|
|
||||||
webBuilder.UseShutdownTimeout(TimeSpan.FromSeconds(1d));
|
|
||||||
webBuilder.UseKestrel(kestrel =>
|
|
||||||
{
|
|
||||||
kestrel.NoLimit();
|
|
||||||
if (OperatingSystem.IsWindows())
|
|
||||||
{
|
|
||||||
kestrel.ListenHttpsReverseProxy();
|
|
||||||
kestrel.ListenHttpReverseProxy();
|
|
||||||
kestrel.ListenSshReverseProxy();
|
|
||||||
kestrel.ListenGitReverseProxy();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
kestrel.ListenHttpProxy();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,13 +38,7 @@ namespace FastGithub
|
|||||||
/// <param name="hostBuilder"></param>
|
/// <param name="hostBuilder"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IHostBuilder UseWindowsService(this IHostBuilder hostBuilder)
|
public static IHostBuilder UseWindowsService(this IHostBuilder hostBuilder)
|
||||||
{
|
{
|
||||||
var contentRoot = Path.GetDirectoryName(Environment.GetCommandLineArgs().First());
|
|
||||||
if (contentRoot != null)
|
|
||||||
{
|
|
||||||
Environment.CurrentDirectory = contentRoot;
|
|
||||||
hostBuilder.UseContentRoot(contentRoot);
|
|
||||||
}
|
|
||||||
return WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(hostBuilder);
|
return WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(hostBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,37 +1,99 @@
|
|||||||
using FastGithub.Configuration;
|
using FastGithub.Configuration;
|
||||||
using FastGithub.FlowAnalyze;
|
using FastGithub.FlowAnalyze;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Serilog;
|
||||||
|
using Serilog.Sinks.Network;
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
namespace FastGithub
|
namespace FastGithub
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Æô¶¯Ïî
|
/// Æô¶¯Ïî
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Startup
|
static class Startup
|
||||||
{
|
{
|
||||||
public IConfiguration Configuration { get; }
|
/// <summary>
|
||||||
|
/// 配置通用主机
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder"></param>
|
||||||
|
public static void ConfigureHost(this WebApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
builder.Host.UseSystemd().UseWindowsService();
|
||||||
|
builder.Host.UseSerilog((hosting, logger) =>
|
||||||
|
{
|
||||||
|
var template = "{Timestamp:O} [{Level:u3}]{NewLine}{SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}";
|
||||||
|
logger
|
||||||
|
.ReadFrom.Configuration(hosting.Configuration)
|
||||||
|
.Enrich.FromLogContext()
|
||||||
|
.WriteTo.Console(outputTemplate: template)
|
||||||
|
.WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day, outputTemplate: template);
|
||||||
|
|
||||||
|
var udpLoggerPort = hosting.Configuration.GetValue(nameof(AppOptions.UdpLoggerPort), 38457);
|
||||||
|
logger.WriteTo.UDPSink(IPAddress.Loopback, udpLoggerPort);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Æô¶¯Ïî
|
/// 配置web主机
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="configuration"></param>
|
/// <param name="builder"></param>
|
||||||
public Startup(IConfiguration configuration)
|
public static void ConfigureWebHost(this WebApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
this.Configuration = configuration;
|
builder.WebHost.UseShutdownTimeout(TimeSpan.FromSeconds(1d));
|
||||||
|
builder.WebHost.UseKestrel(kestrel =>
|
||||||
|
{
|
||||||
|
kestrel.NoLimit();
|
||||||
|
if (OperatingSystem.IsWindows())
|
||||||
|
{
|
||||||
|
kestrel.ListenHttpsReverseProxy();
|
||||||
|
kestrel.ListenHttpReverseProxy();
|
||||||
|
kestrel.ListenSshReverseProxy();
|
||||||
|
kestrel.ListenGitReverseProxy();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
kestrel.ListenHttpProxy();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 配置配置
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder"></param>
|
||||||
|
public static void ConfigureConfiguration(this WebApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
const string APPSETTINGS = "appsettings";
|
||||||
|
if (Directory.Exists(APPSETTINGS) == true)
|
||||||
|
{
|
||||||
|
foreach (var file in Directory.GetFiles(APPSETTINGS, "appsettings.*.json"))
|
||||||
|
{
|
||||||
|
var jsonFile = Path.Combine(APPSETTINGS, Path.GetFileName(file));
|
||||||
|
builder.Configuration.AddJsonFile(jsonFile, true, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// ÅäÖ÷þÎñ
|
/// ÅäÖ÷þÎñ
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="services"></param>
|
/// <param name="builder"></param>
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public static void ConfigureServices(this WebApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
services.Configure<AppOptions>(this.Configuration);
|
var services = builder.Services;
|
||||||
services.Configure<FastGithubOptions>(this.Configuration.GetSection(nameof(FastGithub)));
|
var configuration = builder.Configuration;
|
||||||
|
|
||||||
|
services.Configure<AppOptions>(configuration);
|
||||||
|
services.Configure<FastGithubOptions>(configuration.GetSection(nameof(FastGithub)));
|
||||||
|
|
||||||
services.AddConfiguration();
|
services.AddConfiguration();
|
||||||
services.AddDomainResolve();
|
services.AddDomainResolve();
|
||||||
@ -47,10 +109,10 @@ namespace FastGithub
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// ÅäÖÃÖмä¼þ
|
/// 配置应用
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="app"></param>
|
/// <param name="app"></param>
|
||||||
public void Configure(IApplicationBuilder app)
|
public static void ConfigureApp(this WebApplication app)
|
||||||
{
|
{
|
||||||
app.UseHttpProxyPac();
|
app.UseHttpProxyPac();
|
||||||
app.UseRequestLogging();
|
app.UseRequestLogging();
|
||||||
@ -58,13 +120,11 @@ namespace FastGithub
|
|||||||
|
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
app.DisableRequestLogging();
|
app.DisableRequestLogging();
|
||||||
app.UseEndpoints(endpoint =>
|
|
||||||
|
app.MapGet("/flowStatistics", context =>
|
||||||
{
|
{
|
||||||
endpoint.MapGet("/flowStatistics", context =>
|
var flowStatistics = context.RequestServices.GetRequiredService<IFlowAnalyzer>().GetFlowStatistics();
|
||||||
{
|
return context.Response.WriteAsJsonAsync(flowStatistics);
|
||||||
var flowStatistics = context.RequestServices.GetRequiredService<IFlowAnalyzer>().GetFlowStatistics();
|
|
||||||
return context.Response.WriteAsJsonAsync(flowStatistics);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user