Skip to content

Commit

Permalink
Create common application builder
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Nov 26, 2024
1 parent 799fe9b commit 03897d2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
4 changes: 1 addition & 3 deletions API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
using Scalar.AspNetCore;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

builder.ApplyBaseConfiguration(options =>
var builder = OpenShockApplication.CreateDefaultBuilder<Program>(args, options =>
{
options.ListenAnyIP(80);
#if DEBUG
Expand Down
26 changes: 0 additions & 26 deletions Common/Extensions/WebApplicationBuilderExtensions.cs

This file was deleted.

42 changes: 42 additions & 0 deletions Common/OpenShockApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Reflection;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Serilog;

namespace OpenShock.Common;

public static class OpenShockApplication
{
public static WebApplicationBuilder CreateDefaultBuilder<TProgram>(string[] args, Action<KestrelServerOptions> configurePorts) where TProgram : class
{
var builder = WebApplication.CreateSlimBuilder(args);

builder.Configuration.Sources.Clear();
builder.Configuration
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: false)
.AddJsonFile("appsettings.Custom.json", optional: true, reloadOnChange: false)
.AddEnvironmentVariables()
.AddUserSecrets<TProgram>(true)
.AddCommandLine(args);

var isDevelopment = builder.Environment.IsDevelopment();
builder.Host.UseDefaultServiceProvider((_, options) =>
{
options.ValidateScopes = isDevelopment;
options.ValidateOnBuild = isDevelopment;
});

// Since we use slim builders, this allows for HTTPS during local development
if (isDevelopment) builder.WebHost.UseKestrel();

builder.WebHost.ConfigureKestrel(serverOptions =>
{
configurePorts(serverOptions);
serverOptions.Limits.RequestHeadersTimeout = TimeSpan.FromMilliseconds(3000);
});

builder.Host.UseSerilog((context, _, config) => config.ReadFrom.Configuration(context.Configuration));

return builder;
}
}
4 changes: 1 addition & 3 deletions Cron/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
using OpenShock.Cron;
using OpenShock.Cron.Utils;

var builder = WebApplication.CreateBuilder(args);

builder.ApplyBaseConfiguration(options =>
var builder = OpenShockApplication.CreateDefaultBuilder<Program>(args, options =>
{
options.ListenAnyIP(780);
#if DEBUG
Expand Down
4 changes: 1 addition & 3 deletions LiveControlGateway/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
using OpenShock.LiveControlGateway.LifetimeManager;
using OpenShock.LiveControlGateway.PubSub;

var builder = WebApplication.CreateBuilder(args);

builder.ApplyBaseConfiguration(options =>
var builder = OpenShockApplication.CreateDefaultBuilder<Program>(args, options =>
{
#if DEBUG
options.ListenAnyIP(580);
Expand Down

0 comments on commit 03897d2

Please sign in to comment.