Skip to content

Commit

Permalink
Simplify config and make main async
Browse files Browse the repository at this point in the history
  • Loading branch information
CorruptComputer committed Nov 10, 2024
1 parent eb4b257 commit e5f484b
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 30 deletions.
9 changes: 2 additions & 7 deletions Backend/Bones.Api/Models/ApiConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ namespace Bones.Api.Models;
internal sealed record ApiConfiguration
{
/// <summary>
/// Base URL for the WebUI that uses this API.
/// The origins we want to allow through CORS
/// </summary>
public string? WebUIBaseUrl { get; set; }

/// <summary>
/// Base URL for the API.
/// </summary>
public string? ApiBaseUrl { get; set; }
public IEnumerable<string>? CorsAllowedOrigins { get; init; }
}
15 changes: 8 additions & 7 deletions Backend/Bones.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static class Program
/// The main character of the project.
/// </summary>
/// <param name="args">Arg, I'm a pirate.</param>
public static void Main(string[] args)
public static async Task Main(string[] args)
{
WebApplication.CreateBuilder(args).BuildBonesApi().RunBonesApi();
await WebApplication.CreateBuilder(args).BuildBonesApi().RunBonesApiAsync();
}

private static WebApplication BuildBonesApi(this WebApplicationBuilder builder)
Expand Down Expand Up @@ -99,7 +99,7 @@ private static WebApplication BuildBonesApi(this WebApplicationBuilder builder)
return builder.Build();
}

private static void RunBonesApi(this WebApplication app)
private static async Task RunBonesApiAsync(this WebApplication app)
{
using IServiceScope scope = app.Services.CreateScope();
ApiConfiguration apiConfig = scope.ServiceProvider.GetRequiredService<ApiConfiguration>();
Expand All @@ -108,8 +108,8 @@ private static void RunBonesApi(this WebApplication app)
{
configurePolicy
.WithOrigins(
apiConfig.WebUIBaseUrl ?? throw new BonesException("ApiConfiguration:WebUIBaseUrl missing from appsettings."),
apiConfig.ApiBaseUrl ?? throw new BonesException("ApiConfiguration:ApiBaseUrl missing from appsettings."))
apiConfig.CorsAllowedOrigins?.ToArray() ?? throw new BonesException("ApiConfiguration:CorsAllowedOrigins missing from appsettings.")
)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
Expand All @@ -135,7 +135,8 @@ private static void RunBonesApi(this WebApplication app)
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

app.Run();

Log.Information("Startup complete");
await app.RunAsync();
}
}
6 changes: 4 additions & 2 deletions Backend/Bones.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
}
},
"ApiConfiguration": {
"WebUIBaseUrl" : "http://localhost:9080",
"ApiBaseUrl": "http://localhost:8080"
"CorsAllowedOrigins": [
"http://localhost:9080",
"http://localhost:8080"
]
},
"BackendConfiguration": {
"WebUIBaseUrl" : "http://localhost:9080"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"Kestrel": {
"Endpoints": {
"HttpsWithCert": {
"Url": "",
"Url": "https://api.",
"SslProtocols": ["Tls12", "Tls13"],
"HttpProtocols": "Http1AndHttp2"
}
Expand All @@ -18,11 +18,13 @@
}
},
"ApiConfiguration": {
"WebUIBaseUrl": "",
"ApiBaseUrl": ""
"CorsAllowedOrigins": [
"https://",
"http://api."
]
},
"BackendConfiguration": {
"WebUIBaseUrl" : ""
"WebUIBaseUrl" : "https://"
},
"DatabaseConfiguration": {
"ConnectionString": "",
Expand Down
11 changes: 6 additions & 5 deletions Backend/Bones.BackgroundService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public static class Program
/// <param name="args"></param>
public static async Task Main(string[] args)
{
using CancellationTokenSource cts = new();
await Host.CreateApplicationBuilder(args).BuildBonesBackgroundService().RunBonesBackgroundService(cts.Token);
await Host.CreateApplicationBuilder(args).BuildBonesBackgroundService().RunBonesBackgroundService();
}

private static IHost BuildBonesBackgroundService(this HostApplicationBuilder builder)
Expand Down Expand Up @@ -51,9 +50,11 @@ private static IHost BuildBonesBackgroundService(this HostApplicationBuilder bui
return builder.Build();
}

private static async Task RunBonesBackgroundService(this IHost host, CancellationToken cancellationToken)
private static async Task RunBonesBackgroundService(this IHost host)
{
await host.Services.SetupDatabase(cancellationToken);
await host.RunAsync(cancellationToken);
await host.Services.SetupDatabase();

Log.Information("Startup complete");
await host.RunAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"SmtpPassword": ""
},
"BackendConfiguration": {
"WebUIBaseUrl" : ""
"WebUIBaseUrl" : "https://"
},
"DatabaseConfiguration": {
"ConnectionString": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class ServiceProviderExtensions
/// </summary>
/// <param name="serviceProvider"></param>
/// <param name="cancellationToken"></param>
public static async Task SetupDatabase(this IServiceProvider serviceProvider, CancellationToken cancellationToken)
public static async Task SetupDatabase(this IServiceProvider serviceProvider, CancellationToken cancellationToken = default)
{
using IServiceScope scope = serviceProvider.CreateScope();
ISender sender = scope.ServiceProvider.GetRequiredService<ISender>();
Expand Down
2 changes: 1 addition & 1 deletion Docs/Bones Docs/.obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@
},
"active": "63538227fcb0a17d",
"lastOpenFiles": [
"Development/Development Environment Setup.md",
"Development/Deployment of Frontend on Cloudflare Pages.md",
"Development/Deployment of Backend on Fedora Server.md",
"Development/Feature Areas/Account Management.md",
"Development/Overview.md",
"Development/Feature Areas/Asset Management.md",
"Development/Feature Areas",
"Development/Feature Areas/Work Item Management.md",
"Development/Development Environment Setup.md",
"Development/Project Relations.svg",
"Development",
"Welcome.md"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,14 @@ tmux kill-session -t Api
dnf update
# Go ahead while we're here

rm -rf pkgs/
mkdir pkgs

cd Bones/
gh release download nightly --dir ../
gh release download nightly --dir ../pkgs
cd ..

dnf install ./Bones.Api.0.0.1-nightly.linux-x64.rpm ./Bones.BackgroundService.0.0.1-nightly.linux-x64.rpm
dnf install ./pkgs/Bones.Api.0.0.1-nightly.linux-x64.rpm ./pkgs/Bones.BackgroundService.0.0.1-nightly.linux-x64.rpm

tmux new -d -s BackgroundService 'cd ~/bones/backgroundService; Bones.BackgroundService;'
tmux new -d -s Api 'cd ~/bones/api; Bones.Api'
Expand Down

0 comments on commit e5f484b

Please sign in to comment.