push my ork
This commit is contained in:
@@ -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<string, UserAccountBase> 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<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)
|
||||
|
||||
Reference in New Issue
Block a user