-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
124 lines (103 loc) · 3.84 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using PlayOfferService.Application;
using PlayOfferService.Domain;
using PlayOfferService.Domain.Repositories;
using System.Reflection;
using Microsoft.AspNetCore.Authentication;
using PlayOfferService;
var builder = WebApplication.CreateBuilder(args);
var readConnectionString = "Host=pos-postgres-read;Database=pos_read_db;Username=pos_user;Password=pos_password";
var writeConnectionString = "Host=pos-postgres-write;Database=pos_write_db;Username=pos_user;Password=pos_password;";
builder.Services.AddDbContext<DbReadContext>(options =>
{
options.UseNpgsql(readConnectionString);
options.UseCamelCaseNamingConvention();
}
);
builder.Services.AddDbContext<DbWriteContext>(options =>
{
options.UseNpgsql(writeConnectionString);
options.UseCamelCaseNamingConvention();
}
);
// Add services to the container.
builder.Services.AddScoped<ClubRepository>();
builder.Services.AddScoped<MemberRepository>();
builder.Services.AddScoped<PlayOfferRepository>();
builder.Services.AddScoped<ReservationRepository>();
builder.Services.AddScoped<CourtRepository>();
builder.Services.AddScoped<ReadEventRepository>();
builder.Services.AddScoped<WriteEventRepository>();
builder.Services.AddControllers();
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
builder.Services
.AddAuthentication("BasicScheme")
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicScheme", null);
builder.Services.AddAuthorization();
if (builder.Environment.EnvironmentName != "Test")
{
builder.Services.AddHostedService<RedisPlayOfferStreamService>();
builder.Services.AddHostedService<RedisClubStreamService>();
builder.Services.AddHostedService<RedisMemberStreamService>();
builder.Services.AddHostedService<RedisReservationStreamService>();
builder.Services.AddHostedService<RedisCourtStreamService>();
}
// Swagger configuration
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "PlayOfferService API",
Description = "An ASP.NET Core Web API for managing PlayOffers",
});
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory,
$"{Assembly.GetExecutingAssembly().GetName().Name}.xml"));
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme.",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
}
});
});
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "PlayofferService API v1");
});
using var scope = app.Services.CreateScope();
var services = scope.ServiceProvider;
var readDbContext = services.GetRequiredService<DbReadContext>();
var writeDbContext = services.GetRequiredService<DbWriteContext>();
readDbContext.Database.EnsureCreated();
writeDbContext.Database.EnsureCreated();
app.UseMiddleware<JwtClaimsMiddleware>(); // Use custom middleware to extract JWT claims
app.UseAuthorization();
app.MapControllers();
app.Run();
// Needed for integration tests
public abstract partial class Startup
{
}