mirror of
https://github.com/Govor-team/Govor.git
synced 2026-09-17 16:22:49 +00:00
3c785d5c89
Migrate codebase to use SmartRes Result<T, Error> (and Unit) across services and interfaces, replacing previous Result usage and updating many method signatures and implementations. Add ResultExtensions to convert SmartRes results to ASP.NET ActionResult and refactor AuthController to use functional Bind/Tap chains for registration/login flows. Upgrade projects to .NET 10 and bump related NuGet packages; add libs/SmartRes.dll and project references. Simplify DbContext registration to use Npgsql only with retry policies and enable detailed logging. Update Swagger/launch settings and other minor fixes (AutoMapper registration change, whitespace/exception handling, and removal of the Govor.ConsoleClient files).
161 lines
4.5 KiB
C#
161 lines
4.5 KiB
C#
using System.Text;
|
|
using FirebaseAdmin;
|
|
using Google.Apis.Auth.OAuth2;
|
|
using Govor.API.Common.Extensions;
|
|
using Govor.API.Hubs;
|
|
using Govor.Application.Authentication.JWT;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
var configuration = builder.Configuration;
|
|
var services = builder.Services;
|
|
|
|
builder.AddLogger();// Serilog
|
|
|
|
|
|
builder.Configuration.AddJsonFile("configs/ban_usernames.json", optional: false, reloadOnChange: true);
|
|
|
|
#if DEBUG
|
|
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
|
//builder.Configuration.AddJsonFile("appsettings.Development.json", optional: false, reloadOnChange: true);
|
|
#else
|
|
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
|
#endif
|
|
|
|
FirebaseApp.Create(new AppOptions()
|
|
{
|
|
Credential = GoogleCredential.FromFile("secrets/firebase-adminsdk.json")
|
|
});
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("AllowFrontend", policy =>
|
|
{
|
|
policy.SetIsOriginAllowed(_ => true)
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
|
|
builder.Services.Configure<JwtAccessOption>(configuration.GetSection(nameof(JwtAccessOption)));
|
|
|
|
// Add services
|
|
builder.Services.AddSignalRConf();// signalR
|
|
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(
|
|
Encoding.UTF8.GetBytes(builder.Configuration["JwtAccessOption:SecretKey"]!))
|
|
};
|
|
options.Events = new JwtBearerEvents
|
|
{
|
|
OnMessageReceived = context =>
|
|
{
|
|
var accessToken = context.Request.Query["access_token"];
|
|
var path = context.HttpContext.Request.Path;
|
|
if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/api/chats"))
|
|
{
|
|
context.Token = accessToken;
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
};
|
|
});
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
// Init DI
|
|
builder.Services.AddServices();
|
|
builder.Services.AddOptionsConfiguration(configuration);
|
|
|
|
builder.Services.AddGovorDbContext(configuration); // GovorDbContext init
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
services.AddSwaggerGen(options =>
|
|
{
|
|
const string schemeId = "Bearer";
|
|
|
|
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Govor API", Version = "v1" });
|
|
|
|
options.AddSecurityDefinition(schemeId, new OpenApiSecurityScheme
|
|
{
|
|
Type = SecuritySchemeType.Http,
|
|
In = ParameterLocation.Header,
|
|
Scheme = "bearer",
|
|
BearerFormat = "JWT",
|
|
Description = "JWT Authorization header using the Bearer scheme. Example: 'Bearer {token}'"
|
|
});
|
|
|
|
options.AddSecurityRequirement(document =>
|
|
{
|
|
var requirement = new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecuritySchemeReference(schemeId)
|
|
{
|
|
Reference = new OpenApiReferenceWithDescription { Type = ReferenceType.SecurityScheme, Id = "Bearer" }
|
|
},
|
|
[]
|
|
}
|
|
};
|
|
return requirement;
|
|
});
|
|
});
|
|
|
|
|
|
//builder.Services.AddOpenApi();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
//app.MapOpenApi();
|
|
builder.WebHost.UseUrls("http://0.0.0.0:8080");
|
|
//builder.WebHost.UseUrls("http://10.8.0.5:5000");
|
|
//builder.WebHost.UseUrls("http://192.168.1.107:8080");
|
|
}
|
|
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
|
|
app.UseCors("AllowFrontend");
|
|
|
|
//app.UseHttpsRedirection();
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.MapGet("/server/ping",
|
|
() => new OkResult());
|
|
|
|
app.MapHub<ChatsHub>("/hubs/chats");
|
|
app.MapHub<FriendsHub>("/hubs/friends");
|
|
app.MapHub<ProfileHub>("/hubs/profiles");
|
|
app.MapHub<PresenceHub>("/hubs/presence");
|
|
|
|
app.MapSwagger()
|
|
.RequireAuthorization();
|
|
|
|
app.Map("/", () => "Not for browsers");
|
|
|
|
app.Run(); |