Skip to content

Commit

Permalink
Merge pull request #88 from canro91/remove-startup-frontdesk
Browse files Browse the repository at this point in the history
Migrate Startup class to Program file - FrontDesk
  • Loading branch information
ardalis authored Sep 11, 2024
2 parents e5abad3 + 814f9f2 commit 7a59eda
Show file tree
Hide file tree
Showing 19 changed files with 232 additions and 333 deletions.
134 changes: 97 additions & 37 deletions FrontDesk/src/FrontDesk.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,106 @@
using System;
using System.Linq;
using System.Reflection;
using BlazorShared;
using FastEndpoints;
using FastEndpoints.Swagger;
using FrontDesk.Api;
using FrontDesk.Api.Hubs;
using FrontDesk.Core.Interfaces;
using FrontDesk.Core.ScheduleAggregate;
using FrontDesk.Infrastructure;
using FrontDesk.Infrastructure.Data;
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;

namespace FrontDesk.Api
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSignalR();
builder.Services.AddMemoryCache();

builder.Services.AddSingleton(typeof(IApplicationSettings), typeof(OfficeSettings));

var baseUrlConfig = new BaseUrlConfiguration();
builder.Configuration.Bind(BaseUrlConfiguration.CONFIG_NAME, baseUrlConfig);

builder.Services.AddCors(options =>
{
public class Program
options.AddPolicy(name: CORS_POLICY,
builder =>
{
builder.WithOrigins(baseUrlConfig.WebBase.Replace("host.docker.internal", "localhost").TrimEnd('/'));
builder.SetIsOriginAllowed(origin => true);
//builder.SetIsOriginAllowed(origin => new Uri(origin).Host == "localhost");
builder.AllowAnyMethod();
builder.AllowAnyHeader();
});
});

builder.Services
.AddFastEndpoints()
.SwaggerDocument(options =>
{
public static async System.Threading.Tasks.Task Main(string[] args)
options.DocumentSettings = s =>
{
var host = CreateHostBuilder(args)
.Build();

using (var scope = host.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.");
}
}

host.Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
s.Title = "My API V1";
};
});

var assemblies = new Assembly[]
{
typeof(Program).Assembly,
typeof(AppDbContext).Assembly,
typeof(Appointment).Assembly
};
builder.Services.AddMediatR(config => config.RegisterServicesFromAssemblies(assemblies));

builder.Services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});

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

builder.Services.AddMessaging(builder.Configuration);

// use real database
// Requires LocalDB which can be installed with SQL Server Express 2016
// https://www.microsoft.com/en-us/download/details.aspx?id=54284
builder.Services.AddDbContext<AppDbContext>(c =>
c.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

bool isDevelopment = builder.Environment.IsDevelopment();
builder.Services.AddInfrastructureDependencies(isDevelopment);

var app = builder.Build();

await app.SeedDatabaseAsync();

app.UseResponseCompression();

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

// if enabled configure docker with
// https://docs.microsoft.com/en-us/aspnet/core/security/docker-compose-https?view=aspnetcore-5.0
//app.UseHttpsRedirection();

app.UseRouting();
app.UseCors(CORS_POLICY);

app.UseFastEndpoints().UseSwaggerGen();
app.MapHub<ScheduleHub>("/schedulehub");

app.Run();

public partial class Program
{
public const string CORS_POLICY = "CorsPolicy";
}
1 change: 1 addition & 0 deletions FrontDesk/src/FrontDesk.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "/swagger",
"applicationUrl": "https://localhost:5251;http://localhost:5250",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
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);
});
});
}
}
}
183 changes: 0 additions & 183 deletions FrontDesk/src/FrontDesk.Api/Startup.cs

This file was deleted.

Loading

0 comments on commit 7a59eda

Please sign in to comment.