-
Notifications
You must be signed in to change notification settings - Fork 436
/
Program.cs
68 lines (50 loc) · 1.87 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Microsoft.AspNetCore.HttpLogging;
using Microsoft.AspNetCore.Identity;
using Scalar.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
// Configure data protection, setup the application discriminator
// so that the data protection keys can be shared between the BFF and this API
builder.Services.AddDataProtection(o => o.ApplicationDiscriminator = "TodoApp");
// Configure auth
builder.Services.AddAuthentication().AddBearerToken(IdentityConstants.BearerScheme);
builder.Services.AddAuthorizationBuilder().AddCurrentUserHandler();
// Configure identity
builder.Services.AddIdentityCore<TodoUser>()
.AddEntityFrameworkStores<TodoDbContext>()
.AddApiEndpoints();
// Configure the database
var connectionString = builder.Configuration.GetConnectionString("Todos") ?? "Data Source=.db/Todos.db";
builder.Services.AddSqlite<TodoDbContext>(connectionString);
// State that represents the current user from the database *and* the request
builder.Services.AddCurrentUser();
// Configure Open API
builder.Services.AddOpenApi(options => options.AddBearerTokenAuthentication());
// Configure rate limiting
builder.Services.AddRateLimiting();
builder.Services.AddHttpLogging(o =>
{
if (builder.Environment.IsDevelopment())
{
o.CombineLogs = true;
o.LoggingFields = HttpLoggingFields.ResponseBody | HttpLoggingFields.ResponseHeaders;
}
});
var app = builder.Build();
app.UseHttpLogging();
app.UseRateLimiter();
if (app.Environment.IsDevelopment())
{
app.MapScalarApiReference(options =>
{
options.Servers = [];
options.Authentication = new() { PreferredSecurityScheme = IdentityConstants.BearerScheme };
});
}
app.MapOpenApi();
app.MapDefaultEndpoints();
app.Map("/", () => Results.Redirect("/scalar/v1"));
// Configure the APIs
app.MapTodos();
app.MapUsers();
app.Run();