add better initialization
This commit is contained in:
@@ -17,6 +17,7 @@ public class ApiServer : IApiServer
|
||||
public class MissingDataManagerException : Exception { }
|
||||
|
||||
protected readonly List<PrivateMessageHandler> handlers = [];
|
||||
protected readonly List<object> handlerObjects = [];
|
||||
protected readonly Dictionary<Type, IApiMessageSerializer> serializers = [];
|
||||
protected readonly Dictionary<ThreadHolder, IDataManager> managers = [];
|
||||
protected HttpListener httpListener;
|
||||
@@ -25,6 +26,7 @@ public class ApiServer : IApiServer
|
||||
protected SemaphoreSlim? semaphore;
|
||||
protected bool doListen;
|
||||
protected bool isAutoRestarting;
|
||||
protected bool initializedHandlers;
|
||||
|
||||
public event OnCheckAuthenticationEventHandler? OnCheckAuthentication;
|
||||
public event OnCheckContextEventHandler? OnCheckContext;
|
||||
@@ -109,6 +111,7 @@ public class ApiServer : IApiServer
|
||||
public virtual void Start()
|
||||
{
|
||||
Log.Info("Starting listener");
|
||||
InitializeHandlers();
|
||||
httpListener.Prefixes.Add(ApiUrl + "/");
|
||||
doListen = true;
|
||||
httpListener.Start();
|
||||
@@ -183,6 +186,20 @@ public class ApiServer : IApiServer
|
||||
semaphore.Release();
|
||||
}
|
||||
|
||||
protected virtual void InitializeHandlers()
|
||||
{
|
||||
foreach (var instance in handlerObjects)
|
||||
{
|
||||
if (instance is IApiHandlerInitializer initializer)
|
||||
initializer.Initialize();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual T? GetHandler<T>()
|
||||
{
|
||||
return handlerObjects.OfType<T>().FirstOrDefault();
|
||||
}
|
||||
|
||||
public virtual Dictionary<string, string[]> GetEndpoints()
|
||||
{
|
||||
return handlers.OrderBy(n => n.Attribute.Route).GroupBy(n => n.Attribute.Route).ToDictionary(n => n.Key, n => n.SelectMany(n => n.Attribute.Methods.OrderBy(n => n)).ToArray());
|
||||
@@ -190,9 +207,8 @@ public class ApiServer : IApiServer
|
||||
|
||||
public virtual void RegisterHandler<T>(T instance) where T : class
|
||||
{
|
||||
// Initialize
|
||||
if (instance is IApiHandlerInitializer initializer)
|
||||
initializer.Initialize(this);
|
||||
if (!handlerObjects.Contains(instance))
|
||||
handlerObjects.Add(instance);
|
||||
|
||||
// Get all public instance methods
|
||||
var methods = instance.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public);
|
||||
|
||||
@@ -2,5 +2,5 @@
|
||||
|
||||
public interface IApiHandlerInitializer
|
||||
{
|
||||
public void Initialize(IApiServer server);
|
||||
void Initialize();
|
||||
}
|
||||
|
||||
@@ -30,4 +30,5 @@ public interface IApiServer
|
||||
Dictionary<string, string[]> GetEndpoints();
|
||||
void Stop(bool graceful);
|
||||
void Restart(bool graceful);
|
||||
T? GetHandler<T>();
|
||||
}
|
||||
|
||||
@@ -35,9 +35,9 @@ public abstract class BaseChildItemHandler<TEntity, TParent, TUpdateMsg>(IApiSer
|
||||
protected virtual bool RegisterGetAll => true;
|
||||
protected virtual bool RegisterPost => true;
|
||||
|
||||
public override void Initialize(IApiServer server)
|
||||
protected override void OnInitialize()
|
||||
{
|
||||
base.Initialize(server);
|
||||
base.OnInitialize();
|
||||
var t = GetType();
|
||||
if (RegisterGetAll)
|
||||
server.RegisterHandler(t.GetMethod(nameof(GetAll))!.CreateDelegate(this), new(RouteParent + "/{pid}" + RouteChild, "GET"), Debugger.IsAttached);
|
||||
|
||||
@@ -17,6 +17,8 @@ public abstract class BaseHandler<TEntity, TUpdateMsg>(IApiServer server)
|
||||
public ApiRequestInfo Request { get; } = reqest;
|
||||
}
|
||||
|
||||
public event EventHandler<TEntity>? OnDeleteItem;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the base route (endpoint) for the most API calls.
|
||||
/// </summary>
|
||||
@@ -24,8 +26,18 @@ public abstract class BaseHandler<TEntity, TUpdateMsg>(IApiServer server)
|
||||
protected virtual bool RegisterGet => true;
|
||||
protected virtual bool RegisterPut => true;
|
||||
protected virtual bool RegisterDelete => true;
|
||||
public bool HasInitialized { get; protected set; }
|
||||
|
||||
public virtual void Initialize(IApiServer server)
|
||||
public virtual void Initialize()
|
||||
{
|
||||
if (HasInitialized)
|
||||
return;
|
||||
|
||||
HasInitialized = true;
|
||||
OnInitialize();
|
||||
}
|
||||
|
||||
protected virtual void OnInitialize()
|
||||
{
|
||||
var t = GetType();
|
||||
if (RegisterGet)
|
||||
@@ -64,6 +76,7 @@ public abstract class BaseHandler<TEntity, TUpdateMsg>(IApiServer server)
|
||||
|
||||
protected virtual ApiResult? OnDelete(TEntity entity)
|
||||
{
|
||||
OnDeleteItem?.Invoke(this, entity);
|
||||
server.Manager.Delete(entity, true);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@ public abstract class BaseItemHandler<TEntity, TUpdateMsg>(IApiServer server)
|
||||
protected virtual bool RegisterGetAll => true;
|
||||
protected virtual bool RegisterPost => true;
|
||||
|
||||
public override void Initialize(IApiServer server)
|
||||
protected override void OnInitialize()
|
||||
{
|
||||
base.Initialize(server);
|
||||
base.OnInitialize();
|
||||
var t = GetType();
|
||||
if (RegisterGetAll)
|
||||
server.RegisterHandler(t.GetMethod(nameof(GetAll))!.CreateDelegate(this), new(Route, "GET"), Debugger.IsAttached);
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<Version>2.6.11</Version>
|
||||
<Version>2.7.0</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user