111 lines
3.3 KiB
C#
111 lines
3.3 KiB
C#
using Castle.Core.Logging;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using OwnChar.Base.Data.Model;
|
|
using OwnChar.Data;
|
|
using OwnChar.Model;
|
|
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) : Api.IServer
|
|
{
|
|
private readonly Dictionary<string, UserAccountBase> users = [];
|
|
|
|
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(UserAccountBase 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 UserAccountBase? GetUser(string secret)
|
|
{
|
|
Log.DebugFormat("Getting user with secret {0}", secret);
|
|
if (users.TryGetValue(secret, out UserAccountBase? value))
|
|
return value;
|
|
return null;
|
|
}
|
|
}
|