// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Reflection;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Connections;
namespace Microsoft.AspNetCore.SignalR
{
///
/// Maps incoming requests to types.
///
public class HubRouteBuilder
{
private readonly ConnectionsRouteBuilder _routes;
///
/// Initializes a new instance of the class.
///
/// The routes builder.
public HubRouteBuilder(ConnectionsRouteBuilder routes)
{
_routes = routes;
}
///
/// Maps incoming requests with the specified path to the specified type.
///
/// The type to map requests to.
/// The request path.
public void MapHub(PathString path) where THub : Hub
{
MapHub(path, configureOptions: null);
}
///
/// Maps incoming requests with the specified path to the specified type.
///
/// The type to map requests to.
/// The request path.
/// A callback to configure dispatcher options.
public void MapHub(PathString path, Action configureOptions) where THub : Hub
{
// find auth attributes
var authorizeAttributes = typeof(THub).GetCustomAttributes(inherit: true);
var options = new HttpConnectionDispatcherOptions();
foreach (var attribute in authorizeAttributes)
{
options.AuthorizationData.Add(attribute);
}
configureOptions?.Invoke(options);
_routes.MapConnections(path, options, builder =>
{
builder.UseHub();
});
}
}
}