SimpleApi.vala 940 B

123456789101112131415161718192021222324252627282930
  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 HttpStringResult("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. return new HttpStringResult("{ \"message\": \"Hello JSON\" }")
  16. .set_header("Content-Type", "application/json");
  17. }
  18. }
  19. void main() {
  20. var router = new Router();
  21. var server = new Server(8080, router);
  22. router.map("/hello", new HelloHandler());
  23. router.map("/json", new JsonHandler());
  24. server.run();
  25. }