Add missing files
This commit is contained in:
parent
494f6b1e00
commit
5b4dc640bd
80
NostrRelay/Extensions.cs
Normal file
80
NostrRelay/Extensions.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using System.Net.WebSockets;
|
||||
using Newtonsoft.Json;
|
||||
using Nostr.Client.Json;
|
||||
|
||||
namespace NostrRelay;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static void MapRelay<TRelay>(this WebApplication app, string path,
|
||||
RelayDocument? document = null,
|
||||
string? landingPage = null)
|
||||
where TRelay : INostrRelay, IDisposable
|
||||
{
|
||||
app.MapGet(path, async ctx =>
|
||||
{
|
||||
if (ctx.WebSockets.IsWebSocketRequest)
|
||||
{
|
||||
var logger = app.Services.GetRequiredService<ILogger<NostrRelay<TRelay>>>();
|
||||
using var handler = app.Services.GetRequiredService<TRelay>();
|
||||
try
|
||||
{
|
||||
var ws = await ctx.WebSockets.AcceptWebSocketAsync();
|
||||
var wsCtx = new NostrClientContext(ws, ctx.Connection.RemoteIpAddress!,
|
||||
ctx.Request.Headers.UserAgent.FirstOrDefault() ?? string.Empty);
|
||||
|
||||
var nostrRelay = new NostrRelay<TRelay>(handler, wsCtx, ctx.RequestAborted, logger);
|
||||
|
||||
await nostrRelay.Read();
|
||||
|
||||
await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, null, ctx.RequestAborted);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogDebug(ex.Message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var isRelayDocRequest = ctx.Request.Headers.Accept.FirstOrDefault()
|
||||
?.Equals("application/nostr+json", StringComparison.InvariantCultureIgnoreCase) ?? false;
|
||||
|
||||
if (isRelayDocRequest)
|
||||
{
|
||||
var doc = document ?? new RelayDocument
|
||||
{
|
||||
Name = typeof(TRelay).Name,
|
||||
Description = "NostrServices C# Relay Framework",
|
||||
Pubkey = "63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed",
|
||||
SupportedNips = [1],
|
||||
Software = "NostrServices",
|
||||
Icon = "https://void.cat/d/CtXsF5EvqNLG3K85TgezCt.webp",
|
||||
Limitation = new()
|
||||
{
|
||||
RestrictedWrites = false,
|
||||
AuthRequired = false,
|
||||
PaymentRequired = false
|
||||
}
|
||||
};
|
||||
|
||||
ctx.Response.ContentType = "application/json";
|
||||
await ctx.Response.WriteAsync(JsonConvert.SerializeObject(doc, NostrSerializer.Settings));
|
||||
}
|
||||
else
|
||||
{
|
||||
ctx.Response.ContentType = "text/html";
|
||||
await ctx.Response.WriteAsync(landingPage ?? @$"
|
||||
<html>
|
||||
<head>
|
||||
<title>NostrServices Relay</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome to {typeof(TRelay).Name}</h1>
|
||||
</body>
|
||||
</html>
|
||||
");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,9 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<OutputType>Library</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -5,6 +5,7 @@ using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Nostr.Client.Client;
|
||||
using NostrRelay;
|
||||
using NostrServices.Database;
|
||||
using NostrServices.Services;
|
||||
using NostrServices.Services.EventHandlers;
|
||||
@ -106,7 +107,7 @@ public static class Program
|
||||
app.UseRouting();
|
||||
app.MapControllers();
|
||||
app.MapMetrics();
|
||||
app.MapNostrRelay<CacheRelay>("/");
|
||||
app.MapRelay<CacheRelay>("/");
|
||||
|
||||
await app.RunAsync();
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ using NostrRelay;
|
||||
|
||||
namespace NostrServices.Services;
|
||||
|
||||
public class CacheRelay : INostrRelay
|
||||
public class CacheRelay : INostrRelay, IDisposable
|
||||
{
|
||||
private readonly ILogger<CacheRelay> _logger;
|
||||
private readonly RedisStore _redisStore;
|
||||
@ -37,4 +37,9 @@ public class CacheRelay : INostrRelay
|
||||
|
||||
return new(false, "blocked: kind not accepted");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// TODO release managed resources here
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user