Files
Govor/Govor.API/Common/Extensions/ConfigurationProgramExtensions.cs
T
Artemy 6d1c53beeb Refactor: migrate Core -> Domain and reorganize projects
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.
2026-07-16 19:27:45 +07:00

162 lines
7.0 KiB
C#

using Govor.API.Common.Mapping;
using Govor.API.Common.SignalR.Helpers;
using Govor.API.Hubs.Infrastructure;
using Govor.Application.Authentication;
using Govor.Application.Authentication.JWT;
using Govor.Application.Friends;
using Govor.Application.Groups;
using Govor.Application.Infrastructure.AdminsStuff;
using Govor.Application.Infrastructure.Extensions;
using Govor.Application.Infrastructure.Validators;
using Govor.Application.Medias;
using Govor.Application.Messages;
using Govor.Application.PingHandler;
using Govor.Application.PrivateUserChats;
using Govor.Application.Profiles;
using Govor.Application.PushNotifications;
using Govor.Application.PushNotifications.Providers;
using Govor.Application.Storage;
using Govor.Application.Synching;
using Govor.Application.Users;
using Govor.Application.Users.UserOnlineStatus;
using Govor.Application.Users.UserSessions;
using Govor.Application.Users.UserSessions.Crypto;
using Govor.Domain;
using Microsoft.EntityFrameworkCore;
namespace Govor.API.Common.Extensions;
public static class ConfigurationProgramExtensions
{
public static void AddServices(this IServiceCollection services)
{
services.AddSingleton<IPasswordHasher, PasswordHasher>();
services.AddSingleton<IUsernameValidator, UsernameValidator>();
services.AddSingleton<IJwtTokenHasher, JwtTokenHasher>();
services.AddScoped<IJwtService, JwtService>();
services.AddScoped<IAccountService, AuthService>();
services.AddScoped<IUsersAdministration, UsersService>();
services.AddScoped<IInvitesService, InvitesService>();
services.AddScoped<IInvitationGetter, InvitationGetter>();
services.AddScoped<IInvitationGenerator, InvitationGenerator>();
services.AddScoped<ISynchingService, SynchingService>();
// Friends services
services.AddScoped<IFriendshipService, FriendshipService>();
services.AddScoped<IFriendRequestCommandService, FriendRequestCommandService>();
services.AddScoped<IFriendRequestQueryService, FriendRequestQueryService>();
services.AddScoped<IFriendsBlockService, FriendsBlockService>();
services.AddScoped<IStorageService>(sp =>
{
var env = sp.GetRequiredService<IWebHostEnvironment>();
return new LocalStorageService(env.ContentRootPath);
});
services.AddHttpContextAccessor(); // it's very important for CurrentUserService
services.AddScoped<ICurrentUserService, CurrentUserService>();
services.AddScoped<ICurrentUserSessionService, CurrentUserSessionService>();
services.AddMemoryCache();
services.AddScoped<IPingHandlerService, PingHandlerService>();
services.AddScoped<IUserGroupsGetterService, UserGroupsGetterService>();
//services.AddScoped<IMessageCommandService, MessageCommandService>();
services.AddScoped<IMessageSendingService, MessageSendingService>();
services.AddScoped<IMessageEditingService, MessageEditingService>();
services.AddScoped<IMessageRemovingService, MessageRemovingService>();
services.AddScoped<IVerifyFriendship, VerifyFriendship>();
services.AddScoped<IUserPrivateChatsGetterService, UserPrivateChatsGetter>();
services.AddScoped<IUserPrivateChatsCreator, UserPrivateChatsCreator>();
services.AddScoped<IMessagesLoader, MessagesLoader>();
services.AddScoped<IMediaService, MediaService>();
services.AddScoped<IAccesserToDownloadMedia, AccesserToDownloadMediaService>();
// User
services.AddScoped<IUserNameExistValidator, UserNameExistValidator>();
// UserSession
services.AddScoped<IUserSessionOpener, UserSessionOpener>();
services.AddScoped<IUserSessionRefresher, UserSessionRefresher>();
services.AddScoped<IUserNotificationScopeService, UserNotificationScopeService>();
services.AddScoped<IUserPresenceReader, UserPresenceReader>();
services.AddSingleton<IOnlineUserStore, OnlineUserStore>();
// Hubs Infrastructure
services.AddScoped<IPrivateChatGroupManager, PrivateChatGroupManager>();
services.AddScoped<IChatNotificationService, ChatNotificationService>();
services.AddScoped<IConnectionManager, ConnectionManager>();
services.AddSingleton<IConnectionStore, ConnectionStore>();
// Pushs
services.AddScoped<IPushTokenService, PushTokenService>();
services.AddScoped<IPushNotificationService, PushNotificationService>();
services.AddSingleton<IPushNotificationProvider, FirebasePushProvider>();
// Auto Mapper
services.AddAutoMapper(typeof(MappingProfile));
services.AddScoped<IHubUserAccessor, HubUserAccessor>();
services.AddScoped<IUserSessionReader, UserSessionReader>();
services.AddScoped<IUserSessionRevoker, UserSessionRevoker>();
services.AddScoped<ISessionKeyAttacher, SessionKeyAttacher>();
services.AddScoped<ISessionKeysReader, SessionKeysReader>();
services.AddScoped<IOneTimePreKeysRotator, OneTimePreKeysRotator>();
services.AddScoped<IProfileService, ProfileService>();
}
public static void AddGovorDbContext(this IServiceCollection services, IConfiguration configuration)
{
var useMySql = configuration.GetValue<bool>("UseMySql");
if (useMySql)
{
services.AddDbContext<GovorDbContext>(options =>
{
var connectionString = configuration.GetConnectionString(nameof(GovorDbContext));
options.UseMySql(
connectionString,
new MySqlServerVersion(new Version(8, 0, 21)),
mySqlOptions =>
{
mySqlOptions.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: TimeSpan.FromSeconds(5),
errorNumbersToAdd: null);
});
options.EnableSensitiveDataLogging();
options.EnableDetailedErrors();
});
}
else
{
services.AddDbContext<GovorDbContext>(options =>
{
options.UseNpgsql(
configuration.GetConnectionString(nameof(GovorDbContext)),
npgsqlOptions =>
{
// retry for transient failures
npgsqlOptions.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: TimeSpan.FromSeconds(5),
errorCodesToAdd: null);
});
//options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
options.EnableSensitiveDataLogging();
options.EnableDetailedErrors();
});
}
}
}