diff --git a/VetClinicPublic/src/VetClinicPublic/Program.cs b/VetClinicPublic/src/VetClinicPublic/Program.cs index bca377d..cae83c5 100644 --- a/VetClinicPublic/src/VetClinicPublic/Program.cs +++ b/VetClinicPublic/src/VetClinicPublic/Program.cs @@ -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(); - }); - } } diff --git a/VetClinicPublic/src/VetClinicPublic/ServiceCollectionExtensions.cs b/VetClinicPublic/src/VetClinicPublic/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..f0274fe --- /dev/null +++ b/VetClinicPublic/src/VetClinicPublic/ServiceCollectionExtensions.cs @@ -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(siteSettings); + } + + public static void ConfigureEmailSending(this IServiceCollection services, ConfigurationManager configuration) + { + var mailserverConfig = configuration.GetSection("Mailserver"); + services.Configure(mailserverConfig); + services.AddSingleton(); + services.AddSingleton(); + + services.AddMediatR(config => config.RegisterServicesFromAssembly(typeof(Main).Assembly)); + } + + public static void AddMessaging(this IServiceCollection services, ConfigurationManager configuration) + { + var messagingConfig = configuration.GetSection("RabbitMq"); + services.Configure(messagingConfig); + services.AddScoped(); + + services.AddMassTransit(x => + { + var rabbitMqConfiguration = messagingConfig.Get(); + 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); + }); + }); + } + } +} diff --git a/VetClinicPublic/src/VetClinicPublic/Startup.cs b/VetClinicPublic/src/VetClinicPublic/Startup.cs deleted file mode 100644 index 2a46659..0000000 --- a/VetClinicPublic/src/VetClinicPublic/Startup.cs +++ /dev/null @@ -1,92 +0,0 @@ -using System.Reflection; -using MassTransit; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using VetClinicPublic.Web.Interfaces; -using VetClinicPublic.Web.Services; - -namespace VetClinicPublic -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - public void ConfigureServices(IServiceCollection services) - { - services.AddControllersWithViews(); - - // configure site settings - var siteSettings = Configuration.GetSection("SiteSettings"); - services.Configure(siteSettings); - - // configure email sending - var mailserverConfig = Configuration.GetSection("Mailserver"); - services.Configure(mailserverConfig); - services.AddSingleton(); - services.AddSingleton(); - - - // configure MediatR - services.AddMediatR(config => config.RegisterServicesFromAssembly(typeof(Startup).Assembly)); - - // configure messaging - var messagingConfig = Configuration.GetSection("RabbitMq"); - services.Configure(messagingConfig); - services.AddScoped(); - - services.AddMassTransit(x => - { - var rabbitMqConfiguration = messagingConfig.Get(); - 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); - }); - }); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - app.UseExceptionHandler("/Home/Error"); - app.UseHsts(); - } - // disabled because of docker setup required - //app.UseHttpsRedirection(); - app.UseStaticFiles(); - - app.UseRouting(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllerRoute( - name: "default", - pattern: "{controller=Home}/{action=Index}/{id?}"); - }); - } - } -}