feat: added RabbitMQ queue from back-end to hook auth for client

This commit is contained in:
Pavel-Savely Savianok 2025-07-25 16:27:27 +03:00
parent 06367a3ccd
commit 8c9973e55d
4 changed files with 32 additions and 4 deletions

View File

@ -0,0 +1,6 @@
namespace JOBot.Backend.Infrastructure.Config;
public static class RabbitQueues
{
public const string AuthQueue = "auth";
}

View File

@ -4,7 +4,7 @@ var builder = WebApplication.CreateBuilder(args);
var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);
await startup.ConfigureServices(builder.Services);
var app = builder.Build();
startup.Configure(app, app.Environment);

View File

@ -1,14 +1,19 @@
using System.Text;
using System.Text.Json;
using System.Web;
using JOBot.Backend.DAL.Context;
using JOBot.Backend.DTOs.HeadHunterHook;
using JOBot.Backend.Infrastructure.Config;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using RabbitMQ.Client;
namespace JOBot.Backend.Services;
public class HeadHunterService(ILogger<HeadHunterService> logger, IOptions<HeadHunterConfig> config, AppDbContext dbContext)
public class HeadHunterService(
IChannel channel,
ILogger<HeadHunterService> logger,
IOptions<HeadHunterConfig> config,
AppDbContext dbContext)
{
private readonly HeadHunterConfig _config = config.Value;
@ -88,6 +93,14 @@ public class HeadHunterService(ILogger<HeadHunterService> logger, IOptions<HeadH
await dbContext.SaveChangesAsync();
logger.LogInformation($"User {userId} auth completed!");
await channel.BasicPublishAsync(
string.Empty,
RabbitQueues.AuthQueue,
Encoding.UTF8.GetBytes(userId.ToString()));
logger.LogInformation($"RabbitMQ event was created");
return Status.Success;
}

View File

@ -3,6 +3,7 @@ using JOBot.Backend.Infrastructure.Config;
using JOBot.Backend.Services;
using JOBot.Backend.Services.gRPC;
using Microsoft.EntityFrameworkCore;
using RabbitMQ.Client;
namespace JOBot.Backend;
@ -10,13 +11,21 @@ public class Startup(IConfiguration configuration)
{
private IConfiguration Configuration { get; } = configuration;
public void ConfigureServices(IServiceCollection services)
public async Task ConfigureServices(IServiceCollection services)
{
services.AddGrpc();
services.AddGrpcReflection();
services.AddControllers();
services.AddLogging();
await using var rabbitMqConnection = await new ConnectionFactory
{
HostName = "jobot-rabbitmq"
}.CreateConnectionAsync();
await using var channel = await rabbitMqConnection.CreateChannelAsync();
await channel.QueueDeclareAsync(RabbitQueues.AuthQueue, false, false, autoDelete: false);
services.AddSingleton(channel);
services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("PostgreSQL")));