Feature:Asynchronous Operation & Formatting Code

- Now the async operation is enabled!
- Code formatted by Resharper plugin <3
This commit is contained in:
Somebody
2024-07-22 17:12:03 +08:00
parent e983375620
commit 87d228eb79
793 changed files with 34764 additions and 40190 deletions

View File

@@ -1,106 +1,85 @@
using EggLink.DanhengServer.Util;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using System.Net;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using System.IO;
using EggLink.DanhengServer.Util;
using Microsoft.AspNetCore;
namespace EggLink.DanhengServer.WebServer
namespace EggLink.DanhengServer.WebServer;
public class WebProgram
{
public class WebProgram
public static void Main(string[] args, int port, string address)
{
public static void Main(string[] args, int port, string address)
{
BuildWebHost(args, port, address).Start();
}
public static IWebHost BuildWebHost(string[] args, int port, string address)
{
var b = WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging((hostingContext, logging) =>
{
logging.ClearProviders();
})
.UseUrls(address);
if (ConfigManager.Config.HttpServer.UseSSL)
{
b.UseKestrel(options =>
{
options.Listen(IPAddress.Any, port, listenOptions =>
{
listenOptions.UseHttps(
ConfigManager.Config.KeyStore.KeyStorePath,
ConfigManager.Config.KeyStore.KeyStorePassword
);
});
});
}
return b.Build();
}
BuildWebHost(args, port, address).Start();
}
public class Startup
public static IWebHost BuildWebHost(string[] args, int port, string address)
{
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
var b = WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging((hostingContext, logging) => { logging.ClearProviders(); })
.UseUrls(address);
if (ConfigManager.Config.HttpServer.UseSSL)
b.UseKestrel(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
options.Listen(IPAddress.Any, port, listenOptions =>
{
listenOptions.UseHttps(
ConfigManager.Config.KeyStore.KeyStorePath,
ConfigManager.Config.KeyStore.KeyStorePassword
);
});
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Use(async (context, next) =>
{
using var buffer = new MemoryStream();
var request = context.Request;
var response = context.Response;
var bodyStream = response.Body;
response.Body = buffer;
await next.Invoke();
buffer.Position = 0;
context.Response.Headers["Content-Length"] = (response.ContentLength ?? buffer.Length).ToString();
context.Response.Headers.Remove("Transfer-Encoding");
await buffer.CopyToAsync(bodyStream);
});
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("AllowAll");
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
return b.Build();
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
app.Use(async (context, next) =>
{
using var buffer = new MemoryStream();
var request = context.Request;
var response = context.Response;
var bodyStream = response.Body;
response.Body = buffer;
await next.Invoke();
buffer.Position = 0;
context.Response.Headers["Content-Length"] = (response.ContentLength ?? buffer.Length).ToString();
context.Response.Headers.Remove("Transfer-Encoding");
await buffer.CopyToAsync(bodyStream);
});
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("AllowAll");
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}