Skip to content

Commit

Permalink
refactor(Program): Code refractor and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutak1337 committed Mar 17, 2024
1 parent e07f837 commit 0871cb1
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using StellarChat.Shared.Abstractions.API.Endpoints;

namespace StellarChat.Server.Api.Endpoints;

internal sealed class WeatherForecastEndpoint : IEndpoint
{
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

public void Expose(IEndpointRouteBuilder endpoints)
{
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"

};

endpoints.MapGet("/weatherforecast", (ILogger<WeatherForecast> logger) =>
{
logger.LogInformation("test logginig");
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
}

public void Register(IServiceCollection services, IConfiguration configuration)
{
}

public void Use(IApplicationBuilder app)
{
}
}
59 changes: 3 additions & 56 deletions src/Server/StellarChat.Server.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,11 @@
using StellarChat.Shared.Infrastructure.Exceptions;
using StellarChat.Shared.Infrastructure.Contexts;
using StellarChat.Shared.Infrastructure.Observability.Logging;
using StellarChat.Shared.Infrastructure.DAL.Mongo;
using StellarChat.Shared.Infrastructure.API.CORS;
using StellarChat.Shared.Infrastructure.API.Endpoints;
using StellarChat.Shared.Infrastructure;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddErrorHandling();
builder.Services.AddContext();
builder.Services.AddCorsPolicy(builder.Configuration);
builder.Host.UseLogging();
builder.Services.AddMongo(builder.Configuration);
builder.Services.RegisterEndpoints(builder.Configuration);
builder.AddSharedInfrastructure();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseCorsPolicy();
app.UseErrorHandling();
app.UseContext();
app.UseLogging();
app.UseEndpoints();
app.MapEndpoints();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", (ILogger<WeatherForecast> logger) =>
{
logger.LogInformation("test logginig");
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
app.UseSharedInfrastructure();

app.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
45 changes: 45 additions & 0 deletions src/Shared/StellarChat.Shared.Infrastructure/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using StellarChat.Shared.Infrastructure.API.CORS;
using StellarChat.Shared.Infrastructure.API.Endpoints;
using StellarChat.Shared.Infrastructure.Contexts;
using StellarChat.Shared.Infrastructure.DAL.Mongo;
using StellarChat.Shared.Infrastructure.Exceptions;
using StellarChat.Shared.Infrastructure.Observability.Logging;
using System.Text.RegularExpressions;

namespace StellarChat.Shared.Infrastructure;
Expand All @@ -9,6 +17,43 @@ public static class Extensions
{
private const string CorrelationIdKey = "correlation-id";

public static WebApplicationBuilder AddSharedInfrastructure(this WebApplicationBuilder builder)
{
builder.Host.UseLogging();

builder
.Services
.AddEndpointsApiExplorer()
.AddSwaggerGen()
.AddErrorHandling()
.AddContext()
.AddCorsPolicy(builder.Configuration)
.AddMongo(builder.Configuration)
.RegisterEndpoints(builder.Configuration);

return builder;
}

public static WebApplication UseSharedInfrastructure(this WebApplication app)
{
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection()
.UseCorsPolicy()
.UseErrorHandling()
.UseContext()
.UseLogging()
.UseEndpoints();

app.MapEndpoints();

return app;
}

public static string ToSnakeCase(this string input)
=> Regex.Replace(
Regex.Replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.1-dev-00972" />
<PackageReference Include="Serilog.Sinks.Seq" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

</Project>

0 comments on commit 0871cb1

Please sign in to comment.