party migrate to Pilz.Net

This commit is contained in:
2024-08-22 15:12:57 +02:00
parent 1efc4fd77a
commit 255ba2348d
12 changed files with 30 additions and 137 deletions

View File

@@ -1,124 +1,58 @@
using Castle.Core.Logging;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using OwnChar.Data.Model.Base;
using OwnChar.Server.Api;
using OwnChar.Server.Api.Endpoint;
using OwnChar.Server.Api.Endpoint.Implementations;
using OwnChar.Server.Api.Plugins;
using OwnChar.Server.Data;
using Pilz.Configuration;
using Pilz.Cryptography;
using Pilz.Extensions.Collections;
using Pilz.Net.Api;
using Pilz.Plugins.Advanced;
using System.Net;
namespace OwnChar.Server;
internal class ServerContext(ISettings settings) : IServer
internal class ServerContext(ISettings settings) : ApiServer(settings.Get<ServerSettings>().ApiUrl!), IServer
{
private readonly Dictionary<string, UserAccountBase> users = [];
private readonly HttpListener httpListener = new();
private readonly ApiBuilder apiBuilder = new();
public DbContext? Data { get; private set; }
public ISettings Settings { get; } = settings;
public ILogger Log { get; set; } = NullLogger.Instance;
public void Start(string[] args)
public override void Start()
{
Log.Info("Prepairing server");
// Load database
Log.Debug("Loading database");
var settings = Settings.Get<ServerSettings>();
Log.Debug("Loading database");
Data = new DatabaseContext(settings.DbServer, settings.DbUser, settings.DbPassword);
// Built-in endpoints
Log.Debug("Loading internal api endpoints");
var apibuilder = new ApiBuilder();
new LoginApi(this).Initialize(apibuilder);
new UsersApi(this).Initialize(apibuilder);
new GroupsApi(this).Initialize(apibuilder);
new CharactersApi(this).Initialize(apibuilder);
RegisterHandler(new LoginApi(this));
RegisterHandler(new UsersApi(this));
RegisterHandler(new GroupsApi(this));
RegisterHandler(new CharactersApi(this));
// Plugin endpoints
Log.Debug("Loading plugin api endpoints");
var endpoints = PluginFeatureController.Instance.Features.Get(ApiEndpointFeature.FeatureType).OfType<IApiEndpoint>();
if (endpoints.Any())
{
foreach (var endpoint in endpoints)
endpoint.Initialize(apibuilder);
}
PluginFeatureController.Instance.Features.Get(ApiEndpointFeature.FeatureType).OfType<ApiEndpointFeature>().ForEach(n => n.OnServerInit(this));
// Run server
Log.Info("Starting webserver");
httpListener.Start();
Listen();
base.Start();
}
private void Listen()
protected override string? DecodeAuthKey(string authKey)
{
var apiUrl = Settings.Get<ServerSettings>().ApiUrl;
if (string.IsNullOrWhiteSpace(apiUrl))
throw new NullReferenceException("ApiUrl is empty!");
return base.DecodeAuthKey(new SecureString(authKey, true));
}
while (httpListener.IsListening)
{
var context = httpListener.GetContext();
if (context.Request.HttpMethod != HttpMethod.Post.Method
|| context.Request.ContentType is not string contentType
|| contentType.Contains("application/json")
|| context.Request.AcceptTypes is null
|| context.Request.AcceptTypes.Contains("application/json"))
{
close();
continue;
}
// Parse url
var path = context.Request.Url?.PathAndQuery.Replace(apiUrl, string.Empty);
if (string.IsNullOrWhiteSpace(path) || context.Request.ContentLength64 <= 0)
{
close();
continue;
}
// Find mapped function and get target type
if (!apiBuilder.Handlers.TryGetValue(new(path, context.Request.HttpMethod), out var postAction))
{
close();
return;
}
// ...
// Read input content
using StreamReader input = new(context.Request.InputStream);
var contentJson = input.ReadToEnd();
// Deserialize request
if (JsonHelpers.DeserializeRequest<T>(contentJson) is not T request)
{
close();
continue;
}
// Set response parameters
context.Response.StatusCode = (int)args.ResponseStatusCode;
context.Response.StatusDescription = args.ResponseStatusDescription;
// Write response content
if (args.ResponseContent != null)
{
context.Response.ContentType = ContentTypes.CONTENT_TYPE_JSON;
using StreamWriter output = new(context.Response.OutputStream);
output.Write(args.ResponseContent);
}
close();
void close() => context.Response.OutputStream.Close();
}
protected override bool CheckAuthentication(string authKey)
{
return base.CheckAuthentication(authKey) || IsLoggedIn(authKey);
}
public string Login(UserAccountBase account)