Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ODS-5623] Make the Swagger Startup configurable #1204

Open
wants to merge 7 commits into
base: main-6x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions Application/EdFi.Ods.SwaggerUI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,32 @@
// See the LICENSE and NOTICES files in the project root for more information.

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace EdFi.Ods.SwaggerUI
{
public class Program
{
private const string MissingStartupClassMessage =
"Could not find the startup class. No loaded class member could be found matching the configured startup class name '{0}'.";
private const string DefaultStartupClassName = "Startup";

public static async Task Main(string[] args)
{
await CreateHostBuilder(args).Build().RunAsync();
}

public static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args)
private static IHostBuilder CreateHostBuilder(string[] args)
{
var startupClassName = GetStartupClassName(args);
var startupType = FindStartupClassType(startupClassName);

return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(
webBuilder =>
{
Expand All @@ -28,8 +38,29 @@ public static IHostBuilder CreateHostBuilder(string[] args)
{
serverOptions.AddServerHeader = false;
});

webBuilder.UseStartup<Startup>();
webBuilder.UseStartup(startupType);
});
}

private static string GetStartupClassName(string[] args)
{
using var hostBuilder = Host.CreateDefaultBuilder(args).Build();
var config = hostBuilder.Services.GetService<IConfiguration>();
return config?.GetSection("SwaggerUIOptions").GetValue<string>("StartupClassName") ?? DefaultStartupClassName;
}

private static Type FindStartupClassType(string startupClassName)
{
var startupType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.FirstOrDefault(t => t.Name == startupClassName);

if (startupType == null)
{
throw new MissingMemberException(string.Format(MissingStartupClassMessage, startupClassName));
}

return startupType;
}
}
}
1 change: 1 addition & 0 deletions Application/EdFi.Ods.SwaggerUI/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"WebApiVersionUrl": "",
"SwaggerUIOptions": {
"StartupClassName": "Startup",
"OAuthConfigObject": {
"ClientId": "",
"ClientSecret": ""
Expand Down
Loading