mhf-server/Mhf.Server/Common/Middleware/MiddlewareStack.cs
2019-11-24 19:43:35 +08:00

35 lines
1.0 KiB
C#

using System;
namespace Mhf.Server.Common.Middleware
{
/// <summary>
/// Implementation of a middleware
/// </summary>
public class MiddlewareStack<T, TReq, TRes>
{
private MiddlewareDelegate<T, TReq, TRes> _middlewareDelegate;
public MiddlewareStack(MiddlewareDelegate<T, TReq, TRes> kernel)
{
_middlewareDelegate = kernel;
}
public void Start(T user, TReq request, TRes response)
{
_middlewareDelegate(user, request, response);
}
public MiddlewareStack<T, TReq, TRes> Use(
Func<MiddlewareDelegate<T, TReq, TRes>, MiddlewareDelegate<T, TReq, TRes>> middleware)
{
_middlewareDelegate = middleware(_middlewareDelegate);
return this;
}
public MiddlewareStack<T, TReq, TRes> Use(IMiddleware<T, TReq, TRes> middleware)
{
return Use(next => (user, request, response) => middleware.Handle(user, request, response, next));
}
}
}