WebApplication.vala 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Inversion;
  2. namespace Astralis {
  3. public class WebApplication {
  4. public Container container { get; private set; }
  5. public int port { get; private set; }
  6. private Server server;
  7. private Pipeline pipeline;
  8. public WebApplication(int? port = null) {
  9. this.port = port ?? int.parse(Environment.get_variable ("ASTRALIS_PORT") ?? "8080");
  10. printerr(@"[Astralis] Web application using port $(port)\n");
  11. container = new Container();
  12. pipeline = new Pipeline(container);
  13. server = new Server(this.port, pipeline);
  14. }
  15. public void run() throws Error {
  16. // Ensure router is registered last so that it is the last component in the pipeline.
  17. add_component<EndpointRouter>();
  18. var timer = new Timer();
  19. timer.start();
  20. container.initialise();
  21. timer.stop();
  22. printerr(@"[Astralis] Intialised application in $((int)(timer.elapsed() * 1000))ms\n");
  23. server.run();
  24. }
  25. public Registration add_endpoint<T>(EndpointRoute route, owned TypedFactoryDelegate<T>? endpoint_func = null) {
  26. return add_scoped<T>((owned) endpoint_func)
  27. .with_metadata<EndpointRoute>(route)
  28. .as<Endpoint>();
  29. }
  30. public Registration add_singleton_endpoint<T>(EndpointRoute route, owned TypedFactoryDelegate<T>? endpoint_func = null) {
  31. return add_singleton<T>((owned) endpoint_func)
  32. .with_metadata<EndpointRoute>(route)
  33. .as<Endpoint>();
  34. }
  35. public Registration add_startup_endpoint<T>(EndpointRoute route, owned TypedFactoryDelegate<T>? endpoint_func = null) {
  36. return add_startup<T>((owned) endpoint_func)
  37. .with_metadata<EndpointRoute>(route)
  38. .as<Endpoint>();
  39. }
  40. public Registration add_component<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
  41. return add_scoped<T>(construct_func).as<PipelineComponent>();
  42. }
  43. public Registration add_singleton_component<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
  44. return add_singleton<T>(construct_func).as<PipelineComponent>();
  45. }
  46. public Registration add_startup_component<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
  47. return add_startup<T>(construct_func).as<PipelineComponent>();
  48. }
  49. public Registration add_transient<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
  50. return container.register_transient<T>(construct_func);
  51. }
  52. public Registration add_scoped<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
  53. return container.register_scoped<T>(construct_func);
  54. }
  55. public Registration add_singleton<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
  56. return container.register_singleton<T>(construct_func);
  57. }
  58. public Registration add_startup<T>(owned TypedFactoryDelegate<T>? construct_func = null) {
  59. return container.register_startup<T>(construct_func);
  60. }
  61. public Registration add_module<T>(owned TypedFactoryDelegate<T>? construct_func = null) throws Error {
  62. return container.register_module<T>(construct_func);
  63. }
  64. public void use_compression() {
  65. add_component<GzipCompressor>();
  66. add_component<ZstdCompressor>();
  67. add_component<BrotliCompressor>();
  68. }
  69. }
  70. }