Skip to content

Commit

Permalink
Merge pull request #43 from argon-chat/feature/environment
Browse files Browse the repository at this point in the history
Feature/environment
  • Loading branch information
0xF6 authored Nov 15, 2024
2 parents a9f9e76 + 25398ce commit 629b059
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions Argon.Server.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<s:Boolean x:Key="/Default/InstalledDictionaries/InstalledDictionaries/=C_003A_005Cgit_005Chunspell_002Ddictionaries_005Cru_005FRU_005Cru_005FRU_002Edic/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=appsettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Behaviours/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=dockerenv/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=hwnd/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=livekit/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Rebalance/@EntryIndexedValue">True</s:Boolean>
Expand Down
2 changes: 1 addition & 1 deletion src/Argon.Api/Argon.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down
35 changes: 35 additions & 0 deletions src/Argon.Api/Features/Env/EnvironmentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Argon.Api.Features.Env;

using static File;

public static class EnvironmentExtensions
{
public static bool IsKube(this IHostEnvironment env)
=> env.Determine() == ArgonEnvironmentKind.Kubernetes;

public static bool IsDocker(this IHostEnvironment env)
=> env.Determine() == ArgonEnvironmentKind.Docker;

public static bool IsClassicHost(this IHostEnvironment env)
=> env.Determine() == ArgonEnvironmentKind.HostMachine;

public static bool IsManaged(this IHostEnvironment env)
=> env.IsDocker() || env.IsKube();

public static ArgonEnvironmentKind Determine(this IHostEnvironment _)
{
if (Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST") != null)
return ArgonEnvironmentKind.Kubernetes;
if (Exists("/.dockerenv") || Directory.Exists("/proc/self/cgroup") &&
ReadAllText("/proc/self/cgroup").Contains("docker"))
return ArgonEnvironmentKind.Docker;
return ArgonEnvironmentKind.HostMachine;
}
}

public enum ArgonEnvironmentKind
{
HostMachine,
Docker,
Kubernetes,
}
5 changes: 3 additions & 2 deletions src/Argon.Api/Features/Orleanse/BalanceRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Argon.Api.Features;

using System.Collections.Concurrent;
using System.Globalization;
using Env;
using k8s;
using Orleans.Placement.Repartitioning;
using static Math;
Expand All @@ -13,7 +14,7 @@ public static IServiceCollection AddKubeResources(this WebApplicationBuilder bui
{
var services = builder.Services;

if (builder.Environment.IsProduction())
if (builder.Environment.IsKube())
{
var config = KubernetesClientConfiguration.InClusterConfig();
services.AddSingleton(config);
Expand Down Expand Up @@ -49,7 +50,7 @@ public async ValueTask FetchAsync()

private async Task<double> GetAvgCpu()
{
if (!env.IsProduction())
if (!env.IsManaged())
return 10;
await using var
scope = serviceProvider.CreateAsyncScope();
Expand Down
7 changes: 4 additions & 3 deletions src/Argon.Api/Features/Orleanse/OrleansExtension.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Argon.Api.Features;

using Contracts;
using Env;
using Extensions;
using Orleans.Clustering.Kubernetes;
using Orleans.Configuration;
Expand Down Expand Up @@ -32,10 +33,10 @@ public static WebApplicationBuilder AddOrleans(this WebApplicationBuilder builde
.AddPersistentStreams(IArgonEvent.ProviderId, NatsAdapterFactory.Create, options => { }).UseDashboard(o => o.Port = 22832);
#pragma warning restore ORLEANSEXP001

if (builder.Environment.IsDevelopment())
siloBuilder.UseLocalhostClustering();
else
if (builder.Environment.IsKube())
siloBuilder.UseKubeMembership();
else
siloBuilder.UseLocalhostClustering();
});

return builder;
Expand Down
5 changes: 3 additions & 2 deletions src/Argon.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Argon.Api.Extensions;
using Argon.Api.Features;
using Argon.Api.Features.EmailForms;
using Argon.Api.Features.Env;
using Argon.Api.Features.Jwt;
using Argon.Api.Features.Otp;
using Argon.Api.Grains.Interfaces;
Expand All @@ -25,7 +26,7 @@
builder.AddNatsJetStream();
builder.AddNpgsqlDbContext<ApplicationDbContext>("DefaultConnection");
builder.Services.AddSingleton<IPasswordHashingService, PasswordHashingService>();
if (!builder.Environment.IsProduction())
if (!builder.Environment.IsManaged())
{
builder.AddJwt();
builder.Services.AddControllers().AddNewtonsoftJson();
Expand All @@ -46,7 +47,7 @@
builder.Services.AddAutoMapper(typeof(User).Assembly); // TODO
var app = builder.Build();

if (!builder.Environment.IsProduction())
if (!builder.Environment.IsManaged())
{
app.UseWebSockets();
app.MapRpcWebSocketServer();
Expand Down

0 comments on commit 629b059

Please sign in to comment.