push my ork
This commit is contained in:
@@ -1,17 +1,13 @@
|
|||||||
using OwnChar.Api.Packets;
|
namespace OwnChar.Server.Api.Endpoint;
|
||||||
|
|
||||||
namespace OwnChar.Server.Api.Endpoint;
|
internal class ApiBuilder : IApiBuilder
|
||||||
|
|
||||||
internal class ApiBuilder(WebApplication app) : IApiBuilder
|
|
||||||
{
|
{
|
||||||
public void MapRequest(string pattern, Delegate action)
|
private readonly Dictionary<ApiMapInfo, Delegate> handlers = [];
|
||||||
{
|
|
||||||
Map(pattern + "/{request}", action);
|
public IReadOnlyDictionary<ApiMapInfo, Delegate> Handlers => handlers;
|
||||||
}
|
|
||||||
|
|
||||||
public void Map(string pattern, Delegate action)
|
public void Map(string pattern, Delegate action)
|
||||||
{
|
{
|
||||||
//app.Map(pattern, action);
|
handlers.Add(new(pattern, "POST"), action);
|
||||||
app.MapPost(pattern, action);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3
OwnChar.Server/Api/Endpoint/ApiMapInfo.cs
Normal file
3
OwnChar.Server/Api/Endpoint/ApiMapInfo.cs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
namespace OwnChar.Server.Api.Endpoint;
|
||||||
|
|
||||||
|
public record struct ApiMapInfo(string Pattern, string Method);
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
using OwnChar.Api.Packets;
|
namespace OwnChar.Server.Api.Endpoint;
|
||||||
|
|
||||||
namespace OwnChar.Server.Api.Endpoint;
|
|
||||||
|
|
||||||
public interface IApiBuilder
|
public interface IApiBuilder
|
||||||
{
|
{
|
||||||
void MapRequest(string pattern, Delegate action);
|
public IReadOnlyDictionary<ApiMapInfo, Delegate> Handlers { get; }
|
||||||
|
|
||||||
void Map(string path, Delegate action);
|
void Map(string path, Delegate action);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,15 +13,15 @@ internal class GroupsApi(IServer server) : IApiEndpoint
|
|||||||
{
|
{
|
||||||
public void Initialize(IApiBuilder builder)
|
public void Initialize(IApiBuilder builder)
|
||||||
{
|
{
|
||||||
builder.MapRequest("/groups/get/byid", GetById);
|
builder.Map("/groups/get/byid", GetById);
|
||||||
builder.MapRequest("/groups/get", Get);
|
builder.Map("/groups/get", Get);
|
||||||
builder.MapRequest("/groups/create", Create);
|
builder.Map("/groups/create", Create);
|
||||||
builder.MapRequest("/groups/update", Update);
|
builder.Map("/groups/update", Update);
|
||||||
builder.MapRequest("/groups/delete", Delete);
|
builder.Map("/groups/delete", Delete);
|
||||||
builder.MapRequest("/groups/members/get", GetMembers);
|
builder.Map("/groups/members/get", GetMembers);
|
||||||
builder.MapRequest("/groups/members/add", AddMembers);
|
builder.Map("/groups/members/add", AddMembers);
|
||||||
builder.MapRequest("/groups/members/update", UpdateMember);
|
builder.Map("/groups/members/update", UpdateMember);
|
||||||
builder.MapRequest("/groups/members/remove", RemoveMembers);
|
builder.Map("/groups/members/remove", RemoveMembers);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IResult GetById(GetSinlgeObjectRequest request)
|
private IResult GetById(GetSinlgeObjectRequest request)
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ internal class LoginApi(ServerContext server) : IApiEndpoint
|
|||||||
{
|
{
|
||||||
public void Initialize(IApiBuilder builder)
|
public void Initialize(IApiBuilder builder)
|
||||||
{
|
{
|
||||||
builder.Map("/auth/login/{request}", Login);
|
builder.Map("/auth/login", Login);
|
||||||
builder.Map("/auth/logout/{request}", Logout);
|
builder.Map("/auth/logout", Logout);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IResult Login(LoginRequest request)
|
private IResult Login(LoginRequest request)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
@@ -8,12 +8,10 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Castle.Core" Version="5.1.1" />
|
<PackageReference Include="Castle.Core" Version="5.1.1" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
|
|
||||||
<PackageReference Include="Pilz.Configuration" Version="3.1.2" />
|
<PackageReference Include="Pilz.Configuration" Version="3.1.2" />
|
||||||
<PackageReference Include="Pilz.Cryptography" Version="2.0.1" />
|
<PackageReference Include="Pilz.Cryptography" Version="2.0.1" />
|
||||||
<PackageReference Include="Pilz.Plugins" Version="2.1.9" />
|
<PackageReference Include="Pilz.Plugins" Version="2.1.9" />
|
||||||
<PackageReference Include="Pilz.Plugins.Advanced" Version="2.10.1" />
|
<PackageReference Include="Pilz.Plugins.Advanced" Version="2.10.1" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
@OwnChar.ServerNew_HostAddress = http://localhost:5208
|
|
||||||
|
|
||||||
GET {{OwnChar.ServerNew_HostAddress}}/weatherforecast/
|
|
||||||
Accept: application/json
|
|
||||||
|
|
||||||
###
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
using OwnChar.Plugins;
|
using OwnChar.Plugins;
|
||||||
using OwnChar.ServerNew.Api.Plugins;
|
using OwnChar.Server.Api.Plugins;
|
||||||
using Pilz.Configuration;
|
using Pilz.Configuration;
|
||||||
|
|
||||||
namespace OwnChar.Server;
|
namespace OwnChar.Server;
|
||||||
|
|||||||
@@ -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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +1,23 @@
|
|||||||
using Castle.Core.Logging;
|
using Castle.Core.Logging;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using OwnChar.Data;
|
|
||||||
using OwnChar.Data.Model.Base;
|
using OwnChar.Data.Model.Base;
|
||||||
using OwnChar.Model;
|
|
||||||
using OwnChar.Server.Api;
|
using OwnChar.Server.Api;
|
||||||
using OwnChar.Server.Api.Endpoint;
|
using OwnChar.Server.Api.Endpoint;
|
||||||
|
using OwnChar.Server.Api.Endpoint.Implementations;
|
||||||
using OwnChar.Server.Api.Plugins;
|
using OwnChar.Server.Api.Plugins;
|
||||||
using OwnChar.ServerNew.Api.Endpoint.Implementations;
|
using OwnChar.Server.Data;
|
||||||
using Pilz.Configuration;
|
using Pilz.Configuration;
|
||||||
using Pilz.Cryptography;
|
using Pilz.Cryptography;
|
||||||
using Pilz.Plugins.Advanced;
|
using Pilz.Plugins.Advanced;
|
||||||
using ILogger = Castle.Core.Logging.ILogger;
|
using System.Net;
|
||||||
|
|
||||||
namespace OwnChar.Server;
|
namespace OwnChar.Server;
|
||||||
|
|
||||||
internal class ServerContext(ISettings settings) : IServer
|
internal class ServerContext(ISettings settings) : IServer
|
||||||
{
|
{
|
||||||
private readonly Dictionary<string, UserAccountBase> users = [];
|
private readonly Dictionary<string, UserAccountBase> users = [];
|
||||||
|
private readonly HttpListener httpListener = new();
|
||||||
|
private readonly ApiBuilder apiBuilder = new();
|
||||||
|
|
||||||
public DbContext? Data { get; private set; }
|
public DbContext? Data { get; private set; }
|
||||||
|
|
||||||
@@ -26,25 +27,7 @@ internal class ServerContext(ISettings settings) : IServer
|
|||||||
|
|
||||||
public void Start(string[] args)
|
public void Start(string[] args)
|
||||||
{
|
{
|
||||||
Log.Info("Prepairing server context");
|
Log.Info("Prepairing 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())
|
|
||||||
{
|
|
||||||
app.UseSwagger();
|
|
||||||
app.UseSwaggerUI();
|
|
||||||
}
|
|
||||||
app.UseHttpsRedirection();
|
|
||||||
app.UseAuthorization();
|
|
||||||
|
|
||||||
// Load database
|
// Load database
|
||||||
Log.Debug("Loading database");
|
Log.Debug("Loading database");
|
||||||
@@ -53,7 +36,7 @@ internal class ServerContext(ISettings settings) : IServer
|
|||||||
|
|
||||||
// Built-in endpoints
|
// Built-in endpoints
|
||||||
Log.Debug("Loading internal api endpoints");
|
Log.Debug("Loading internal api endpoints");
|
||||||
var apibuilder = new ApiBuilder(app);
|
var apibuilder = new ApiBuilder();
|
||||||
new LoginApi(this).Initialize(apibuilder);
|
new LoginApi(this).Initialize(apibuilder);
|
||||||
new UsersApi(this).Initialize(apibuilder);
|
new UsersApi(this).Initialize(apibuilder);
|
||||||
new GroupsApi(this).Initialize(apibuilder);
|
new GroupsApi(this).Initialize(apibuilder);
|
||||||
@@ -70,7 +53,72 @@ internal class ServerContext(ISettings settings) : IServer
|
|||||||
|
|
||||||
// Run server
|
// Run server
|
||||||
Log.Info("Starting webserver");
|
Log.Info("Starting webserver");
|
||||||
app.Run();
|
httpListener.Start();
|
||||||
|
Listen();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Listen()
|
||||||
|
{
|
||||||
|
var apiUrl = Settings.Get<ServerSettings>().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<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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Login(UserAccountBase account)
|
public string Login(UserAccountBase account)
|
||||||
|
|||||||
@@ -9,11 +9,13 @@ public class ServerSettings : IChildSettings, ISettingsIdentifier
|
|||||||
public string? DbServer { get; set; }
|
public string? DbServer { get; set; }
|
||||||
public string? DbUser { get; set; }
|
public string? DbUser { get; set; }
|
||||||
public string? DbPassword { get; set; }
|
public string? DbPassword { get; set; }
|
||||||
|
public string? ApiUrl { get; set; }
|
||||||
|
|
||||||
public void Reset()
|
public void Reset()
|
||||||
{
|
{
|
||||||
DbServer = null;
|
DbServer = null;
|
||||||
DbUser = null;
|
DbUser = null;
|
||||||
DbPassword = null;
|
DbPassword = null;
|
||||||
|
ApiUrl = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
{
|
|
||||||
"Logging": {
|
|
||||||
"LogLevel": {
|
|
||||||
"Default": "Information",
|
|
||||||
"Microsoft.AspNetCore": "Warning"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
{
|
|
||||||
"Logging": {
|
|
||||||
"LogLevel": {
|
|
||||||
"Default": "Information",
|
|
||||||
"Microsoft.AspNetCore": "Warning"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"AllowedHosts": "*"
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user