using Invercargill.DataStructures; namespace Astralis { public delegate HttpResult RouteHandler(HttpContext context) throws Error; public class RouteEntry { public RouteHandler handler; public RouteEntry(owned RouteHandler handler) { this.handler = (owned) handler; } } public class Router : HttpHandler, Object { private Dictionary routes = new Dictionary(); public void map_get(string path, owned RouteHandler handler) { // In a real router, we'd handle methods separately. // For now, simple path mapping. routes.set(path, new RouteEntry((owned) handler)); } public async HttpResult handle(HttpContext context) { try { return routes.get(context.request.raw_path).handler(context); } catch (Invercargill.IndexError e) { return new BufferedHttpResult.from_string("Not Found", StatusCode.NOT_FOUND); } catch { return new BufferedHttpResult.from_string("Internal Server Error", StatusCode.INTERNAL_SERVER_ERROR); } } } }