Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions frameworks/aspnet-minimal-twinflow/AppData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Text.Json;
using Npgsql;
using StackExchange.Redis;

static class AppData
{
public static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

public static List<DatasetItem>? DatasetItems;

public static NpgsqlDataSource? PgDataSource;

// Optional Redis cache for the crud profile. When REDIS_URL is set,
// crud handlers use Redis as a shared cache; otherwise they use the
// in-process IMemoryCache. Mirrors hono-bun's pattern so frameworks
// can be compared apples-to-apples on the same cache topology.
public static IDatabase? RedisDb;

public static void Load()
{
LoadDataset();
OpenPgPool();
OpenRedis();
}

static void LoadDataset()
{
var path = Environment.GetEnvironmentVariable("DATASET_PATH") ?? "/data/dataset.json";
if (!File.Exists(path)) return;
DatasetItems = JsonSerializer.Deserialize<List<DatasetItem>>(File.ReadAllText(path), JsonOptions);
}

static void OpenPgPool()
{
var dbUrl = Environment.GetEnvironmentVariable("DATABASE_URL");
if (string.IsNullOrEmpty(dbUrl)) return;
try
{
var uri = new Uri(dbUrl);
var userInfo = uri.UserInfo.Split(':');
var maxConn = int.TryParse(Environment.GetEnvironmentVariable("DATABASE_MAX_CONN"), out var p) && p > 0 ? p : 256;
var minConn = Math.Min(64, maxConn);
var connStr = $"Host={uri.Host};Port={uri.Port};Username={userInfo[0]};Password={userInfo[1]};Database={uri.AbsolutePath.TrimStart('/')};Maximum Pool Size={maxConn};Minimum Pool Size={minConn};Multiplexing=true;No Reset On Close=true;Max Auto Prepare=20;Auto Prepare Min Usages=1";
var builder = new NpgsqlDataSourceBuilder(connStr);
PgDataSource = builder.Build();
}
catch { }
}

static void OpenRedis()
{
var redisUrl = Environment.GetEnvironmentVariable("REDIS_URL");
if (string.IsNullOrEmpty(redisUrl)) return;
try
{
// REDIS_URL is "redis://host:port" — convert to StackExchange's
// "host:port" configuration string.
var uri = new Uri(redisUrl);
var config = ConfigurationOptions.Parse($"{uri.Host}:{uri.Port}");
config.AbortOnConnectFail = false;
var muxer = ConnectionMultiplexer.Connect(config);
RedisDb = muxer.GetDatabase();
}
catch { }
}
}
22 changes: 22 additions & 0 deletions frameworks/aspnet-minimal-twinflow/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /app
COPY frameworks/aspnet-minimal-twinflow/ .
COPY data/static/ wwwroot/static/
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:10.0


ADD https://packages.microsoft.com/config/debian/12/packages-microsoft-prod.deb /packages-microsoft-prod.deb

RUN dpkg -i packages-microsoft-prod.deb && rm packages-microsoft-prod.deb \
&& apt-get update \
&& apt-get install -y --no-install-recommends libmsquic \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY --from=build /app/out .
EXPOSE 8080 8081 8082 8443/tcp 8443/udp

ENTRYPOINT ["dotnet", "aspnet-minimal.dll"]
Loading
Loading