SimpleApi.vala 952 B

12345678910111213141516171819202122232425262728
  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.add_endpoint<HelloEndpoint>(new EndpointRoute("/hello"));
  22. application.add_endpoint<JsonEndpoint>(new EndpointRoute("/json"));
  23. application.run();
  24. }