-
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
Douglas Lima
committed
Aug 4, 2021
1 parent
803cf31
commit db2bff4
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> routeMappedHandler) | ||
{ | ||
this.middlewares = middlewares; | ||
this.RouteMappedHandler = routeMappedHandler; | ||
} | ||
|
||
public IReadOnlyCollection<Type> Middlewares => this.middlewares.AsReadOnly(); | ||
|
||
public Action<IEndpointConventionBuilder> RouteMappedHandler { get; } | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
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,38 @@ | ||
namespace KafkaFlow.Admin.Dashboard | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
internal class DashboardConfigurationBuilder : IDashboardConfigurationBuilder | ||
{ | ||
private readonly List<Type> middlewares = new(); | ||
private Action<IEndpointConventionBuilder> routeMappedHandler = _ => { }; | ||
|
||
public IDashboardConfigurationBuilder AddMiddleware(Type middleware) | ||
{ | ||
this.middlewares.Add(middleware); | ||
return this; | ||
} | ||
|
||
public IDashboardConfigurationBuilder AddMiddlewares(IEnumerable<Type> typeMiddlewares) | ||
{ | ||
this.middlewares.AddRange(typeMiddlewares); | ||
return this; | ||
} | ||
|
||
public IDashboardConfigurationBuilder WithPostMapHandler(Action<IEndpointConventionBuilder> handler) | ||
{ | ||
this.routeMappedHandler = handler; | ||
return this; | ||
} | ||
|
||
public DashboardConfiguration Build() | ||
{ | ||
return new(this.middlewares, this.routeMappedHandler); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
namespace KafkaFlow.Admin.Dashboard | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
public interface IDashboardConfigurationBuilder | ||
{ | ||
IDashboardConfigurationBuilder AddMiddleware(Type middleware); | ||
|
||
IDashboardConfigurationBuilder AddMiddlewares(IEnumerable<Type> middlewares); | ||
|
||
IDashboardConfigurationBuilder WithPostMapHandler(Action<IEndpointConventionBuilder> handler); | ||
} | ||
} |