Skip to content

Commit

Permalink
Simplify API Program.cs with extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
canro91 committed Sep 5, 2024
1 parent 6470604 commit 814f9f2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 48 deletions.
51 changes: 3 additions & 48 deletions FrontDesk/src/FrontDesk.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Linq;
using System.Reflection;
using BlazorShared;
using FastEndpoints;
Expand All @@ -10,16 +9,13 @@
using FrontDesk.Core.ScheduleAggregate;
using FrontDesk.Infrastructure;
using FrontDesk.Infrastructure.Data;
using FrontDesk.Infrastructure.Messaging;
using MassTransit;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -70,30 +66,7 @@

builder.Services.AddAutoMapper(typeof(Program).Assembly);

// configure messaging
var messagingConfig = builder.Configuration.GetSection("RabbitMq");
builder.Services.Configure<RabbitMqConfiguration>(messagingConfig);
builder.Services.AddScoped<IMessagePublisher, MassTransitMessagePublisher>();

builder.Services.AddMassTransit(x =>
{
var rabbitMqConfiguration = messagingConfig.Get<RabbitMqConfiguration>();
x.SetKebabCaseEndpointNameFormatter();

x.AddConsumers(Assembly.GetExecutingAssembly());

x.UsingRabbitMq((context, cfg) =>
{
var port = (ushort)rabbitMqConfiguration.Port;
cfg.Host(rabbitMqConfiguration.Hostname, port, rabbitMqConfiguration.VirtualHost, h =>
{
h.Username(rabbitMqConfiguration.UserName);
h.Password(rabbitMqConfiguration.Password);
});

cfg.ConfigureEndpoints(context);
});
});
builder.Services.AddMessaging(builder.Configuration);

// use real database
// Requires LocalDB which can be installed with SQL Server Express 2016
Expand All @@ -106,24 +79,7 @@

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var hostEnvironment = services.GetService<IWebHostEnvironment>();
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation($"Starting in environment {hostEnvironment.EnvironmentName}");
try
{
var seedService = services.GetRequiredService<AppDbContextSeed>();
//var catalogContext = services.GetRequiredService<AppDbContext>();
await seedService.SeedAsync(new OfficeSettings().TestDate);
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred seeding the DB.");
}
}
await app.SeedDatabaseAsync();

app.UseResponseCompression();

Expand All @@ -144,7 +100,6 @@

app.Run();


public partial class Program
{
public const string CORS_POLICY = "CorsPolicy";
Expand Down
39 changes: 39 additions & 0 deletions FrontDesk/src/FrontDesk.Api/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Reflection;
using FrontDesk.Core.Interfaces;
using FrontDesk.Infrastructure.Messaging;
using MassTransit;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace FrontDesk.Api
{
public static class ServiceCollectionExtensions
{
public static void AddMessaging(this IServiceCollection services, ConfigurationManager configuration)
{
var messagingConfig = configuration.GetSection("RabbitMq");
services.Configure<RabbitMqConfiguration>(messagingConfig);
services.AddScoped<IMessagePublisher, MassTransitMessagePublisher>();

services.AddMassTransit(x =>
{
var rabbitMqConfiguration = messagingConfig.Get<RabbitMqConfiguration>();
x.SetKebabCaseEndpointNameFormatter();

x.AddConsumers(Assembly.GetExecutingAssembly());

x.UsingRabbitMq((context, cfg) =>
{
var port = (ushort)rabbitMqConfiguration.Port;
cfg.Host(rabbitMqConfiguration.Hostname, port, rabbitMqConfiguration.VirtualHost, h =>
{
h.Username(rabbitMqConfiguration.UserName);
h.Password(rabbitMqConfiguration.Password);
});

cfg.ConfigureEndpoints(context);
});
});
}
}
}
35 changes: 35 additions & 0 deletions FrontDesk/src/FrontDesk.Api/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Threading.Tasks;
using FrontDesk.Infrastructure.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace FrontDesk.Api
{
public static class WebApplicationExtensions
{
public static async Task SeedDatabaseAsync(this WebApplication app)
{
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var hostEnvironment = services.GetService<IWebHostEnvironment>();
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation($"Starting in environment {hostEnvironment.EnvironmentName}");
try
{
var seedService = services.GetRequiredService<AppDbContextSeed>();
//var catalogContext = services.GetRequiredService<AppDbContext>();
await seedService.SeedAsync(new OfficeSettings().TestDate);
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred seeding the DB.");
}

Check notice

Code scanning / CodeQL

Generic catch clause Note

Generic catch clause.
}
}
}
}

0 comments on commit 814f9f2

Please sign in to comment.