-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from canro91/remove-startup-frontdesk
Migrate Startup class to Program file - FrontDesk
- Loading branch information
Showing
19 changed files
with
232 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
FrontDesk/src/FrontDesk.Api/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.