-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
154 lines (130 loc) · 5.11 KB
/
Program.cs
File metadata and controls
154 lines (130 loc) · 5.11 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System.Text;
using System.Text.Json.Serialization;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using TaskManagementAPI.Data;
using TaskManagementAPI.Services;
using TaskManagementAPI.Blazor;
var builder = WebApplication.CreateBuilder(args);
// ---------- EF Core (SQLite) ----------
builder.Services.AddDbContext<TaskDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("Default")));
// ---------- Controllers + JSON ----------
builder.Services.AddControllers().AddJsonOptions(o =>
{ // ignore cycles to avoid ref loops
o.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
o.JsonSerializerOptions.MaxDepth = 32;
});
// ---------- Blazor Components ----------
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
// ---------- HTTP Client for Blazor ----------
builder.Services.AddHttpClient<TaskManagementAPI.Blazor.Services.IApiClient, TaskManagementAPI.Blazor.Services.ApiClient>(client =>
{
var apiSettings = builder.Configuration.GetSection("ApiSettings");
var baseUrl = apiSettings["BaseUrl"] ?? "http://localhost:5114";
client.BaseAddress = new Uri(baseUrl);
});
// ---------- Blazored LocalStorage ----------
builder.Services.AddBlazoredLocalStorage();
// ---------- Blazor Services ----------
builder.Services.AddScoped<TaskManagementAPI.Blazor.Services.AuthenticationService>();
builder.Services.AddScoped<TaskManagementAPI.Blazor.Services.ProjectApiService>();
builder.Services.AddScoped<TaskManagementAPI.Blazor.Services.TaskApiService>();
// ---------- CORS (dev-friendly) ----------
builder.Services.AddCors(o => o.AddPolicy("frontend", p => p
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
));
// ---------- DI ----------
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<IUserContext, HttpUserContext>();
builder.Services.AddScoped<ITokenService, TokenService>();
builder.Services.AddScoped<IProjectService, ProjectService>();
builder.Services.AddScoped<ITaskService, TaskService>();
builder.Services.AddScoped<ICommentService, CommentService>();
// ---------- JWT Auth ----------
var jwt = builder.Configuration.GetSection("Jwt"); // needs Key, Issuer, Audience, ExpiresMinutes
var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwt["Key"]!));
builder.Services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwt["Issuer"],
ValidAudience = jwt["Audience"],
IssuerSigningKey = signingKey
};
});
builder.Services.AddAuthorization();
// ---------- OpenAPI (Swagger JSON generator) ----------
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Task Management API",
Version = "v1",
Description = "Projects, Tasks, Users (JWT protected)"
});
// avoid conflicting schema names
c.CustomSchemaIds(t => t.FullName);
// JWT bearer auth in the spec
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "Enter: {your JWT token}"
});
// Apply security requirement - reference by name
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] {}
}
});
});
var app = builder.Build();
// ---------- Serve the OpenAPI JSON at a stable path ----------
app.UseSwagger(c =>
{
// JSON will be at /openapi/v1/openapi.json
c.RouteTemplate = "openapi/{documentName}/openapi.json";
});
// ---------- Pipeline ----------
app.UseStaticFiles(); // serves /swagger/index.html from wwwroot/swagger
// app.UseHttpsRedirection(); // keep off unless you configure HTTPS/ports
app.UseCors("frontend"); // before auth for preflight
app.UseRouting(); // First: UseRouting
app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery(); // After UseAuthentication/UseAuthorization, before MapControllers
app.MapControllers();
// Map Blazor components
app.MapRazorComponents<TaskManagementAPI.Blazor.App>()
.AddInteractiveServerRenderMode();
// Convenience redirects
// app.MapGet("/", () => Results.Redirect("/swagger/index.html")); // root -> docs
// app.MapGet("/docs", () => Results.Redirect("/swagger/index.html"));// /docs -> docs
app.Run();