Something went wrong. Try again.
A fashion app that uses computer vision to recommend clothing items
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103using Worker.Models;using Microsoft.EntityFrameworkCore;using Microsoft.AspNetCore.Identity;using Microsoft.AspNetCore.Authentication.JwtBearer;using Microsoft.EntityFrameworkCore.Metadata.Internal;using System.Text;using Worker.Services;using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbucklebuilder.Services.AddEndpointsApiExplorer();builder.Services.AddSwaggerGen(options =>{ options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme() { Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.Http, Scheme = "Bearer" });
options.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, Array.Empty<string>() } });});
// Initialize Database with correct connection string.builder.Services.AddEntityFrameworkNpgsql().AddDbContext<ClothingItemContext>(options => options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
// Add Identity services to the container.builder.Services.AddIdentity<User, IdentityRole>() .AddEntityFrameworkStores<ClothingItemContext>() .AddDefaultTokenProviders();
// Set up JWT authenticationbuilder.Services.AddAuthorization();builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;}) .AddJwtBearer(options => { options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters { ValidateIssuerSigningKey = true, // Make sure the token is valid from the key IssuerSigningKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JwtSettings:Secret"]!)), ValidateIssuer = false, // Not caring about where the token is coming from ValidateAudience = false, // Not caring about who is receiving this token ValidateLifetime = true // Ensure it's not expired }; });
builder.Services.AddScoped<JwtService>();
// TODO Implement CORS functionality for APIbuilder.Services.AddCors(options =>{ options.AddPolicy(name: "frontend", policy => { policy.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); });});
var app = builder.Build();
// Apply migrations to databaseusing (var scope = app.Services.CreateScope()){ var clothingItemContext = scope.ServiceProvider.GetRequiredService<ClothingItemContext>(); clothingItemContext.Database.Migrate();}
// Apply CORS policyapp.UseCors("frontend");
app.UseSwagger();app.UseSwaggerUI();// app.UseHttpsRedirection();app.UseAuthentication();app.UseAuthorization();app.MapControllers();app.Run();