SimpleApi.vala 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. using Astralis;
  2. using Invercargill;
  3. using Invercargill.DataStructures;
  4. // Simple handler for /hello endpoint
  5. class HelloHandler : Object, RouteHandler {
  6. public async HttpResult handle_route(HttpContext http_context, RouteContext route_context) throws Error {
  7. print("Handling /hello\n");
  8. return new BufferedHttpResult.from_string("Hello from Astralis!");
  9. }
  10. }
  11. // Simple handler for /json endpoint
  12. class JsonHandler : Object, RouteHandler {
  13. public async HttpResult handle_route(HttpContext http_context, RouteContext route_context) throws Error {
  14. print("Handling /json\n");
  15. var headers = new Catalogue<string, string>();
  16. headers.add("Content-Type", "application/json");
  17. return new BufferedHttpResult.from_string("{ \"message\": \"Hello JSON\" }", StatusCode.OK, headers);
  18. }
  19. }
  20. void main() {
  21. var router = new Router();
  22. var server = new Server(8080, router);
  23. router.map("/hello", new HelloHandler());
  24. router.map("/json", new JsonHandler());
  25. server.run();
  26. }