-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to execute middlewares when using the dashboard
- Loading branch information
Showing
10 changed files
with
254 additions
and
135 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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
39 changes: 39 additions & 0 deletions
39
src/KafkaFlow.Admin.Dashboard/ClientApp/src/app/api-interceptor.service.ts
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,39 @@ | ||
import { | ||
HttpEvent, | ||
HttpInterceptor, | ||
HttpHandler, | ||
HttpRequest, | ||
HttpErrorResponse | ||
} from '@angular/common/http'; | ||
import {Observable, throwError} from 'rxjs'; | ||
import {retry, catchError} from 'rxjs/operators'; | ||
import {CookieService} from 'ngx-cookie-service'; | ||
import {Injectable} from '@angular/core'; | ||
|
||
@Injectable() | ||
export class ApiInterceptor implements HttpInterceptor { | ||
constructor( private cookieService: CookieService ) { } | ||
|
||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
request = request.clone({ | ||
setHeaders: this.cookieService.getAll() | ||
}); | ||
return next.handle(request) | ||
.pipe( | ||
retry(1), | ||
catchError((error: HttpErrorResponse) => { | ||
let errorMessage = ''; | ||
if (error.error instanceof ErrorEvent) { | ||
// client-side error | ||
errorMessage = `Error: ${error.error.message}`; | ||
} else { | ||
// server-side error | ||
errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`; | ||
} | ||
// window.alert(errorMessage); | ||
console.error(errorMessage); | ||
return throwError(errorMessage); | ||
}) | ||
); | ||
} | ||
} |
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
31 changes: 0 additions & 31 deletions
31
src/KafkaFlow.Admin.Dashboard/ClientApp/src/app/http-error.interceptor.ts
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
namespace KafkaFlow.Admin.Dashboard | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
internal class DashboardConfiguration | ||
{ | ||
private readonly List<Type> middlewares; | ||
|
||
public DashboardConfiguration(List<Type> middlewares, Action<IEndpointConventionBuilder> endpointHandler) | ||
{ | ||
this.middlewares = middlewares; | ||
this.EndpointHandler = endpointHandler; | ||
} | ||
|
||
public IReadOnlyCollection<Type> Middlewares => this.middlewares.AsReadOnly(); | ||
|
||
public Action<IEndpointConventionBuilder> EndpointHandler { get; } | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/KafkaFlow.Admin.Dashboard/DashboardConfigurationBuilder.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace KafkaFlow.Admin.Dashboard | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
internal class DashboardConfigurationBuilder : IDashboardConfigurationBuilder | ||
{ | ||
private readonly List<Type> middlewares = new(); | ||
private Action<IEndpointConventionBuilder> endpointHandler = _ => { }; | ||
|
||
public IDashboardConfigurationBuilder UseMiddleware<T>() | ||
{ | ||
this.middlewares.Add(typeof(T)); | ||
return this; | ||
} | ||
|
||
public IDashboardConfigurationBuilder ConfigureEndpoint(Action<IEndpointConventionBuilder> endpointHandler) | ||
{ | ||
this.endpointHandler = endpointHandler; | ||
return this; | ||
} | ||
|
||
public DashboardConfiguration Build() | ||
{ | ||
return new(this.middlewares, this.endpointHandler); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/KafkaFlow.Admin.Dashboard/IDashboardConfigurationBuilder.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace KafkaFlow.Admin.Dashboard | ||
{ | ||
using System; | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
/// <summary> | ||
/// Used to build the dashboard configuration | ||
/// </summary> | ||
public interface IDashboardConfigurationBuilder | ||
{ | ||
/// <summary> | ||
/// Adds a middleware type to the dashboard's request pipeline. | ||
/// </summary> | ||
/// <returns>The <see cref="IDashboardConfigurationBuilder"/> instance.</returns> | ||
IDashboardConfigurationBuilder UseMiddleware<T>(); | ||
|
||
/// <summary> | ||
/// Use this method to configure the dashboard endpoint pipeline. | ||
/// </summary> | ||
/// <param name="endpointHandler">A handler to configure the endpoint</param> | ||
/// <returns>The <see cref="IDashboardConfigurationBuilder"/> instance.</returns> | ||
IDashboardConfigurationBuilder ConfigureEndpoint(Action<IEndpointConventionBuilder> endpointHandler); | ||
} | ||
} |