This commit is contained in:
Bob.Song
2026-03-27 17:55:04 +08:00
parent a924cd9c01
commit d5dafd2bcf
11 changed files with 114 additions and 200 deletions

View File

@@ -0,0 +1,66 @@
using Fantasy.Async;
using Fantasy.Event;
using Fantasy.Network.HTTP;
using Microsoft.AspNetCore.Builder;
using System;
using Fantasy;
namespace NBF
{
public class GameHttpApplicationHandler : AsyncEventSystem<OnConfigureHttpApplication>
{
protected override async FTask Handler(OnConfigureHttpApplication self)
{
var app = self.Application;
// 1. CORS必须在认证之前
app.UseCors("GameClient");
// 2. 请求日志中间件
app.Use(async (context, next) =>
{
var requestId = Guid.NewGuid().ToString("N");
context.Items["RequestId"] = requestId;
var start = DateTime.UtcNow;
var method = context.Request.Method;
var path = context.Request.Path;
var ip = context.Connection.RemoteIpAddress?.ToString();
Log.Info($"[HTTP-{requestId}] {method} {path} - IP: {ip}");
try
{
await next.Invoke();
var duration = (DateTime.UtcNow - start).TotalMilliseconds;
var status = context.Response.StatusCode;
Log.Info($"[HTTP-{requestId}] {status} - {duration}ms");
}
catch (Exception e)
{
Log.Error($"[HTTP-{requestId}] 异常: {e.Message}");
throw;
}
});
// 3. 认证
app.UseAuthentication();
// 4. 授权
app.UseAuthorization();
// 5. 自定义响应头
app.Use(async (context, next) =>
{
context.Response.Headers.Add("X-Game-Server", "NBF");
context.Response.Headers.Add("X-Server-Version", "1.0.0");
await next.Invoke();
});
Log.Info($"[HTTP] 游戏应用配置完成: Scene {self.Scene.SceneConfigId}");
await FTask.CompletedTask;
}
}
}