mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
6d1c53beeb
Large refactor that renames/moves core types into a new Govor.Domain surface and reorganizes the Application layer. Models, configurations, migrations and many files moved from Govor.Core/Govor.Data to Govor.Domain; numerous Application services, interfaces and implementations were relocated or added (authentication, friends, messages, medias, push notifications, user sessions, storage, synching, private chats, etc.). Tests updated to use Govor.Domain namespaces and adjusted project references (removed Govor.Data reference from API tests). Also updated API, Hub and mapping code and project files to reflect the new structure and naming. This is primarily a codebase-wide namespace and module reorganization to establish a Domain project and restructure application services.
155 lines
4.3 KiB
C#
155 lines
4.3 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.Models;
|
|
|
|
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();
|
|
|
|
builder.Services.AddSwaggerGen(options =>
|
|
{
|
|
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Govor API", Version = "v1" });
|
|
|
|
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
Description = "JWT Authorization header using the Bearer scheme. Example: 'Bearer {token}'",
|
|
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>()
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
//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(); |