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
RevLine 
[5eb7847]1using System.Text;
[c0256f3]2using System.Text.Json.Serialization;
[5eb7847]3using iknow_api.Data;
[1b20b22]4using iknow_api.Models;
[271178d]5using iknow_api.Repositories;
[5eb7847]6using iknow_api.Services;
7using Microsoft.AspNetCore.Authentication.JwtBearer;
8using Microsoft.EntityFrameworkCore;
9using Microsoft.IdentityModel.Tokens;
[618be76]10
[c11d3a3]11var builder = WebApplication.CreateBuilder(args);
[bf18ca4]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
[5eb7847]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});
[c11d3a3]52// Add services to the container.
[c0256f3]53builder.Services.AddControllers()
54 .AddJsonOptions(options =>
55 {
56 options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
[f0fc386]57 options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
58 options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
[c0256f3]59 });
[c11d3a3]60builder.Services.AddOpenApi();
61
[1b20b22]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.
[c0a75ab]68builder.Services.AddDbContext<AppDbContext>(options =>
[1b20b22]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 }));
[c0a75ab]79
[fc7b4b4]80// Register your services
81builder.Services.AddScoped<IAuthService, AuthService>();
82builder.Services.AddScoped<IUserRepository, UserRepository>(); // You'll need to add the UserRepository implementation
[4f5d8fd]83builder.Services.AddScoped<IUserService, UserService>();
84
[4e061d8]85builder.Services.AddScoped<IRefreshTokenService, RefreshTokenService>();
86builder.Services.AddScoped<IUserRepository, UserRepository>();
87builder.Services.AddScoped<IRefreshTokenRepository, RefreshTokenRepository>();
[1b20b22]88builder.Services.AddScoped<IProfRepository, ProfRepository>();
89builder.Services.AddScoped<IProfService, ProfService>();
90builder.Services.AddScoped<IEnrollmentService, EnrollmentService>();
91builder.Services.AddScoped<IAdminService, AdminService>();
[5eb7847]92builder.Services.AddAuthorization();
[c11d3a3]93var app = builder.Build();
94
95// Configure the HTTP request pipeline.
96if (app.Environment.IsDevelopment())
97{
98 app.MapOpenApi();
99}
100
[bf18ca4]101// Enable CORS - must be called before UseAuthentication and UseAuthorization
102app.UseCors("AllowFrontend");
103
[c11d3a3]104app.UseHttpsRedirection();
[5eb7847]105app.UseAuthentication(); // <-- must come BEFORE UseAuthorization
[c11d3a3]106app.UseAuthorization();
107app.MapControllers();
108
109app.Run();
Note: See TracBrowser for help on using the repository browser.