Skip to content

Commit

Permalink
clean up Program.cs to use extension methods, group endpoints with /a…
Browse files Browse the repository at this point in the history
…pi & add signIn minimal endpoint
  • Loading branch information
philipphoeninger committed Oct 20, 2024
1 parent 8711dec commit 05cb559
Showing 1 changed file with 16 additions and 47 deletions.
63 changes: 16 additions & 47 deletions API/API.FileSharing/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,17 @@

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddSwaggerExplorer(builder.Configuration)
.AddFileSharingApiVersionConfiguration(new ApiVersion(0, 1))
.AddSqlServerConnection(builder.Configuration)
.AddAppConfig(builder.Configuration)
.AddIdentityHandlersAndStores()
.ConfigureIdentityOptions()
.AddIdentityAuth(builder.Configuration)
.AddRepositories();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddAndConfigureSwagger(
builder.Configuration,
Path.Combine(
AppContext.BaseDirectory,
$"{Assembly.GetExecutingAssembly().GetName().Name}.xml"),
true);

// add API Version support
builder.Services.AddFileSharingApiVersionConfiguration(new ApiVersion(0, 1));

// add SQL Server Connection
var connectionString = builder.Configuration.GetConnectionString("FileSharing");
builder.Services.AddSqlServer<ApplicationDBContext>(connectionString, options =>
{
options.EnableRetryOnFailure().CommandTimeout(60);
});

// add auth
builder.Services.AddAuthorization();
builder.Services.AddAuthentication();

// Add Identity
builder.Services
.AddIdentityCore<User>()
.AddEntityFrameworkStores<ApplicationDBContext>()
.AddApiEndpoints();

// add repositories
builder.Services.AddRepositories();

// Configure logging
builder.ConfigureSerilog();
Expand All @@ -55,26 +31,19 @@
SampleDataInitializer.InitializeData(dbContext);
//SampleDataInitializer.ClearAndReseedDatabase(dbContext);
}
app.UseSwagger();
app.UseSwaggerUI(options =>
{
using var scope = app.Services.CreateScope();
var versionProvider = scope.ServiceProvider.GetRequiredService<IApiVersionDescriptionProvider>();
// build a swagger endpoint for each discovered API version
foreach (var description in versionProvider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
}
});
app.ConfigureSwaggerExplorer();
}

app.UseHttpsRedirection();

// enable authorization checks
app.UseAuthentication();
app.UseAuthorization();
app.ConfigureCORS(builder.Configuration)
.AddIdentityAuthMiddlewares();

app.MapIdentityApi<User>();
app.MapControllers();
app.MapGroup("/api")
.MapIdentityApi<User>();
app.MapGroup("/api")
.MapIdentityUserEndpoints();


app.Run();

0 comments on commit 05cb559

Please sign in to comment.