Skip to content

Commit

Permalink
feat: allow configuring the dashboard endpoint and the dashboard requ…
Browse files Browse the repository at this point in the history
…est pipeline
  • Loading branch information
Douglas Lima authored and dougolima committed Aug 11, 2021
1 parent 0c499a9 commit 5e80846
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
22 changes: 9 additions & 13 deletions src/KafkaFlow.Admin.Dashboard/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,37 @@ public static class ApplicationBuilderExtensions
/// <returns></returns>
public static IApplicationBuilder UseKafkaFlowDashboard(this IApplicationBuilder app)
{
return app.UseKafkaFlowDashboard("/kafka-flow");
return app.UseKafkaFlowDashboard((Action<IDashboardConfigurationBuilder>) null);
}

/// <summary>
/// Enable the KafkaFlow dashboard
/// </summary>
/// <param name="app">Instance of <see cref="IApplicationBuilder"/></param>
/// <param name="pathMatch">The request path to match.</param>
/// <param name="pathMatch">Do nothing.</param>
/// <returns></returns>
[Obsolete("This method was deprecated and the pathMatch parameter will not change the dashboard base path.", true)]
public static IApplicationBuilder UseKafkaFlowDashboard(this IApplicationBuilder app, PathString pathMatch)
{
return app.UseKafkaFlowDashboard(pathMatch, null);
return app.UseKafkaFlowDashboard((Action<IDashboardConfigurationBuilder>) null);
}

/// <summary>
/// Enable the KafkaFlow dashboard
/// </summary>
/// <param name="app">Instance of <see cref="IApplicationBuilder"/></param>
/// <param name="pathMatch">The request path to match.</param>
/// <param name="actionBuilder">The handler to be executed after the dashboard url is mapped.</param>
/// <param name="dashboard">A handler to configure the dashboard.</param>
/// <returns></returns>
public static IApplicationBuilder UseKafkaFlowDashboard(
this IApplicationBuilder app,
PathString pathMatch,
Action<IDashboardConfigurationBuilder> actionBuilder)
Action<IDashboardConfigurationBuilder> dashboard)
{
var builder = new DashboardConfigurationBuilder();
actionBuilder?.Invoke(builder);
dashboard?.Invoke(builder);
var configuration = builder.Build();

app.Map(
pathMatch,
configuration.BasePath,
appBuilder =>
{
var provider = new ManifestEmbeddedFileProvider(
Expand All @@ -60,10 +59,7 @@ public static IApplicationBuilder UseKafkaFlowDashboard(
.UseStaticFiles(new StaticFileOptions { FileProvider = provider })
.UseRouting();

foreach (var middleware in configuration.Middlewares)
{
appBuilder.UseMiddleware(middleware);
}
configuration.RequestHandler?.Invoke(appBuilder);

appBuilder.UseEndpoints(routeBuilder =>
{
Expand Down
16 changes: 10 additions & 6 deletions src/KafkaFlow.Admin.Dashboard/DashboardConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
namespace KafkaFlow.Admin.Dashboard
{
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

internal class DashboardConfiguration
{
private readonly List<Type> middlewares;

public DashboardConfiguration(List<Type> middlewares, Action<IEndpointConventionBuilder> endpointHandler)
public DashboardConfiguration(
PathString basePath,
Action<IApplicationBuilder> requestHandler,
Action<IEndpointConventionBuilder> endpointHandler)
{
this.middlewares = middlewares;
this.BasePath = basePath;
this.RequestHandler = requestHandler;
this.EndpointHandler = endpointHandler;
}

public IReadOnlyCollection<Type> Middlewares => this.middlewares.AsReadOnly();
public PathString BasePath { get; }

public Action<IApplicationBuilder> RequestHandler { get; }

public Action<IEndpointConventionBuilder> EndpointHandler { get; }
}
Expand Down
12 changes: 7 additions & 5 deletions src/KafkaFlow.Admin.Dashboard/DashboardConfigurationBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
namespace KafkaFlow.Admin.Dashboard
{
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

internal class DashboardConfigurationBuilder : IDashboardConfigurationBuilder
{
private readonly List<Type> middlewares = new();
private readonly PathString basePath = "/kafka-flow";

private Action<IApplicationBuilder> requestHandler = _ => { };
private Action<IEndpointConventionBuilder> endpointHandler = _ => { };

public IDashboardConfigurationBuilder UseMiddleware<T>()
public IDashboardConfigurationBuilder ConfigureRequestPipeline(Action<IApplicationBuilder> requestHandler)
{
this.middlewares.Add(typeof(T));
this.requestHandler = requestHandler;
return this;
}

Expand All @@ -23,7 +25,7 @@ public IDashboardConfigurationBuilder ConfigureEndpoint(Action<IEndpointConventi

public DashboardConfiguration Build()
{
return new(this.middlewares, this.endpointHandler);
return new(this.basePath, this.requestHandler, this.endpointHandler);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ namespace KafkaFlow.Admin.Dashboard
public interface IDashboardConfigurationBuilder
{
/// <summary>
/// Adds a middleware type to the dashboard's request pipeline.
/// Use this method to configure the dashboard request pipeline
/// </summary>
/// <param name="requestHandler">A handler to configure the request pipeline</param>
/// <returns>The <see cref="IDashboardConfigurationBuilder"/> instance.</returns>
IDashboardConfigurationBuilder UseMiddleware<T>();
IDashboardConfigurationBuilder ConfigureRequestPipeline(Action<IApplicationBuilder> requestHandler);

/// <summary>
/// Use this method to configure the dashboard endpoint pipeline.
/// Use this method to configure the dashboard endpoint
/// </summary>
/// <param name="endpointHandler">A handler to configure the endpoint</param>
/// <returns>The <see cref="IDashboardConfigurationBuilder"/> instance.</returns>
/// <returns>The <see cref="IEndpointConventionBuilder"/> instance.</returns>
IDashboardConfigurationBuilder ConfigureEndpoint(Action<IEndpointConventionBuilder> endpointHandler);
}
}

0 comments on commit 5e80846

Please sign in to comment.