| 1 | using System.Text;
|
|---|
| 2 | using System.Text.Json.Serialization;
|
|---|
| 3 | using iknow_api.Data;
|
|---|
| 4 | using iknow_api.Models;
|
|---|
| 5 | using iknow_api.Repositories;
|
|---|
| 6 | using iknow_api.Services;
|
|---|
| 7 | using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|---|
| 8 | using Microsoft.EntityFrameworkCore;
|
|---|
| 9 | using Microsoft.IdentityModel.Tokens;
|
|---|
| 10 |
|
|---|
| 11 | var builder = WebApplication.CreateBuilder(args);
|
|---|
| 12 |
|
|---|
| 13 | // Add CORS policy
|
|---|
| 14 | builder.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 |
|
|---|
| 28 | builder.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.
|
|---|
| 53 | builder.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 | });
|
|---|
| 60 | builder.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.
|
|---|
| 68 | builder.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
|
|---|
| 81 | builder.Services.AddScoped<IAuthService, AuthService>();
|
|---|
| 82 | builder.Services.AddScoped<IUserRepository, UserRepository>(); // You'll need to add the UserRepository implementation
|
|---|
| 83 | builder.Services.AddScoped<IUserService, UserService>();
|
|---|
| 84 |
|
|---|
| 85 | builder.Services.AddScoped<IRefreshTokenService, RefreshTokenService>();
|
|---|
| 86 | builder.Services.AddScoped<IUserRepository, UserRepository>();
|
|---|
| 87 | builder.Services.AddScoped<IRefreshTokenRepository, RefreshTokenRepository>();
|
|---|
| 88 | builder.Services.AddScoped<IProfRepository, ProfRepository>();
|
|---|
| 89 | builder.Services.AddScoped<IProfService, ProfService>();
|
|---|
| 90 | builder.Services.AddScoped<IEnrollmentService, EnrollmentService>();
|
|---|
| 91 | builder.Services.AddScoped<IAdminService, AdminService>();
|
|---|
| 92 | builder.Services.AddAuthorization();
|
|---|
| 93 | var app = builder.Build();
|
|---|
| 94 |
|
|---|
| 95 | // Configure the HTTP request pipeline.
|
|---|
| 96 | if (app.Environment.IsDevelopment())
|
|---|
| 97 | {
|
|---|
| 98 | app.MapOpenApi();
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | // Enable CORS - must be called before UseAuthentication and UseAuthorization
|
|---|
| 102 | app.UseCors("AllowFrontend");
|
|---|
| 103 |
|
|---|
| 104 | app.UseHttpsRedirection();
|
|---|
| 105 | app.UseAuthentication(); // <-- must come BEFORE UseAuthorization
|
|---|
| 106 | app.UseAuthorization();
|
|---|
| 107 | app.MapControllers();
|
|---|
| 108 |
|
|---|
| 109 | app.Run();
|
|---|