source: iknow-api/Program.cs@ b8093a0

Last change on this file since b8093a0 was 1b20b22, checked in by Stefan-Saveski <stefansaveski19@…>, 8 days ago
  • ready for presentation
  • Property mode set to 100644
File size: 4.1 KB
Line 
1using System.Text;
2using System.Text.Json.Serialization;
3using iknow_api.Data;
4using iknow_api.Models;
5using iknow_api.Repositories;
6using iknow_api.Services;
7using Microsoft.AspNetCore.Authentication.JwtBearer;
8using Microsoft.EntityFrameworkCore;
9using Microsoft.IdentityModel.Tokens;
10
11var builder = WebApplication.CreateBuilder(args);
12
13// Add CORS policy
14builder.Services.AddCors(options =>
15{
16 options.AddPolicy("AllowFrontend", policy =>
17 {
18 var allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get<string[]>()
19 ?? new[] { "http://localhost:3000" };
20
21 policy.WithOrigins(allowedOrigins) // Your frontend URL(s)
22 .AllowAnyHeader()
23 .AllowAnyMethod()
24 .AllowCredentials();
25 });
26});
27
28builder.Services.AddAuthentication(options =>
29{
30 options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
31 options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
32})
33.AddJwtBearer(options =>
34{
35 options.RequireHttpsMetadata = false; // only for development
36 options.SaveToken = true;
37 options.TokenValidationParameters = new TokenValidationParameters
38 {
39 ValidateIssuer = true,
40 ValidIssuer = "iknow-api",
41
42 ValidateAudience = true,
43 ValidAudience = "iknow-api",
44
45 ValidateLifetime = true,
46
47 ValidateIssuerSigningKey = true,
48 IssuerSigningKey = new SymmetricSecurityKey(
49 Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
50 };
51});
52// Add services to the container.
53builder.Services.AddControllers()
54 .AddJsonOptions(options =>
55 {
56 options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
57 options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
58 options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
59 });
60builder.Services.AddOpenApi();
61
62// Register DbContext.
63// The database lives behind the SSH tunnel: tunnel_scripta.cmd maps
64// localhost:9999 to the faculty Postgres server. Start it before the API.
65// The enum labels come from sql/ddl.sql, so each CLR enum is mapped onto its
66// Postgres type by name; PgEnumLabels covers the members whose label is not
67// simply the lowercased member name.
68builder.Services.AddDbContext<AppDbContext>(options =>
69 options.UseNpgsql(
70 builder.Configuration.GetConnectionString("DefaultConnection"),
71 npgsql =>
72 {
73 npgsql.MapEnum<UserRole>("user_role", AppDbContext.ProjectSchema, PgEnumLabels.UserRole);
74 npgsql.MapEnum<HighSchoolType>("hs_type", AppDbContext.ProjectSchema, PgEnumLabels.Lowercase);
75 npgsql.MapEnum<Quota>("quota_type", AppDbContext.ProjectSchema, PgEnumLabels.Lowercase);
76 npgsql.MapEnum<sType>("semester_type", AppDbContext.ProjectSchema, PgEnumLabels.Lowercase);
77 npgsql.MapEnum<Grade>("grade_type", AppDbContext.ProjectSchema, PgEnumLabels.Grade);
78 }));
79
80// Register your services
81builder.Services.AddScoped<IAuthService, AuthService>();
82builder.Services.AddScoped<IUserRepository, UserRepository>(); // You'll need to add the UserRepository implementation
83builder.Services.AddScoped<IUserService, UserService>();
84
85builder.Services.AddScoped<IRefreshTokenService, RefreshTokenService>();
86builder.Services.AddScoped<IUserRepository, UserRepository>();
87builder.Services.AddScoped<IRefreshTokenRepository, RefreshTokenRepository>();
88builder.Services.AddScoped<IProfRepository, ProfRepository>();
89builder.Services.AddScoped<IProfService, ProfService>();
90builder.Services.AddScoped<IEnrollmentService, EnrollmentService>();
91builder.Services.AddScoped<IAdminService, AdminService>();
92builder.Services.AddAuthorization();
93var app = builder.Build();
94
95// Configure the HTTP request pipeline.
96if (app.Environment.IsDevelopment())
97{
98 app.MapOpenApi();
99}
100
101// Enable CORS - must be called before UseAuthentication and UseAuthorization
102app.UseCors("AllowFrontend");
103
104app.UseHttpsRedirection();
105app.UseAuthentication(); // <-- must come BEFORE UseAuthorization
106app.UseAuthorization();
107app.MapControllers();
108
109app.Run();
Note: See TracBrowser for help on using the repository browser.