Skip to content

Instantly share code, notes, and snippets.

@danielcrenna
Last active February 21, 2020 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielcrenna/90846c230d114c5fd48efcc1dd2b4299 to your computer and use it in GitHub Desktop.
Save danielcrenna/90846c230d114c5fd48efcc1dd2b4299 to your computer and use it in GitHub Desktop.
[ActiveRoutes] Hello World
public static class Add
{
public static IMvcCoreBuilder AddRuntimeApi<T>(this IMvcCoreBuilder mvcBuilder, IConfiguration config)
{
mvcBuilder.Services.Configure<RuntimeOptions>(config, o => { o.BindNonPublicProperties = false; });
return mvcBuilder.AddActiveRoute<RuntimeController<T>, RuntimeFeature, RuntimeOptions>();
}
public static IMvcCoreBuilder AddRuntimeApi<T>(this IMvcCoreBuilder mvcBuilder,
Action<RuntimeOptions> configureAction = null)
{
if (configureAction != null)
mvcBuilder.Services.Configure(configureAction);
return mvcBuilder.AddActiveRoute<RuntimeController<T>, RuntimeFeature, RuntimeOptions>();
}
}
"RuntimeFeature": {
"Enabled": true,
"RootPath": "/runtime",
"Policy": "AuthenticatedUser",
"Scheme": "Bearer"
}
public class RuntimeController<T> : Controller
{
[DynamicHttpGet("env/name")]
public IActionResult GetEnvironmentMachineName()
{
return Ok($"{Environment.MachineName}.{typeof(T).Name}");
}
}
public class RuntimeFeature : DynamicFeature
{
public RuntimeFeature() => ControllerTypes = new[] {typeof(RuntimeController<>)};
public override IList<Type> ControllerTypes { get; } = new List<Type>();
}
public class RuntimeOptions :
IFeatureNamespace,
IFeatureToggle,
IFeatureScheme,
IFeaturePolicy
{
public bool Enabled { get; set; } = true;
public string RootPath { get; set; } = "/api";
public string Policy { get; set; } = Constants.Security.Policies.NoPolicy;
public string Scheme { get; set; } = Constants.Security.Schemes.NoScheme;
}
public class Startup
{
private readonly IConfiguration _configuration;
public Startup(IConfiguration configuration) => _configuration = configuration;
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddActiveRouting(mvcBuilder =>
{
mvcBuilder.AddAuthorization(options =>
options.AddPolicy("AuthenticatedUser", b => { b.RequireAuthenticatedUser(); }));
mvcBuilder.AddRuntimeApi<Startup>(_configuration.GetSection("RuntimeFeature"));
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
app.UseActiveRouting();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment