1
0

70 lines
1.7 KiB
C#

using Flawless.Server.Controllers;
using Flawless.Server.Middlewares;
using Flawless.Server.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Api related
builder.Services.AddOpenApi();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSingleton<RepositoryContext>();
builder.Services.AddSwaggerGen(opt =>
{
opt.DocInclusionPredicate((name, api) => api.HttpMethod != null); // Filter out WebSocket methods
opt.SupportNonNullableReferenceTypes();
});
// Authentication related.
builder.Services.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(opt =>
{
});
// Data connection related.
builder.Services.AddDbContextFactory<RepositoryContext, RepositoryContextFactory>();
builder.Services.AddDbContext<GlobalContext>(opt =>
{
opt.UseInMemoryDatabase("Main");
});
var app = builder.Build();
app.UseRouting();
// Config WebSocket support.
app.UseWebSockets(new WebSocketOptions
{
KeepAliveInterval = TimeSpan.FromSeconds(60),
KeepAliveTimeout = TimeSpan.FromSeconds(300),
});
app.UseWebSocketHandoffMiddleware();
// Configure identity control
app.UseAuthentication();
app.UseAuthorization();
// Configure actual controllers
app.MapControllers();
// Configure fallback endpoints
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwagger();
app.UseSwaggerUI();
app.MapGet("/", () => Results.Redirect("/swagger/index.html"));
}
else
{
app.MapGet("/", () => "<p>Please use client app to open this server.</p>");
}
app.Run();