SimpleApi.vala 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. using Astralis;
  2. using Invercargill;
  3. using Invercargill.DataStructures;
  4. // Simple handler for /hello endpoint
  5. class HelloEndpoint : Object, Endpoint {
  6. public async HttpResult handle_request(HttpContext http_context, RouteContext route) throws Error {
  7. print("Handling /hello\n");
  8. return new HttpStringResult("Hello from Astralis!");
  9. }
  10. }
  11. // Simple handler for /json endpoint
  12. class JsonEndpoint : Object, Endpoint {
  13. public async HttpResult handle_request(HttpContext http_context, RouteContext route) throws Error {
  14. print("Handling /json\n");
  15. return new HttpStringResult("{ \"message\": \"Hello JSON\" }")
  16. .set_header("Content-Type", "application/json");
  17. }
  18. }
  19. void main() {
  20. var application = new WebApplication(8080);
  21. application.container.register_scoped<Endpoint>(() => new HelloEndpoint())
  22. .with_metadata<EndpointRoute>(new EndpointRoute("/hello"));
  23. application.container.register_scoped<Endpoint>(() => new JsonEndpoint())
  24. .with_metadata<EndpointRoute>(new EndpointRoute("/json"));
  25. application.run();
  26. }