mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-05 18:48:41 +00:00
102 lines
3.7 KiB
C#
102 lines
3.7 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.RateLimiting;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using SharpbinV3.Server.Data;
|
|
using SharpbinV3.Server.Services;
|
|
using System.Text;
|
|
using System.Threading.RateLimiting;
|
|
|
|
namespace SharpbinV3.Server
|
|
{
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
|
|
builder.Services.AddControllers();
|
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
|
builder.Services.AddOpenApi();
|
|
builder.Services.AddDbContext<AppDbContext>(opt =>
|
|
{
|
|
opt.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection"));
|
|
});
|
|
builder.Services.AddSingleton<ICompressionService, CompressionService>();
|
|
builder.Services.AddScoped<IUserService, UserService>();
|
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
|
builder.Services.AddScoped<IPasteService, PasteService>();
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(jwt =>
|
|
{
|
|
var key = Encoding.ASCII.GetBytes(builder.Configuration.GetSection("JwtConfig:Secret").Value ?? string.Empty);
|
|
jwt.SaveToken = true;
|
|
jwt.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(key),
|
|
ValidateIssuer = true,
|
|
ValidIssuer = builder.Configuration["JwtConfig:Issuer"],
|
|
ValidateAudience = true,
|
|
ValidAudience = builder.Configuration["JwtConfig:Audience"],
|
|
RequireExpirationTime = true,
|
|
ValidateLifetime = true
|
|
};
|
|
});
|
|
|
|
builder.Services.AddRateLimiter(options =>
|
|
{
|
|
options.AddSlidingWindowLimiter("Sliding", opt =>
|
|
{
|
|
opt.Window = TimeSpan.FromSeconds(10);
|
|
opt.PermitLimit = 10;
|
|
opt.QueueLimit = 2;
|
|
opt.SegmentsPerWindow = 5;
|
|
opt.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
|
|
});
|
|
|
|
options.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
|
|
});
|
|
builder.Services.AddCors(o =>
|
|
{
|
|
o.AddDefaultPolicy(p =>
|
|
p.WithOrigins("http://localhost:5173")
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod());
|
|
});
|
|
|
|
var app = builder.Build();
|
|
app.UseRateLimiter();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.MapOpenApi();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
db.Database.Migrate();
|
|
}
|
|
|
|
app.Run();
|
|
}
|
|
}
|
|
}
|