Skip to content

Commit

Permalink
Move All Swagger config into Configure.OpenApi.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Feb 7, 2024
1 parent e92bcc1 commit 5d408df
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
32 changes: 32 additions & 0 deletions MyApp/Configure.OpenApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[assembly: HostingStartup(typeof(MyApp.ConfigureOpenApi))]

namespace MyApp;

public class ConfigureOpenApi : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices((context, services) =>
{
if (context.HostingEnvironment.IsDevelopment())
{
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
services.AddServiceStackSwagger();
services.AddBasicAuth<Data.ApplicationUser>();
//services.AddJwtAuth();
services.AddTransient<IStartupFilter, StartupFilter>();
}
});

public class StartupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) => app =>
{
app.UseSwagger();
app.UseSwaggerUI();
next(app);
};
}
}
11 changes: 2 additions & 9 deletions MyApp/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using ServiceStack.Mvc;
using MyApp.Models;
using ServiceStack;

namespace MyApp.Controllers;

public class HomeController : ServiceStackController
public class HomeController(ILogger<HomeController> logger) : ServiceStackController
{
private readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
private readonly ILogger<HomeController> _logger = logger;

public IActionResult Index() => View();

Expand Down
5 changes: 4 additions & 1 deletion MyApp/MyApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.*" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.*" />
<PackageReference Include="Markdig" Version="0.34.*" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.*" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.*" />
<PackageReference Include="ServiceStack.AspNetCore.OpenApi" Version="8.*" />
</ItemGroup>

<ItemGroup>
Expand All @@ -35,7 +39,6 @@
<PackageReference Include="ServiceStack.Server" Version="8.*" />
<PackageReference Include="ServiceStack.Extensions" Version="8.*" />
<PackageReference Include="ServiceStack.OrmLite.Sqlite.Data" Version="8.*" />
<PackageReference Include="ServiceStack.AspNetCore.OpenApi" Version="8.*" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 1 addition & 10 deletions MyApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,15 @@
//services.AddSingleton<IEmailSender<ApplicationUser>, EmailSender>();
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, AdditionalUserClaimsPrincipalFactory>();

services.AddEndpointsApiExplorer();
services.AddSwaggerGen();

// Register all services
services.AddServiceStack(typeof(MyServices).Assembly, c => {
c.AddSwagger(o => {
o.AddBasicAuth();
});
});
services.AddServiceStack(typeof(MyServices).Assembly);

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI();
}
else
{
Expand Down

0 comments on commit 5d408df

Please sign in to comment.