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