forked from shiftwinting/FastGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
270 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
FastGithub.Scanner/IGithubScanMiddleware.cs → FastGithub.Core/IMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace FastGithub.Scanner | ||
namespace FastGithub | ||
{ | ||
/// <summary> | ||
/// 定义中间件的接口 | ||
/// </summary> | ||
interface IGithubScanMiddleware | ||
/// </summary> | ||
/// <typeparam name="TContext"></typeparam> | ||
public interface IMiddleware<TContext> | ||
{ | ||
/// <summary> | ||
/// 执行中间件 | ||
/// </summary> | ||
/// <param name="context">上下文</param> | ||
/// <param name="next">下一个中间件</param> | ||
/// <returns></returns> | ||
Task InvokeAsync(GithubContext context, Func<Task> next); | ||
Task InvokeAsync(TContext context, Func<Task> next); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace FastGithub | ||
{ | ||
/// <summary> | ||
/// 定义中间件管道创建者的接口 | ||
/// </summary> | ||
/// <typeparam name="TContext">中间件上下文</typeparam> | ||
public interface IPipelineBuilder<TContext> | ||
{ | ||
/// <summary> | ||
/// 获取服务提供者 | ||
/// </summary> | ||
IServiceProvider AppServices { get; } | ||
|
||
/// <summary> | ||
/// 使用中间件 | ||
/// </summary> | ||
/// <typeparam name="TContext"></typeparam> | ||
/// <typeparam name="TMiddleware"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <returns></returns> | ||
IPipelineBuilder<TContext> Use<TMiddleware>() where TMiddleware : class, IMiddleware<TContext>; | ||
|
||
/// <summary> | ||
/// 使用中间件 | ||
/// </summary> | ||
/// <typeparam name="TContext"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="middleware"></param> | ||
/// <returns></returns> | ||
IPipelineBuilder<TContext> Use(Func<TContext, Func<Task>, Task> middleware); | ||
|
||
/// <summary> | ||
/// 使用中间件 | ||
/// </summary> | ||
/// <param name="middleware">中间件</param> | ||
/// <returns></returns> | ||
IPipelineBuilder<TContext> Use(Func<InvokeDelegate<TContext>, InvokeDelegate<TContext>> middleware); | ||
|
||
/// <summary> | ||
/// 创建所有中间件执行处理者 | ||
/// </summary> | ||
/// <returns></returns> | ||
InvokeDelegate<TContext> Build(); | ||
|
||
/// <summary> | ||
/// 使用默认配制创建新的PipelineBuilder | ||
/// </summary> | ||
/// <returns></returns> | ||
IPipelineBuilder<TContext> New(); | ||
} | ||
} |
7 changes: 4 additions & 3 deletions
7
FastGithub.Scanner/GithubScanDelegate.cs → FastGithub.Core/InvokeDelegate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace FastGithub.Scanner | ||
namespace FastGithub | ||
{ | ||
/// <summary> | ||
/// 表示所有中间件执行委托 | ||
/// </summary> | ||
/// </summary> | ||
/// <typeparam name="TContext">中间件上下文类型</typeparam> | ||
/// <param name="context">中间件上下文</param> | ||
/// <returns></returns> | ||
delegate Task GithubScanDelegate(GithubContext context); | ||
public delegate Task InvokeDelegate<TContext>(TContext context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace FastGithub | ||
{ | ||
/// <summary> | ||
/// 表示中间件创建者 | ||
/// </summary> | ||
public class PipelineBuilder<TContext> : IPipelineBuilder<TContext> | ||
{ | ||
private readonly InvokeDelegate<TContext> completedHandler; | ||
private readonly List<Func<InvokeDelegate<TContext>, InvokeDelegate<TContext>>> middlewares = new List<Func<InvokeDelegate<TContext>, InvokeDelegate<TContext>>>(); | ||
|
||
/// <summary> | ||
/// 获取服务提供者 | ||
/// </summary> | ||
public IServiceProvider AppServices { get; } | ||
|
||
/// <summary> | ||
/// 中间件创建者 | ||
/// </summary> | ||
/// <param name="appServices"></param> | ||
public PipelineBuilder(IServiceProvider appServices) | ||
: this(appServices, context => Task.CompletedTask) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// 中间件创建者 | ||
/// </summary> | ||
/// <param name="appServices"></param> | ||
/// <param name="completedHandler">完成执行内容处理者</param> | ||
public PipelineBuilder(IServiceProvider appServices, InvokeDelegate<TContext> completedHandler) | ||
{ | ||
this.AppServices = appServices; | ||
this.completedHandler = completedHandler; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// 使用中间件 | ||
/// </summary> | ||
/// <typeparam name="TContext"></typeparam> | ||
/// <typeparam name="TMiddleware"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <returns></returns> | ||
public IPipelineBuilder<TContext> Use<TMiddleware>() where TMiddleware : class, IMiddleware<TContext> | ||
{ | ||
var middleware = this.AppServices.GetRequiredService<TMiddleware>(); | ||
return this.Use(middleware.InvokeAsync); | ||
} | ||
|
||
/// <summary> | ||
/// 使用中间件 | ||
/// </summary> | ||
/// <typeparam name="TContext"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="middleware"></param> | ||
/// <returns></returns> | ||
public IPipelineBuilder<TContext> Use(Func<TContext, Func<Task>, Task> middleware) | ||
{ | ||
return this.Use(next => context => middleware(context, () => next(context))); | ||
} | ||
|
||
/// <summary> | ||
/// 使用中间件 | ||
/// </summary> | ||
/// <param name="middleware"></param> | ||
/// <returns></returns> | ||
public IPipelineBuilder<TContext> Use(Func<InvokeDelegate<TContext>, InvokeDelegate<TContext>> middleware) | ||
{ | ||
this.middlewares.Add(middleware); | ||
return this; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// 创建所有中间件执行处理者 | ||
/// </summary> | ||
/// <returns></returns> | ||
public InvokeDelegate<TContext> Build() | ||
{ | ||
var handler = this.completedHandler; | ||
for (var i = this.middlewares.Count - 1; i >= 0; i--) | ||
{ | ||
handler = this.middlewares[i](handler); | ||
} | ||
return handler; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// 使用默认配制创建新的PipelineBuilder | ||
/// </summary> | ||
/// <returns></returns> | ||
public IPipelineBuilder<TContext> New() | ||
{ | ||
return new PipelineBuilder<TContext>(this.AppServices, this.completedHandler); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
|
||
namespace FastGithub | ||
{ | ||
/// <summary> | ||
/// 中间件创建者扩展 | ||
/// </summary> | ||
public static class PipelineBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// 中断执行中间件 | ||
/// </summary> | ||
/// <typeparam name="TContext"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="handler">处理者</param> | ||
/// <returns></returns> | ||
public static IPipelineBuilder<TContext> Run<TContext>(this IPipelineBuilder<TContext> builder, InvokeDelegate<TContext> handler) | ||
{ | ||
return builder.Use(_ => handler); | ||
} | ||
|
||
/// <summary> | ||
/// 条件中间件 | ||
/// </summary> | ||
/// <typeparam name="TContext"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="predicate"></param> | ||
/// <param name="handler"></param> | ||
/// <returns></returns> | ||
public static IPipelineBuilder<TContext> When<TContext>(this IPipelineBuilder<TContext> builder, Func<TContext, bool> predicate, InvokeDelegate<TContext> handler) | ||
{ | ||
return builder.Use(next => async context => | ||
{ | ||
if (predicate.Invoke(context) == true) | ||
{ | ||
await handler.Invoke(context); | ||
} | ||
else | ||
{ | ||
await next(context); | ||
} | ||
}); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// 条件中间件 | ||
/// </summary> | ||
/// <typeparam name="TContext"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="predicate"></param> | ||
/// <param name="configureAction"></param> | ||
/// <returns></returns> | ||
public static IPipelineBuilder<TContext> When<TContext>(this IPipelineBuilder<TContext> builder, Func<TContext, bool> predicate, Action<IPipelineBuilder<TContext>> configureAction) | ||
{ | ||
return builder.Use(next => async context => | ||
{ | ||
if (predicate.Invoke(context) == true) | ||
{ | ||
var branchBuilder = builder.New(); | ||
configureAction(branchBuilder); | ||
await branchBuilder.Build().Invoke(context); | ||
} | ||
else | ||
{ | ||
await next(context); | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.