Skip to content

Commit

Permalink
Merge pull request #89 from canro91/remove-startup-class-vetclinic
Browse files Browse the repository at this point in the history
Migrate Startup class to Program file - VetClinicPublic
  • Loading branch information
ardalis authored Sep 11, 2024
2 parents 0686c2e + be86a23 commit e5abad3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 111 deletions.
58 changes: 39 additions & 19 deletions VetClinicPublic/src/VetClinicPublic/Program.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using VetClinicPublic;

namespace VetClinicPublic
var builder = WebApplication.CreateBuilder(args);

Console.WriteLine("Run RabbitMQ in Docker if it's not already running.");
Console.WriteLine(">> docker run -p 5672:5672 -p 15672:15672 rabbitmq:management");

builder.Services.AddControllersWithViews();

var configuration = builder.Configuration;
builder.Services.ConfigureSiteSettings(configuration);
builder.Services.ConfigureEmailSending(configuration);
builder.Services.AddMessaging(configuration);

var app = builder.Build();

if (builder.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
// disabled because of docker setup required
//app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

public partial class Main
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();

Console.WriteLine("Run RabbitMQ in Docker if it's not already running.");
Console.WriteLine(">> docker run -p 5672:5672 -p 15672:15672 rabbitmq:management");
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
55 changes: 55 additions & 0 deletions VetClinicPublic/src/VetClinicPublic/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Reflection;
using MassTransit;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using VetClinicPublic.Web.Interfaces;
using VetClinicPublic.Web.Services;

namespace VetClinicPublic
{
public static class ServiceCollectionExtensions
{
public static void ConfigureSiteSettings(this IServiceCollection services, ConfigurationManager configuration)
{
var siteSettings = configuration.GetSection("SiteSettings");
services.Configure<SiteConfiguration>(siteSettings);
}

public static void ConfigureEmailSending(this IServiceCollection services, ConfigurationManager configuration)
{
var mailserverConfig = configuration.GetSection("Mailserver");
services.Configure<MailserverConfiguration>(mailserverConfig);
services.AddSingleton<ISendEmail, SmtpEmailSender>();
services.AddSingleton<ISendConfirmationEmails, ConfirmationEmailSender>();

services.AddMediatR(config => config.RegisterServicesFromAssembly(typeof(Main).Assembly));
}

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);
});
});
}
}
}
92 changes: 0 additions & 92 deletions VetClinicPublic/src/VetClinicPublic/Startup.cs

This file was deleted.

0 comments on commit e5abad3

Please sign in to comment.