Files
Fishing2Server/Hotfix/Api/GameHttpApplicationHandler.cs
Bob.Song a924cd9c01 fkkkkk
2026-03-26 18:01:04 +08:00

66 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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", "Fantasy");
context.Response.Headers.Add("X-Server-Version", "1.0.0");
await next.Invoke();
});
Log.Info($"[HTTP] 游戏应用配置完成: Scene {self.Scene.SceneConfigId}");
await FTask.CompletedTask;
}
}
}