-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
76 lines (63 loc) · 2.03 KB
/
Program.cs
File metadata and controls
76 lines (63 loc) · 2.03 KB
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
using cubets_core.Data;
using cubets_core.Hubs;
using CubetsCore.Extensions;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
// EF Core / MySQL
builder.Services.AddDbContext<CubetsDbContext>(options =>
options.UseMySql(
builder.Configuration.GetConnectionString("Default"),
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("Default"))
));
builder.Services.AddApplicationServices();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(opt =>
{
var key = Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!);
opt.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"],
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key)
};
});
// SignalR
builder.Services.AddSignalR();
// Controllers
builder.Services.AddControllers();
// OpenAPI / Swagger
builder.Services.AddEndpointsApiExplorer(); // wajib sebelum builder.Build()
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Swagger UI hanya di Development
if (app.Environment.IsDevelopment())
{
app.UseSwagger(); // JSON OpenAPI
app.UseSwaggerUI(); // Swagger UI di /swagger
}
// Middleware
app.UseHttpsRedirection();
// CORS
app.UseCors(policy => policy
.WithOrigins("http://localhost:5173")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseAuthentication();
app.UseAuthorization();
// Redirect root "/" ke Swagger UI
app.MapGet("/", context =>
{
context.Response.Redirect("/swagger");
return Task.CompletedTask;
});
// Endpoint Controller dan Hub
app.MapControllers();
app.MapHub<GameHub>("/gamehub");
app.Run();