some work (need to thing about client and server data model as next step)
This commit is contained in:
@@ -1,45 +1,107 @@
|
||||
using OwnChar.Data;
|
||||
using Castle.Core.Logging;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OwnChar.Data;
|
||||
using OwnChar.Model;
|
||||
using OwnChar.ServerNew.Api;
|
||||
using OwnChar.Server;
|
||||
using OwnChar.ServerNew.Api.Endpoint;
|
||||
using OwnChar.ServerNew.Api.Endpoint.Implementations;
|
||||
using OwnChar.ServerNew.Api.Plugins;
|
||||
using Pilz.Configuration;
|
||||
using Pilz.Cryptography;
|
||||
using Pilz.Plugins.Advanced;
|
||||
using ILogger = Castle.Core.Logging.ILogger;
|
||||
|
||||
namespace OwnChar.ServerNew;
|
||||
|
||||
internal class ServerContext(ISettings settings) : IServer, IPluginLoadContextServer
|
||||
internal class ServerContext(ISettings settings) : Api.IServer
|
||||
{
|
||||
private readonly Dictionary<string, UserAccount> users = [];
|
||||
|
||||
public IDataProvider? Data { get; private set; }
|
||||
public DbContext? Data { get; private set; }
|
||||
|
||||
public ISettings Settings { get; } = settings;
|
||||
|
||||
public ILogger Log { get; set; } = NullLogger.Instance;
|
||||
|
||||
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();
|
||||
|
||||
// Load database
|
||||
Log.Debug("Loading database");
|
||||
var settings = Settings.Get<ServerSettings>();
|
||||
Data = new DatabaseContext(settings.DbServer, settings.DbUser, settings.DbPassword);
|
||||
|
||||
// Built-in endpoints
|
||||
Log.Debug("Loading internal api endpoints");
|
||||
var apibuilder = new ApiBuilder(app);
|
||||
new LoginApi(this).Initialize(apibuilder);
|
||||
new UsersApi(this).Initialize(apibuilder);
|
||||
new GroupsApi(this).Initialize(apibuilder);
|
||||
new CharactersApi(this).Initialize(apibuilder);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// Run server
|
||||
Log.Info("Starting webserver");
|
||||
app.Run();
|
||||
}
|
||||
|
||||
public string Login(UserAccount account)
|
||||
{
|
||||
var secret = new UniquieID(UniquieIDGenerationMode.GenerateOnInit).ID;
|
||||
users.Add(secret, account);
|
||||
Log.DebugFormat("Logged-in out user with secret {0}", secret);
|
||||
return secret;
|
||||
}
|
||||
|
||||
public void Logout(string secret)
|
||||
{
|
||||
users.Remove(secret);
|
||||
Log.DebugFormat("Logged-out user with secret {0}", secret);
|
||||
}
|
||||
|
||||
public bool IsLoggedIn(string secret)
|
||||
{
|
||||
Log.DebugFormat("Deleting user with secret {0}", secret);
|
||||
return users.ContainsKey(secret);
|
||||
}
|
||||
|
||||
public void CheckLogin(string secret)
|
||||
{
|
||||
Log.DebugFormat("Checking login for user with secret {0}", secret);
|
||||
if (!IsLoggedIn(secret))
|
||||
throw new UnauthorizedAccessException();
|
||||
}
|
||||
|
||||
public UserAccount? GetUser(string secret)
|
||||
{
|
||||
Log.DebugFormat("Getting user with secret {0}", secret);
|
||||
if (users.TryGetValue(secret, out UserAccount? value))
|
||||
return value;
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user