Add Ping backend
.NET 10 solution for Ping, a Discord style chat app: Domain, Application and Infrastructure projects plus the Api host. SignalR for chat and presence, LiveKit for call tokens, EF Core on Postgres, S3 compatible storage for attachments, and OIDC auth shared with the Musify Zitadel instance. Includes the Compose stack for local development.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using Livekit.Server.Sdk.Dotnet;
|
||||
using Ping.Application.Contracts;
|
||||
using Ping.Infrastructure.Configuration;
|
||||
|
||||
namespace Ping.Infrastructure.Services
|
||||
{
|
||||
public class CallTokenService(LiveKitConfiguration configuration) : ICallTokenService
|
||||
{
|
||||
public CallAccessToken CreateAccessToken(string roomName, string identity, string? displayName)
|
||||
{
|
||||
var token = new AccessToken(configuration.ApiKey, configuration.ApiSecret)
|
||||
.WithIdentity(identity)
|
||||
.WithName(displayName ?? identity)
|
||||
.WithGrants(new VideoGrants
|
||||
{
|
||||
RoomJoin = true,
|
||||
Room = roomName,
|
||||
CanPublish = true,
|
||||
CanSubscribe = true,
|
||||
CanPublishData = true,
|
||||
})
|
||||
.WithTtl(TimeSpan.FromMinutes(configuration.TokenTtlMinutes));
|
||||
|
||||
return new CallAccessToken(token.ToJwt(), roomName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using Amazon.S3;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Ping.Application.Contracts;
|
||||
using Ping.Infrastructure.Configuration;
|
||||
|
||||
namespace Ping.Infrastructure.Services
|
||||
{
|
||||
public static class ServicesDependencyInjection
|
||||
{
|
||||
public static IServiceCollection AddStorageService(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors
|
||||
.AddOptionsWithValidateOnStart<InfrastructureStorageConfiguration>()
|
||||
.Bind(configuration.GetRequiredSection(InfrastructureStorageConfiguration.SectionName))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
serviceDescriptors.AddSingleton(serviceProvider =>
|
||||
serviceProvider.GetRequiredService<IOptions<InfrastructureStorageConfiguration>>().Value);
|
||||
|
||||
serviceDescriptors.AddSingleton<IAmazonS3>(serviceProvider =>
|
||||
{
|
||||
var storageConfiguration = serviceProvider.GetRequiredService<InfrastructureStorageConfiguration>();
|
||||
var s3Configuration = new AmazonS3Config
|
||||
{
|
||||
ServiceURL = storageConfiguration.Address,
|
||||
ForcePathStyle = storageConfiguration.ForcePathStyle,
|
||||
UseHttp = storageConfiguration.UseHttp,
|
||||
};
|
||||
return new AmazonS3Client(storageConfiguration.AccessKey, storageConfiguration.SecretAccessKey, s3Configuration);
|
||||
});
|
||||
|
||||
serviceDescriptors.AddScoped<IStorageService, StorageService>();
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddCallTokenService(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors
|
||||
.AddOptionsWithValidateOnStart<LiveKitConfiguration>()
|
||||
.Bind(configuration.GetRequiredSection(LiveKitConfiguration.SectionName))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
serviceDescriptors.AddSingleton(serviceProvider =>
|
||||
serviceProvider.GetRequiredService<IOptions<LiveKitConfiguration>>().Value);
|
||||
|
||||
serviceDescriptors.AddSingleton<ICallTokenService, CallTokenService>();
|
||||
return serviceDescriptors;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using Amazon.S3;
|
||||
using Amazon.S3.Model;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ping.Application.Contracts;
|
||||
using Ping.Infrastructure.Configuration;
|
||||
|
||||
namespace Ping.Infrastructure.Services
|
||||
{
|
||||
public class StorageService(IAmazonS3 amazonS3, ILogger<StorageService> logger, InfrastructureStorageConfiguration storageClientConfiguration)
|
||||
: IStorageService
|
||||
{
|
||||
public async Task<Stream> GetFileAsync(string bucket, string key, CancellationToken cancellationToken)
|
||||
{
|
||||
var request = new GetObjectRequest { BucketName = bucket, Key = key };
|
||||
|
||||
try
|
||||
{
|
||||
var response = await amazonS3.GetObjectAsync(request, cancellationToken);
|
||||
logger.LogDebug("Retrieved file from S3 {Bucket}/{Key}", bucket, key);
|
||||
return response.ResponseStream;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to get file from S3 {Bucket}/{Key}", bucket, key);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetUrlAsync(string bucket, string key, TimeSpan expirationTime, CancellationToken cancellationToken)
|
||||
{
|
||||
var request = new GetPreSignedUrlRequest
|
||||
{
|
||||
BucketName = bucket,
|
||||
Key = key,
|
||||
Expires = DateTime.UtcNow + expirationTime,
|
||||
Protocol = storageClientConfiguration.UseHttp ? Protocol.HTTP : Protocol.HTTPS,
|
||||
};
|
||||
|
||||
logger.LogDebug("Generating pre-signed URL for {Bucket}/{Key}", bucket, key);
|
||||
return await amazonS3.GetPreSignedURLAsync(request);
|
||||
}
|
||||
|
||||
public async Task<string> GetUploadUrlAsync(
|
||||
string bucket,
|
||||
string key,
|
||||
string contentType,
|
||||
TimeSpan expirationTime,
|
||||
CancellationToken cancellationToken,
|
||||
bool preventOverwrite = true)
|
||||
{
|
||||
var request = new GetPreSignedUrlRequest
|
||||
{
|
||||
BucketName = bucket,
|
||||
Key = key,
|
||||
Expires = DateTime.UtcNow + expirationTime,
|
||||
Protocol = storageClientConfiguration.UseHttp ? Protocol.HTTP : Protocol.HTTPS,
|
||||
Verb = HttpVerb.PUT,
|
||||
ContentType = contentType,
|
||||
};
|
||||
|
||||
if (preventOverwrite)
|
||||
request.Headers["If-None-Match"] = "*";
|
||||
|
||||
logger.LogDebug("Generating pre-signed upload URL for {Bucket}/{Key}", bucket, key);
|
||||
return await amazonS3.GetPreSignedURLAsync(request);
|
||||
}
|
||||
|
||||
public async Task<ObjectMetaData?> HeadObjectAsync(string bucket, string key, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await amazonS3.GetObjectMetadataAsync(bucket, key, cancellationToken);
|
||||
return new ObjectMetaData(response.Headers.ContentType, response.ContentLength);
|
||||
}
|
||||
catch (AmazonS3Exception ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "HEAD failed for {Bucket}/{Key}", bucket, key);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RemoveFileAsync(string bucket, string key, CancellationToken cancellationToken)
|
||||
{
|
||||
var request = new DeleteObjectRequest { BucketName = bucket, Key = key };
|
||||
|
||||
logger.LogDebug("Removing file from S3 {Bucket}/{Key}", bucket, key);
|
||||
await amazonS3.DeleteObjectAsync(request, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user