using Inversion; namespace Astralis { public class WebApplication { public Container container { get; private set; } public int port { get; private set; } private Server server; private Pipeline pipeline; public WebApplication(int? port = null) { this.port = port ?? int.parse(Environment.get_variable ("ASTRALIS_PORT") ?? "8080"); printerr(@"[Astralis] Web application using port $(port)\n"); container = new Container(); pipeline = new Pipeline(container); server = new Server(this.port, pipeline); } public void run() throws Error { // Ensure router is registered last so that it is the last component in the pipeline. add_component(); var timer = new Timer(); timer.start(); container.initialise(); timer.stop(); printerr(@"[Astralis] Intialised application in $((int)(timer.elapsed() * 1000))ms\n"); server.run(); } public Registration add_endpoint(EndpointRoute route, owned TypedFactoryDelegate? endpoint_func = null) { return add_scoped((owned) endpoint_func) .with_metadata(route) .as(); } public Registration add_singleton_endpoint(EndpointRoute route, owned TypedFactoryDelegate? endpoint_func = null) { return add_singleton((owned) endpoint_func) .with_metadata(route) .as(); } public Registration add_startup_endpoint(EndpointRoute route, owned TypedFactoryDelegate? endpoint_func = null) { return add_startup((owned) endpoint_func) .with_metadata(route) .as(); } public Registration add_component(owned TypedFactoryDelegate? construct_func = null) { return add_scoped(construct_func).as(); } public Registration add_singleton_component(owned TypedFactoryDelegate? construct_func = null) { return add_singleton(construct_func).as(); } public Registration add_startup_component(owned TypedFactoryDelegate? construct_func = null) { return add_startup(construct_func).as(); } public Registration add_transient(owned TypedFactoryDelegate? construct_func = null) { return container.register_transient(construct_func); } public Registration add_scoped(owned TypedFactoryDelegate? construct_func = null) { return container.register_scoped(construct_func); } public Registration add_singleton(owned TypedFactoryDelegate? construct_func = null) { return container.register_singleton(construct_func); } public Registration add_startup(owned TypedFactoryDelegate? construct_func = null) { return container.register_startup(construct_func); } public Registration add_module(owned TypedFactoryDelegate? construct_func = null) throws Error { return container.register_module(construct_func); } public void use_compression() { add_component(); add_component(); add_component(); } } }