Last active
February 21, 2020 05:03
-
-
Save danielcrenna/90846c230d114c5fd48efcc1dd2b4299 to your computer and use it in GitHub Desktop.
[ActiveRoutes] Hello World
This file contains hidden or 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
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>(); | |
} | |
} |
This file contains hidden or 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
"RuntimeFeature": { | |
"Enabled": true, | |
"RootPath": "/runtime", | |
"Policy": "AuthenticatedUser", | |
"Scheme": "Bearer" | |
} |
This file contains hidden or 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
public class RuntimeController<T> : Controller | |
{ | |
[DynamicHttpGet("env/name")] | |
public IActionResult GetEnvironmentMachineName() | |
{ | |
return Ok($"{Environment.MachineName}.{typeof(T).Name}"); | |
} | |
} |
This file contains hidden or 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
public class RuntimeFeature : DynamicFeature | |
{ | |
public RuntimeFeature() => ControllerTypes = new[] {typeof(RuntimeController<>)}; | |
public override IList<Type> ControllerTypes { get; } = new List<Type>(); | |
} |
This file contains hidden or 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
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; | |
} |
This file contains hidden or 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
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