| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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<EndpointRouter>();
- 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<T>(EndpointRoute route, owned TypedFactoryDelegate<T>? endpoint_func = null) {
- return add_scoped<T>((owned) endpoint_func)
- .with_metadata<EndpointRoute>(route)
- .as<Endpoint>();
- }
- public Registration add_singleton_endpoint<T>(EndpointRoute route, owned TypedFactoryDelegate<T>? endpoint_func = null) {
- return add_singleton<T>((owned) endpoint_func)
- .with_metadata<EndpointRoute>(route)
- .as<Endpoint>();
- }
- public Registration add_startup_endpoint<T>(EndpointRoute route, owned TypedFactoryDelegate<T>? endpoint_func = null) {
- return add_startup<T>((owned) endpoint_func)
- .with_metadata<EndpointRoute>(route)
- .as<Endpoint>();
- }
- public Registration add_component<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
- return add_scoped<T>(construct_func).as<PipelineComponent>();
- }
- public Registration add_singleton_component<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
- return add_singleton<T>(construct_func).as<PipelineComponent>();
- }
- public Registration add_startup_component<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
- return add_startup<T>(construct_func).as<PipelineComponent>();
- }
- public Registration add_transient<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
- return container.register_transient<T>(construct_func);
- }
- public Registration add_scoped<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
- return container.register_scoped<T>(construct_func);
- }
- public Registration add_singleton<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
- return container.register_singleton<T>(construct_func);
- }
- public Registration add_startup<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
- return container.register_startup<T>(construct_func);
- }
- public Registration add_module<T>(owned TypedFactoryDelegate<T>? construct_func = null) throws Error {
- return container.register_module<T>(construct_func);
- }
- public void use_compression() {
- add_component<GzipCompressor>();
- add_component<ZstdCompressor>();
- add_component<BrotliCompressor>();
- }
- }
- }
|