chat: add hello controller
This commit is contained in:
parent
6f6e19bbb5
commit
7b4cbd4fb7
12 changed files with 114 additions and 53 deletions
38
Foxchat.Core/Extensions/HttpContextExtensions.cs
Normal file
38
Foxchat.Core/Extensions/HttpContextExtensions.cs
Normal file
|
@ -0,0 +1,38 @@
|
|||
using Foxchat.Core.Federation;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Foxchat.Core.Extensions;
|
||||
|
||||
public static class HttpContextExtensions
|
||||
{
|
||||
public static bool ExtractRequestData(this HttpContext ctx, out string signature, out string domain, out SignatureData data)
|
||||
{
|
||||
signature = string.Empty;
|
||||
domain = string.Empty;
|
||||
data = SignatureData.Empty;
|
||||
|
||||
if (!ctx.Request.Headers.TryGetValue(RequestSigningService.SIGNATURE_HEADER, out var encodedSignature))
|
||||
return false;
|
||||
if (!ctx.Request.Headers.TryGetValue(RequestSigningService.DATE_HEADER, out var date))
|
||||
return false;
|
||||
if (!ctx.Request.Headers.TryGetValue(RequestSigningService.SERVER_HEADER, out var server))
|
||||
return false;
|
||||
var time = RequestSigningService.ParseTime(date.ToString());
|
||||
string? userId = null;
|
||||
if (ctx.Request.Headers.TryGetValue(RequestSigningService.USER_HEADER, out var userIdHeader))
|
||||
userId = userIdHeader;
|
||||
var host = ctx.Request.Headers.Host.ToString();
|
||||
|
||||
signature = encodedSignature.ToString();
|
||||
domain = server.ToString();
|
||||
data = new SignatureData(
|
||||
time,
|
||||
host,
|
||||
ctx.Request.Path,
|
||||
(int?)ctx.Request.Headers.ContentLength,
|
||||
userId
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
79
Foxchat.Core/Extensions/ServiceCollectionExtensions.cs
Normal file
79
Foxchat.Core/Extensions/ServiceCollectionExtensions.cs
Normal file
|
@ -0,0 +1,79 @@
|
|||
using Foxchat.Core.Database;
|
||||
using Foxchat.Core.Federation;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NodaTime;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
|
||||
namespace Foxchat.Core;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds Serilog to this service collection. This method also initializes Serilog so it should be called as early as possible, before any log calls.
|
||||
/// </summary>
|
||||
public static void AddSerilog(this WebApplicationBuilder builder, LogEventLevel level)
|
||||
{
|
||||
var config = builder.Configuration.Get<CoreConfig>() ?? new();
|
||||
|
||||
var logCfg = new LoggerConfiguration()
|
||||
.Enrich.FromLogContext()
|
||||
.MinimumLevel.Is(level)
|
||||
// ASP.NET's built in request logs are extremely verbose, so we use Serilog's instead.
|
||||
// Serilog doesn't disable the built in logs so we do it here.
|
||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore.Mvc", LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore.Routing", LogEventLevel.Warning)
|
||||
.WriteTo.Console();
|
||||
|
||||
if (config.SeqLogUrl != null)
|
||||
logCfg.WriteTo.Seq(config.SeqLogUrl, restrictedToMinimumLevel: LogEventLevel.Verbose);
|
||||
|
||||
Log.Logger = logCfg.CreateLogger();
|
||||
|
||||
// AddSerilog doesn't seem to add an ILogger to the service collection, so add that manually.
|
||||
builder.Services.AddSerilog().AddSingleton(Log.Logger);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the core Foxchat services to this service collection.
|
||||
/// </summary>
|
||||
public static IServiceCollection AddCoreServices<T>(this IServiceCollection services) where T : IDatabaseContext
|
||||
{
|
||||
services.AddDbContext<T>();
|
||||
|
||||
// NodaTime recommends only depending on the IClock interface, not the singleton.
|
||||
services.AddSingleton<IClock>(SystemClock.Instance);
|
||||
// Some core services rely on an IDatabaseContext, not the server-specific context type.
|
||||
services.AddScoped<IDatabaseContext, T>();
|
||||
services.AddSingleton<RequestSigningService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static T AddConfiguration<T>(this WebApplicationBuilder builder, string? configFile = null) where T : class, new()
|
||||
{
|
||||
|
||||
builder.Configuration.Sources.Clear();
|
||||
builder.Configuration.AddConfiguration(configFile);
|
||||
|
||||
var config = builder.Configuration.Get<T>() ?? new();
|
||||
var coreConfig = builder.Configuration.Get<CoreConfig>() ?? new();
|
||||
builder.Services.AddSingleton(config);
|
||||
builder.Services.AddSingleton(coreConfig);
|
||||
return config;
|
||||
}
|
||||
|
||||
public static IConfigurationBuilder AddConfiguration(this IConfigurationBuilder builder, string? configFile = null)
|
||||
{
|
||||
var file = Environment.GetEnvironmentVariable("FOXCHAT_CONFIG_FILE") ?? configFile ?? "config.ini";
|
||||
|
||||
return builder
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddIniFile(file, optional: false, reloadOnChange: true)
|
||||
.AddEnvironmentVariables();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue