From 1a976fc9ef58bb20ac044b21e5af47fe137fdee7 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Thu, 18 Jul 2024 14:44:29 +0200 Subject: [PATCH] begin new and last rework --- OwnChar.Server.sln | 6 ++ OwnChar.Server/Api/Endpoint/ApiBuilder.cs | 25 +++++++ .../Api/Endpoint/ApiRequestMethods.cs | 12 ++++ OwnChar.Server/Api/Endpoint/IApiBuilder.cs | 6 ++ OwnChar.Server/Api/Endpoint/IApiEndpoint.cs | 6 ++ .../Endpoint/Implementations/CharactersApi.cs | 34 ++++++++++ .../Api/Endpoint/Implementations/GroupsApi.cs | 48 +++++++++++++ .../Api/Endpoint/Implementations/LoginApi.cs | 31 +++++++++ .../Api/Endpoint/Implementations/UsersApi.cs | 44 ++++++++++++ OwnChar.Server/Api/IServer.cs | 21 ++++++ .../Api/Plugins/ApiEndpointFeature.cs | 11 +++ .../Api/Plugins/IPluginLoadContextServer.cs | 5 ++ .../Plugins/OwnCharServerPluginInitParams.cs | 8 +++ OwnChar.Server/Commands/CmdSave.cs | 17 ----- OwnChar.Server/Commands/IServerCommand.cs | 15 ----- .../Data/ClientServerDataProvider.cs | 17 ----- OwnChar.Server/Data/IServerDataProvider.cs | 19 ------ OwnChar.Server/Network/NetworkHandler.cs | 12 ---- OwnChar.Server/Network/NetworkManager.cs | 12 ---- OwnChar.Server/OwnChar.Server.csproj | 18 +++-- OwnChar.Server/OwnChar.Server.http | 6 ++ OwnChar.Server/Program.cs | 67 +++++++++++++++++-- OwnChar.Server/Properties/launchSettings.json | 41 ++++++++++++ OwnChar.Server/ServerContext.cs | 47 +++++++++++++ OwnChar.Server/appsettings.Development.json | 8 +++ OwnChar.Server/appsettings.json | 9 +++ 26 files changed, 444 insertions(+), 101 deletions(-) create mode 100644 OwnChar.Server/Api/Endpoint/ApiBuilder.cs create mode 100644 OwnChar.Server/Api/Endpoint/ApiRequestMethods.cs create mode 100644 OwnChar.Server/Api/Endpoint/IApiBuilder.cs create mode 100644 OwnChar.Server/Api/Endpoint/IApiEndpoint.cs create mode 100644 OwnChar.Server/Api/Endpoint/Implementations/CharactersApi.cs create mode 100644 OwnChar.Server/Api/Endpoint/Implementations/GroupsApi.cs create mode 100644 OwnChar.Server/Api/Endpoint/Implementations/LoginApi.cs create mode 100644 OwnChar.Server/Api/Endpoint/Implementations/UsersApi.cs create mode 100644 OwnChar.Server/Api/IServer.cs create mode 100644 OwnChar.Server/Api/Plugins/ApiEndpointFeature.cs create mode 100644 OwnChar.Server/Api/Plugins/IPluginLoadContextServer.cs create mode 100644 OwnChar.Server/Api/Plugins/OwnCharServerPluginInitParams.cs delete mode 100644 OwnChar.Server/Commands/CmdSave.cs delete mode 100644 OwnChar.Server/Commands/IServerCommand.cs delete mode 100644 OwnChar.Server/Data/ClientServerDataProvider.cs delete mode 100644 OwnChar.Server/Data/IServerDataProvider.cs delete mode 100644 OwnChar.Server/Network/NetworkHandler.cs delete mode 100644 OwnChar.Server/Network/NetworkManager.cs create mode 100644 OwnChar.Server/OwnChar.Server.http create mode 100644 OwnChar.Server/Properties/launchSettings.json create mode 100644 OwnChar.Server/ServerContext.cs create mode 100644 OwnChar.Server/appsettings.Development.json create mode 100644 OwnChar.Server/appsettings.json diff --git a/OwnChar.Server.sln b/OwnChar.Server.sln index d387317..279eeda 100644 --- a/OwnChar.Server.sln +++ b/OwnChar.Server.sln @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OwnChar.Server", "OwnChar.S EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OwnChar", "OwnChar\OwnChar\OwnChar.csproj", "{BAD88A97-650D-493B-BAC3-3510B6F354B6}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OwnChar.Plugins", "OwnChar.Plugins\OwnChar.Plugins\OwnChar.Plugins.csproj", "{45BB6992-127F-4C4A-A7E3-B32F414784DE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {BAD88A97-650D-493B-BAC3-3510B6F354B6}.Debug|Any CPU.Build.0 = Debug|Any CPU {BAD88A97-650D-493B-BAC3-3510B6F354B6}.Release|Any CPU.ActiveCfg = Release|Any CPU {BAD88A97-650D-493B-BAC3-3510B6F354B6}.Release|Any CPU.Build.0 = Release|Any CPU + {45BB6992-127F-4C4A-A7E3-B32F414784DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {45BB6992-127F-4C4A-A7E3-B32F414784DE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {45BB6992-127F-4C4A-A7E3-B32F414784DE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {45BB6992-127F-4C4A-A7E3-B32F414784DE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/OwnChar.Server/Api/Endpoint/ApiBuilder.cs b/OwnChar.Server/Api/Endpoint/ApiBuilder.cs new file mode 100644 index 0000000..bf8cbfa --- /dev/null +++ b/OwnChar.Server/Api/Endpoint/ApiBuilder.cs @@ -0,0 +1,25 @@ +namespace OwnChar.ServerNew.Api.Endpoint; + +internal class ApiBuilder(WebApplication app) : IApiBuilder +{ + public void Map(string method, string pattern, Delegate action) + { + if (method == ApiRequestMethods.Get) + app.MapGet(pattern, action); + else if (method == ApiRequestMethods.Post) + app.MapPost(pattern, action); + else if (method == ApiRequestMethods.Put) + app.MapPut(pattern, action); + else if (method == ApiRequestMethods.Patch) + app.MapPatch(pattern, action); + else if (method == ApiRequestMethods.Delete) + app.MapDelete(pattern, action); + else + throw new NotSupportedException(); + } + + public void Map(string pattern, Delegate action) + { + app.Map(pattern, action); + } +} diff --git a/OwnChar.Server/Api/Endpoint/ApiRequestMethods.cs b/OwnChar.Server/Api/Endpoint/ApiRequestMethods.cs new file mode 100644 index 0000000..1166eb2 --- /dev/null +++ b/OwnChar.Server/Api/Endpoint/ApiRequestMethods.cs @@ -0,0 +1,12 @@ +using System.Net; + +namespace OwnChar.ServerNew.Api.Endpoint; + +public static class ApiRequestMethods +{ + public static string Get => WebRequestMethods.Http.Get; + public static string Put => WebRequestMethods.Http.Put; + public static string Post => WebRequestMethods.Http.Post; + public static string Patch => "PATCH"; + public static string Delete => "DELETE"; +} diff --git a/OwnChar.Server/Api/Endpoint/IApiBuilder.cs b/OwnChar.Server/Api/Endpoint/IApiBuilder.cs new file mode 100644 index 0000000..b35f5fd --- /dev/null +++ b/OwnChar.Server/Api/Endpoint/IApiBuilder.cs @@ -0,0 +1,6 @@ +namespace OwnChar.ServerNew.Api.Endpoint; + +public interface IApiBuilder +{ + void Map(string path, Delegate action); +} diff --git a/OwnChar.Server/Api/Endpoint/IApiEndpoint.cs b/OwnChar.Server/Api/Endpoint/IApiEndpoint.cs new file mode 100644 index 0000000..50ea90b --- /dev/null +++ b/OwnChar.Server/Api/Endpoint/IApiEndpoint.cs @@ -0,0 +1,6 @@ +namespace OwnChar.ServerNew.Api.Endpoint; + +internal interface IApiEndpoint +{ + void Initialize(IApiBuilder builder); +} diff --git a/OwnChar.Server/Api/Endpoint/Implementations/CharactersApi.cs b/OwnChar.Server/Api/Endpoint/Implementations/CharactersApi.cs new file mode 100644 index 0000000..dd4ba0d --- /dev/null +++ b/OwnChar.Server/Api/Endpoint/Implementations/CharactersApi.cs @@ -0,0 +1,34 @@ +namespace OwnChar.ServerNew.Api.Endpoint.Implementations; + +internal class CharactersApi(IServer server) : IApiEndpoint +{ + public void Initialize(IApiBuilder builder) + { + throw new NotImplementedException(); + } + + private IResult GetCharacter(long characterId) + { + + } + + private IResult CreateGroupCharacter(string name, long groupId) + { + + } + + private IResult CreateUserCharacter(string name, long userId) + { + + } + + private IResult UpdateCharacter(long characterId) + { + + } + + private IResult DeleteCharacter(long characterId) + { + + } +} diff --git a/OwnChar.Server/Api/Endpoint/Implementations/GroupsApi.cs b/OwnChar.Server/Api/Endpoint/Implementations/GroupsApi.cs new file mode 100644 index 0000000..3bc7501 --- /dev/null +++ b/OwnChar.Server/Api/Endpoint/Implementations/GroupsApi.cs @@ -0,0 +1,48 @@ +namespace OwnChar.ServerNew.Api.Endpoint.Implementations; + +internal class GroupsApi(IServer server) : IApiEndpoint +{ + public void Initialize(IApiBuilder builder) + { + } + + private IResult GetGroups() + { + + } + + private IResult GetGroups(long characterId) + { + + } + + private IResult GetGroup(long groupId) + { + + } + + private IResult CreateGroup(string name) + { + + } + + private IResult UpdateGroup(int groupId, string name) + { + + } + + private IResult DeleteGroup(int groupId) + { + + } + + private IResult AddMember(int groupId, long memberId) + { + + } + + private IResult RemoveMember(int groupId, long memberId) + { + + } +} diff --git a/OwnChar.Server/Api/Endpoint/Implementations/LoginApi.cs b/OwnChar.Server/Api/Endpoint/Implementations/LoginApi.cs new file mode 100644 index 0000000..4fd4ad9 --- /dev/null +++ b/OwnChar.Server/Api/Endpoint/Implementations/LoginApi.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Mvc; +using OwnChar.Api.Responses; +using OwnChar.Model; + +namespace OwnChar.ServerNew.Api.Endpoint.Implementations; + +internal class LoginApi(ServerContext server) : IApiEndpoint +{ + public void Initialize(IApiBuilder builder) + { + builder.Map("/auth/login/{username}", Login); + builder.Map("/auth/logout/{secret}", Logout); + } + + private IResult Login(string username, [FromHeader(Name = "X-USER-PASSWORD")] string password) + { + if (server.Data != null && server.Data.GetAll()?.FirstOrDefault(n => n.Username == username && n.Password == password) is UserAccount account) + return TypedResults.Ok(new LoginResponse + { + Secret = server.Login(account), + UserAccount = account, + }); + return TypedResults.Unauthorized(); + } + + private IResult Logout([FromHeader(Name = "X-AUTH-SECRET")] string secret) + { + server.Logout(secret); + return TypedResults.Ok(); + } +} diff --git a/OwnChar.Server/Api/Endpoint/Implementations/UsersApi.cs b/OwnChar.Server/Api/Endpoint/Implementations/UsersApi.cs new file mode 100644 index 0000000..ad46959 --- /dev/null +++ b/OwnChar.Server/Api/Endpoint/Implementations/UsersApi.cs @@ -0,0 +1,44 @@ +namespace OwnChar.ServerNew.Api.Endpoint.Implementations; + +internal class UsersApi(IServer server) : IApiEndpoint +{ + public void Initialize(IApiBuilder builder) + { + throw new NotImplementedException(); + } + + private IResult GetUsers() + { + + } + + private IResult GetUser(long userId) + { + + } + + private IResult GetUserProfile(long userId) + { + + } + + private IResult CreateUser(string username, string password) + { + + } + + private IResult DeleteUser(long userId) + { + + } + + private IResult UpdateUserPassword(long userId, string username, string password) + { + + } + + private IResult UpdateUserProfile(long profileId, string displayName) + { + + } +} diff --git a/OwnChar.Server/Api/IServer.cs b/OwnChar.Server/Api/IServer.cs new file mode 100644 index 0000000..f87ab86 --- /dev/null +++ b/OwnChar.Server/Api/IServer.cs @@ -0,0 +1,21 @@ +using OwnChar.Data; +using OwnChar.Model; +using Pilz.Configuration; +using System.Diagnostics.CodeAnalysis; + +namespace OwnChar.ServerNew.Api; + +public interface IServer +{ + ISettings Settings { get; } + IDataProvider? Data { get; } + + [MemberNotNull(nameof(Data))] + void CheckLogin(string secret); + + [MemberNotNullWhen(true, nameof(Data))] + bool IsLoggedIn(string secret); + + [MemberNotNullWhen(true, nameof(Data))] + UserAccount? GetUser(string secret); +} diff --git a/OwnChar.Server/Api/Plugins/ApiEndpointFeature.cs b/OwnChar.Server/Api/Plugins/ApiEndpointFeature.cs new file mode 100644 index 0000000..e10257e --- /dev/null +++ b/OwnChar.Server/Api/Plugins/ApiEndpointFeature.cs @@ -0,0 +1,11 @@ +using OwnChar.ServerNew.Api.Endpoint; +using Pilz.Plugins.Advanced; + +namespace OwnChar.ServerNew.Api.Plugins; + +public abstract class ApiEndpointFeature(string identifier) : PluginFeature(FeatureType, identifier), IApiEndpoint +{ + public static string FeatureType => "ownchar.server.apiep"; + + public abstract void Initialize(IApiBuilder builder); +} diff --git a/OwnChar.Server/Api/Plugins/IPluginLoadContextServer.cs b/OwnChar.Server/Api/Plugins/IPluginLoadContextServer.cs new file mode 100644 index 0000000..15b6615 --- /dev/null +++ b/OwnChar.Server/Api/Plugins/IPluginLoadContextServer.cs @@ -0,0 +1,5 @@ +namespace OwnChar.ServerNew.Api.Plugins; + +public interface IPluginLoadContextServer +{ +} diff --git a/OwnChar.Server/Api/Plugins/OwnCharServerPluginInitParams.cs b/OwnChar.Server/Api/Plugins/OwnCharServerPluginInitParams.cs new file mode 100644 index 0000000..8943d19 --- /dev/null +++ b/OwnChar.Server/Api/Plugins/OwnCharServerPluginInitParams.cs @@ -0,0 +1,8 @@ +using OwnChar.Plugins; + +namespace OwnChar.ServerNew.Api.Plugins; + +public class OwnCharServerPluginInitParams(IServer server) : OwnCharPluginInitParams +{ + public IServer Server { get; } = server; +} diff --git a/OwnChar.Server/Commands/CmdSave.cs b/OwnChar.Server/Commands/CmdSave.cs deleted file mode 100644 index 0edc0a3..0000000 --- a/OwnChar.Server/Commands/CmdSave.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Pilz.Plugins.Advanced; - -namespace OwnChar.Server.Commands -{ - public class CmdSave() : PluginFunction(IServerCommand.FeatureCode, "ownchar.save"), IPluginFeatureProvider, IServerCommand - { - public static CmdSave Instance { get; } = new(); - - public string Command => "save"; - public string Description => "Saves the current state to disk."; - - protected override object? ExecuteFunction(PluginFunctionParameter? @params) - { - return this; - } - } -} diff --git a/OwnChar.Server/Commands/IServerCommand.cs b/OwnChar.Server/Commands/IServerCommand.cs deleted file mode 100644 index 087c95e..0000000 --- a/OwnChar.Server/Commands/IServerCommand.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Pilz.Plugins.Advanced; - -namespace OwnChar.Server.Commands -{ - public interface IServerCommand - { - // Shared - public const string FeatureCode = "ownchar.server.command"; - public IEnumerable Commands => PluginFeatureController.Instance.Features.Get(FeatureCode).Cast().Select(f => (IServerCommand)f.Execute()!); - - // Interface - public string Command { get; } - public string Description { get; } - } -} diff --git a/OwnChar.Server/Data/ClientServerDataProvider.cs b/OwnChar.Server/Data/ClientServerDataProvider.cs deleted file mode 100644 index d96c660..0000000 --- a/OwnChar.Server/Data/ClientServerDataProvider.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Pilz.Plugins.Advanced; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OwnChar.Server.Data -{ - public class ClientServerDataProvider() : PluginFunction(IServerDataProvider.FeatureCode, "ownchar.clientserver"), IServerDataProvider - { - protected override object? ExecuteFunction(PluginFunctionParameter? @params) - { - return this; - } - } -} diff --git a/OwnChar.Server/Data/IServerDataProvider.cs b/OwnChar.Server/Data/IServerDataProvider.cs deleted file mode 100644 index abedd03..0000000 --- a/OwnChar.Server/Data/IServerDataProvider.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Pilz.Plugins.Advanced; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OwnChar.Server.Data -{ - public interface IServerDataProvider - { - // Shared - public const string FeatureCode = "ownchar.server.dataprovider"; - public IEnumerable DataProviders => PluginFeatureController.Instance.Features.Get(FeatureCode).Cast().Select(f => (IServerDataProvider)f.Execute()!); - - // Interface - // ... - } -} diff --git a/OwnChar.Server/Network/NetworkHandler.cs b/OwnChar.Server/Network/NetworkHandler.cs deleted file mode 100644 index 62746c6..0000000 --- a/OwnChar.Server/Network/NetworkHandler.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OwnChar.Server.Network -{ - public class NetworkHandler - { - } -} diff --git a/OwnChar.Server/Network/NetworkManager.cs b/OwnChar.Server/Network/NetworkManager.cs deleted file mode 100644 index 83860d6..0000000 --- a/OwnChar.Server/Network/NetworkManager.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OwnChar.Server.Network -{ - public class NetworkManager - { - } -} diff --git a/OwnChar.Server/OwnChar.Server.csproj b/OwnChar.Server/OwnChar.Server.csproj index 2b56a5b..1259423 100644 --- a/OwnChar.Server/OwnChar.Server.csproj +++ b/OwnChar.Server/OwnChar.Server.csproj @@ -1,15 +1,23 @@ - + - Exe net8.0 - enable enable + enable - - + + + + + + + + + + + diff --git a/OwnChar.Server/OwnChar.Server.http b/OwnChar.Server/OwnChar.Server.http new file mode 100644 index 0000000..0ab4612 --- /dev/null +++ b/OwnChar.Server/OwnChar.Server.http @@ -0,0 +1,6 @@ +@OwnChar.ServerNew_HostAddress = http://localhost:5208 + +GET {{OwnChar.ServerNew_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/OwnChar.Server/Program.cs b/OwnChar.Server/Program.cs index 6447507..6bd16e8 100644 --- a/OwnChar.Server/Program.cs +++ b/OwnChar.Server/Program.cs @@ -1,10 +1,69 @@ -namespace OwnChar.Server +using OwnChar.Data; +using OwnChar.Plugins; +using OwnChar.ServerNew.Api.Endpoint; +using OwnChar.ServerNew.Api.Endpoint.Implementations; +using OwnChar.ServerNew.Api.Plugins; +using Pilz.Configuration; +using Pilz.Plugins; +using Pilz.Plugins.Advanced; + +namespace OwnChar.ServerNew; + +internal class Program { - internal class Program + public static string? AppTempFolder { get; private set; } + public static ISettingsManager? SettingsManager { get; private set; } + + public static void Main(string[] args) { - static void Main(string[] args) + // Load settings + AppTempFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "OwnChar", "Server"); + Directory.CreateDirectory(AppTempFolder); + SettingsManager = new SettingsManager(Path.Combine(AppTempFolder, "Settings.json"), true); + + // Create server context + var server = new ServerContext(SettingsManager.Instance); + + // Load plugins + var pluginPath = Path.Combine(AppTempFolder, "Plugins"); + Directory.CreateDirectory(pluginPath); + var pluginPaths = Directory.GetDirectories(pluginPath, "*", SearchOption.TopDirectoryOnly).Select(n => Path.Combine(n, n + ".dll")).ToArray(); + OwnCharPlugins.Instance.LoadPlugins(pluginPaths, new OwnCharServerPluginInitParams(server)); + + // Add services to the container. + var builder = WebApplication.CreateBuilder(args); + builder.Services.AddAuthorization(); + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + + // Build app + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) { - Console.WriteLine("Hello, World!"); + app.UseSwagger(); + app.UseSwaggerUI(); } + app.UseHttpsRedirection(); + app.UseAuthorization(); + + // Built-in endpoints + var apibuilder = new ApiBuilder(app); + new LoginApi(server).Initialize(apibuilder); + new UsersApi(server).Initialize(apibuilder); + new GroupsApi(server).Initialize(apibuilder); + new CharactersApi(server).Initialize(apibuilder); + + // Plugin endpoints + var endpoints = PluginFeatureController.Instance.Features.Get(ApiEndpointFeature.FeatureType).OfType(); + if (endpoints.Any()) + { + foreach (var endpoint in endpoints) + endpoint.Initialize(apibuilder); + } + + // Run server + app.Run(); } } diff --git a/OwnChar.Server/Properties/launchSettings.json b/OwnChar.Server/Properties/launchSettings.json new file mode 100644 index 0000000..c17c527 --- /dev/null +++ b/OwnChar.Server/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:48428", + "sslPort": 44302 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5208", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7174;http://localhost:5208", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/OwnChar.Server/ServerContext.cs b/OwnChar.Server/ServerContext.cs new file mode 100644 index 0000000..d61a6a3 --- /dev/null +++ b/OwnChar.Server/ServerContext.cs @@ -0,0 +1,47 @@ +using OwnChar.Data; +using OwnChar.Model; +using OwnChar.ServerNew.Api; +using OwnChar.ServerNew.Api.Plugins; +using Pilz.Configuration; +using Pilz.Cryptography; + +namespace OwnChar.ServerNew; + +internal class ServerContext(ISettings settings) : IServer, IPluginLoadContextServer +{ + private readonly Dictionary users = []; + + public IDataProvider? Data { get; private set; } + + public ISettings Settings { get; } = settings; + + public string Login(UserAccount account) + { + var secret = new UniquieID(UniquieIDGenerationMode.GenerateOnInit).ID; + users.Add(secret, account); + return secret; + } + + public void Logout(string secret) + { + users.Remove(secret); + } + + public bool IsLoggedIn(string secret) + { + return users.ContainsKey(secret); + } + + public void CheckLogin(string secret) + { + if (!IsLoggedIn(secret)) + throw new UnauthorizedAccessException(); + } + + public UserAccount? GetUser(string secret) + { + if (users.TryGetValue(secret, out UserAccount? value)) + return value; + return null; + } +} diff --git a/OwnChar.Server/appsettings.Development.json b/OwnChar.Server/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/OwnChar.Server/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/OwnChar.Server/appsettings.json b/OwnChar.Server/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/OwnChar.Server/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}