Skip to content

Commit 4534d4d

Browse files
admin api packaging
1 parent a4527d1 commit 4534d4d

124 files changed

Lines changed: 2246 additions & 264 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ bun.lock
2626
# optional generated outputs from schema.js
2727
schemas_orig/
2828
schemas_nested/
29-
schemas_final/
29+
schemas_final/
30+
31+
!/extra/admin-api/Utilities/Spacebar.AdminApi.TestClient/wwwroot/lib/bootstrap/dist/

extra/admin-api/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ appsettings.Local*.json
66
/*.patch
77

88
Spacebar.Db/**/*.orig
9-
Spacebar.Db/**/*.rej
9+
Spacebar.Db/**/*.rej
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Worker">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<UserSecretsId>dotnet-ConfigTest-18d89c0e-df5d-447b-8429-7d526a35ab13</UserSecretsId>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
12+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
13+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\Spacebar.ConfigModel\Spacebar.ConfigModel.csproj" />
18+
<ProjectReference Include="..\Spacebar.Db\Spacebar.Db.csproj" />
19+
</ItemGroup>
20+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using ConfigTest;
2+
using Microsoft.EntityFrameworkCore;
3+
using Spacebar.Db.Contexts;
4+
5+
var builder = Host.CreateApplicationBuilder(args);
6+
builder.Services.AddHostedService<Worker>();
7+
builder.Services.AddDbContext<SpacebarDbContext>(options => {
8+
options
9+
.UseNpgsql(builder.Configuration.GetConnectionString("Spacebar"))
10+
.EnableDetailedErrors();
11+
}, ServiceLifetime.Singleton);
12+
13+
var host = builder.Build();
14+
host.Run();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"ConfigTest": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"environmentVariables": {
8+
"DOTNET_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Text.Json.Nodes;
2+
using Spacebar.ConfigModel.Extensions;
3+
using Spacebar.Db.Contexts;
4+
5+
namespace ConfigTest;
6+
7+
public class Worker(ILogger<Worker> logger, SpacebarDbContext db) : BackgroundService
8+
{
9+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
10+
{
11+
var config = db.Configs
12+
.OrderBy(x => x.Key)
13+
.ToDictionary(x => x.Key, x => x.Value);
14+
foreach (var (key, value) in config)
15+
{
16+
Console.WriteLine("Config Key: {0}, Value: {1}", key, value ?? "[NULL]");
17+
}
18+
19+
var readConfig = config.ToNestedJsonObject();
20+
Console.WriteLine(readConfig);
21+
var mapped = readConfig.ToFlatKv();
22+
foreach (var (key, value) in mapped)
23+
{
24+
Console.WriteLine("Mapped Key: {0}, Value: {1}", key, value ?? "[NULL]");
25+
}
26+
27+
// check that they're equal
28+
foreach (var (key, value) in config)
29+
{
30+
if (!mapped.ContainsKey(key))
31+
{
32+
Console.WriteLine("Missing Key in Mapped: {0}", key);
33+
continue;
34+
}
35+
36+
if (mapped[key] != value)
37+
{
38+
Console.WriteLine("Value Mismatch for Key: {0}, Original: {1}, Mapped: {2}", key, value ?? "[NULL]", mapped[key] ?? "[NULL]");
39+
}
40+
}
41+
Environment.Exit(0);
42+
}
43+
}

extra/admin-api/Spacebar.AdminAPI/appsettings.Development.json renamed to extra/admin-api/ConfigTest/appsettings.Development.json

File renamed without changes.
File renamed without changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Text.Json.Nodes;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.EntityFrameworkCore;
4+
using Spacebar.AdminApi.Extensions;
5+
using Spacebar.AdminApi.Models;
6+
using Spacebar.AdminApi.Services;
7+
using Spacebar.Db.Contexts;
8+
using Spacebar.Db.Models;
9+
using Spacebar.RabbitMqUtilities;
10+
using Spacebar.ConfigModel.Extensions;
11+
12+
namespace Spacebar.AdminApi.Controllers;
13+
14+
[ApiController]
15+
[Route("/Configuration")]
16+
public class ConfigController(ILogger<ConfigController> logger, SpacebarDbContext db, RabbitMQService mq, IServiceProvider sp, AuthenticationService auth) : ControllerBase {
17+
private readonly ILogger<ConfigController> _logger = logger;
18+
19+
[HttpGet]
20+
public async Task<JsonObject> Get() {
21+
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
22+
23+
var config = (await db.Configs.AsNoTracking().ToDictionaryAsync(x => x.Key, x => x.Value)).ToNestedJsonObject();
24+
return config;
25+
}
26+
27+
[HttpPost]
28+
public async Task<IActionResult> Post([FromBody] JsonObject newConfig) {
29+
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
30+
31+
var flatConfig = newConfig.ToFlatKv();
32+
var tasks = flatConfig.Select(async x => {
33+
await using var scope = sp.CreateAsyncScope();
34+
var scopedDb = scope.ServiceProvider.GetRequiredService<SpacebarDbContext>();
35+
var existingConfig = await scopedDb.Configs.FindAsync(x.Key);
36+
if (existingConfig != null) {
37+
existingConfig.Value = x.Value;
38+
scopedDb.Configs.Update(existingConfig);
39+
}
40+
else {
41+
await scopedDb.Configs.AddAsync(new Config
42+
{ Key = x.Key, Value = x.Value });
43+
}
44+
45+
await scopedDb.SaveChangesAsync();
46+
});
47+
await Task.WhenAll(tasks);
48+
// TODO: rabbitmq
49+
50+
return Ok();
51+
}
52+
53+
[HttpPost]
54+
public async Task<IActionResult> ReloadConfig() {
55+
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
56+
57+
// TODO: rabbitmq
58+
59+
return Ok();
60+
}
61+
}

extra/admin-api/Spacebar.AdminAPI/Controllers/GuildController.cs renamed to extra/admin-api/Spacebar.AdminApi/Controllers/GuildController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.EntityFrameworkCore;
44
using RabbitMQ.Client;
5-
using Spacebar.AdminAPI.Extensions;
5+
using Spacebar.AdminApi.Extensions;
66
using Spacebar.AdminApi.Models;
7-
using Spacebar.AdminAPI.Services;
7+
using Spacebar.AdminApi.Services;
88
using Spacebar.Db.Contexts;
99
using Spacebar.Db.Models;
1010
using Spacebar.RabbitMqUtilities;
1111

12-
namespace Spacebar.AdminAPI.Controllers;
12+
namespace Spacebar.AdminApi.Controllers;
1313

1414
[ApiController]
1515
[Route("/Guilds")]

0 commit comments

Comments
 (0)