Add Area Support

Needs testing
This commit is contained in:
Kaveman
2024-08-15 01:12:39 -07:00
parent b21a1caabc
commit ff1b4bfd2c
2 changed files with 103 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ namespace WebServer {
public delegate Task<HttpResponse?> Callback(HttpListenerContext context, CachedResponse? cache);
Dictionary<string, Dictionary<Regex, Callback>> HttpCallbacks = new Dictionary<string, Dictionary<Regex, Callback>>();
public List<AreaBase> RegisteredAreas = new List<AreaBase>();
Dictionary<string, RazorLightEngine> RazorEngines = new Dictionary<string, RazorLightEngine>();
public readonly RazorLightEngine DefaultRazorEngine;
/* Instead of three directories (from previous render engine)
@@ -357,5 +358,17 @@ namespace WebServer {
}
return removeCount > 0;
}
public bool TryRegisterArea<T>(Func<T>? areaInitilizer, out T area) where T : AreaBase {
area = areaInitilizer?.Invoke() ?? default;
if (area == null) return false;
RegisteredAreas.Add(area);
return true;
}
public void RegisterArea<T>(Func<T>? areaInitializer, out T area) where T : AreaBase {
if (!TryRegisterArea(areaInitializer, out area))
throw new Exception($"Failed to bind {typeof(T).FullName}! Is your initializer returning a valid area?");
}
}
}