From 3522aff72cb95f189db193c58d4af438ea338d15 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Thu, 15 Aug 2024 09:20:20 +0200 Subject: [PATCH] push my ork --- OwnChar.Server/Api/Endpoint/ApiBuilder.cs | 16 ++- OwnChar.Server/Api/Endpoint/ApiMapInfo.cs | 3 + OwnChar.Server/Api/Endpoint/IApiBuilder.cs | 7 +- .../Api/Endpoint/Implementations/GroupsApi.cs | 18 ++-- .../Api/Endpoint/Implementations/LoginApi.cs | 4 +- OwnChar.Server/OwnChar.Server.csproj | 4 +- OwnChar.Server/OwnChar.Server.http | 6 -- OwnChar.Server/Program.cs | 2 +- OwnChar.Server/Properties/launchSettings.json | 41 -------- OwnChar.Server/ServerContext.cs | 98 ++++++++++++++----- OwnChar.Server/ServerSettings.cs | 2 + OwnChar.Server/appsettings.Development.json | 8 -- OwnChar.Server/appsettings.json | 9 -- 13 files changed, 100 insertions(+), 118 deletions(-) create mode 100644 OwnChar.Server/Api/Endpoint/ApiMapInfo.cs delete mode 100644 OwnChar.Server/OwnChar.Server.http delete mode 100644 OwnChar.Server/Properties/launchSettings.json delete mode 100644 OwnChar.Server/appsettings.Development.json delete mode 100644 OwnChar.Server/appsettings.json diff --git a/OwnChar.Server/Api/Endpoint/ApiBuilder.cs b/OwnChar.Server/Api/Endpoint/ApiBuilder.cs index 45abb20..508c4a0 100644 --- a/OwnChar.Server/Api/Endpoint/ApiBuilder.cs +++ b/OwnChar.Server/Api/Endpoint/ApiBuilder.cs @@ -1,17 +1,13 @@ -using OwnChar.Api.Packets; +namespace OwnChar.Server.Api.Endpoint; -namespace OwnChar.Server.Api.Endpoint; - -internal class ApiBuilder(WebApplication app) : IApiBuilder +internal class ApiBuilder : IApiBuilder { - public void MapRequest(string pattern, Delegate action) - { - Map(pattern + "/{request}", action); - } + private readonly Dictionary handlers = []; + + public IReadOnlyDictionary Handlers => handlers; public void Map(string pattern, Delegate action) { - //app.Map(pattern, action); - app.MapPost(pattern, action); + handlers.Add(new(pattern, "POST"), action); } } diff --git a/OwnChar.Server/Api/Endpoint/ApiMapInfo.cs b/OwnChar.Server/Api/Endpoint/ApiMapInfo.cs new file mode 100644 index 0000000..98f25ba --- /dev/null +++ b/OwnChar.Server/Api/Endpoint/ApiMapInfo.cs @@ -0,0 +1,3 @@ +namespace OwnChar.Server.Api.Endpoint; + +public record struct ApiMapInfo(string Pattern, string Method); \ No newline at end of file diff --git a/OwnChar.Server/Api/Endpoint/IApiBuilder.cs b/OwnChar.Server/Api/Endpoint/IApiBuilder.cs index ec6c063..8662ef2 100644 --- a/OwnChar.Server/Api/Endpoint/IApiBuilder.cs +++ b/OwnChar.Server/Api/Endpoint/IApiBuilder.cs @@ -1,9 +1,8 @@ -using OwnChar.Api.Packets; - -namespace OwnChar.Server.Api.Endpoint; +namespace OwnChar.Server.Api.Endpoint; public interface IApiBuilder { - void MapRequest(string pattern, Delegate action); + public IReadOnlyDictionary Handlers { get; } + void Map(string path, Delegate action); } diff --git a/OwnChar.Server/Api/Endpoint/Implementations/GroupsApi.cs b/OwnChar.Server/Api/Endpoint/Implementations/GroupsApi.cs index b68266a..9bc9f35 100644 --- a/OwnChar.Server/Api/Endpoint/Implementations/GroupsApi.cs +++ b/OwnChar.Server/Api/Endpoint/Implementations/GroupsApi.cs @@ -13,15 +13,15 @@ internal class GroupsApi(IServer server) : IApiEndpoint { public void Initialize(IApiBuilder builder) { - builder.MapRequest("/groups/get/byid", GetById); - builder.MapRequest("/groups/get", Get); - builder.MapRequest("/groups/create", Create); - builder.MapRequest("/groups/update", Update); - builder.MapRequest("/groups/delete", Delete); - builder.MapRequest("/groups/members/get", GetMembers); - builder.MapRequest("/groups/members/add", AddMembers); - builder.MapRequest("/groups/members/update", UpdateMember); - builder.MapRequest("/groups/members/remove", RemoveMembers); + builder.Map("/groups/get/byid", GetById); + builder.Map("/groups/get", Get); + builder.Map("/groups/create", Create); + builder.Map("/groups/update", Update); + builder.Map("/groups/delete", Delete); + builder.Map("/groups/members/get", GetMembers); + builder.Map("/groups/members/add", AddMembers); + builder.Map("/groups/members/update", UpdateMember); + builder.Map("/groups/members/remove", RemoveMembers); } private IResult GetById(GetSinlgeObjectRequest request) diff --git a/OwnChar.Server/Api/Endpoint/Implementations/LoginApi.cs b/OwnChar.Server/Api/Endpoint/Implementations/LoginApi.cs index 5f628df..5038938 100644 --- a/OwnChar.Server/Api/Endpoint/Implementations/LoginApi.cs +++ b/OwnChar.Server/Api/Endpoint/Implementations/LoginApi.cs @@ -9,8 +9,8 @@ internal class LoginApi(ServerContext server) : IApiEndpoint { public void Initialize(IApiBuilder builder) { - builder.Map("/auth/login/{request}", Login); - builder.Map("/auth/logout/{request}", Logout); + builder.Map("/auth/login", Login); + builder.Map("/auth/logout", Logout); } private IResult Login(LoginRequest request) diff --git a/OwnChar.Server/OwnChar.Server.csproj b/OwnChar.Server/OwnChar.Server.csproj index 4d6a891..86ec53f 100644 --- a/OwnChar.Server/OwnChar.Server.csproj +++ b/OwnChar.Server/OwnChar.Server.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -8,12 +8,10 @@ - - diff --git a/OwnChar.Server/OwnChar.Server.http b/OwnChar.Server/OwnChar.Server.http deleted file mode 100644 index 0ab4612..0000000 --- a/OwnChar.Server/OwnChar.Server.http +++ /dev/null @@ -1,6 +0,0 @@ -@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 6bbad8d..d699625 100644 --- a/OwnChar.Server/Program.cs +++ b/OwnChar.Server/Program.cs @@ -1,5 +1,5 @@ using OwnChar.Plugins; -using OwnChar.ServerNew.Api.Plugins; +using OwnChar.Server.Api.Plugins; using Pilz.Configuration; namespace OwnChar.Server; diff --git a/OwnChar.Server/Properties/launchSettings.json b/OwnChar.Server/Properties/launchSettings.json deleted file mode 100644 index c17c527..0000000 --- a/OwnChar.Server/Properties/launchSettings.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "$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 index 2c0bca9..2cf8fbf 100644 --- a/OwnChar.Server/ServerContext.cs +++ b/OwnChar.Server/ServerContext.cs @@ -1,22 +1,23 @@ using Castle.Core.Logging; using Microsoft.EntityFrameworkCore; -using OwnChar.Data; using OwnChar.Data.Model.Base; -using OwnChar.Model; using OwnChar.Server.Api; using OwnChar.Server.Api.Endpoint; +using OwnChar.Server.Api.Endpoint.Implementations; using OwnChar.Server.Api.Plugins; -using OwnChar.ServerNew.Api.Endpoint.Implementations; +using OwnChar.Server.Data; using Pilz.Configuration; using Pilz.Cryptography; using Pilz.Plugins.Advanced; -using ILogger = Castle.Core.Logging.ILogger; +using System.Net; namespace OwnChar.Server; internal class ServerContext(ISettings settings) : IServer { private readonly Dictionary users = []; + private readonly HttpListener httpListener = new(); + private readonly ApiBuilder apiBuilder = new(); public DbContext? Data { get; private set; } @@ -26,25 +27,7 @@ internal class ServerContext(ISettings settings) : IServer public void Start(string[] args) { - Log.Info("Prepairing server context"); - - // 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()) - { - app.UseSwagger(); - app.UseSwaggerUI(); - } - app.UseHttpsRedirection(); - app.UseAuthorization(); + Log.Info("Prepairing server"); // Load database Log.Debug("Loading database"); @@ -53,7 +36,7 @@ internal class ServerContext(ISettings settings) : IServer // Built-in endpoints Log.Debug("Loading internal api endpoints"); - var apibuilder = new ApiBuilder(app); + var apibuilder = new ApiBuilder(); new LoginApi(this).Initialize(apibuilder); new UsersApi(this).Initialize(apibuilder); new GroupsApi(this).Initialize(apibuilder); @@ -70,7 +53,72 @@ internal class ServerContext(ISettings settings) : IServer // Run server Log.Info("Starting webserver"); - app.Run(); + httpListener.Start(); + Listen(); + } + + private void Listen() + { + var apiUrl = Settings.Get().ApiUrl; + if (string.IsNullOrWhiteSpace(apiUrl)) + throw new NullReferenceException("ApiUrl is empty!"); + + 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(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(); + } } public string Login(UserAccountBase account) diff --git a/OwnChar.Server/ServerSettings.cs b/OwnChar.Server/ServerSettings.cs index cf6e87f..2a5e4d2 100644 --- a/OwnChar.Server/ServerSettings.cs +++ b/OwnChar.Server/ServerSettings.cs @@ -9,11 +9,13 @@ public class ServerSettings : IChildSettings, ISettingsIdentifier public string? DbServer { get; set; } public string? DbUser { get; set; } public string? DbPassword { get; set; } + public string? ApiUrl { get; set; } public void Reset() { DbServer = null; DbUser = null; DbPassword = null; + ApiUrl = null; } } diff --git a/OwnChar.Server/appsettings.Development.json b/OwnChar.Server/appsettings.Development.json deleted file mode 100644 index 0c208ae..0000000 --- a/OwnChar.Server/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} diff --git a/OwnChar.Server/appsettings.json b/OwnChar.Server/appsettings.json deleted file mode 100644 index 10f68b8..0000000 --- a/OwnChar.Server/appsettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" -}