SimpleApi.vala 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Astralis;
  2. using Invercargill;
  3. using Invercargill.DataStructures;
  4. // Simple handler for /hello endpoint
  5. class HelloEndpoint : Object, Endpoint {
  6. public string route { get { return "/hello"; } }
  7. public Enumerable<Method> methods { owned get { return Iterate.these(Method.GET);} }
  8. public async HttpResult handle_request(HttpContext http_context, RouteInformation route) throws Error {
  9. print("Handling /hello\n");
  10. return new HttpStringResult("Hello from Astralis!");
  11. }
  12. }
  13. // Simple handler for /json endpoint
  14. class JsonEndpoint : Object, Endpoint {
  15. public string route { get { return "/json"; } }
  16. public Enumerable<Method> methods { owned get { return Iterate.these(Method.GET);} }
  17. public async HttpResult handle_request(HttpContext http_context, RouteInformation route) throws Error {
  18. print("Handling /json\n");
  19. return new HttpStringResult("{ \"message\": \"Hello JSON\" }")
  20. .set_header("Content-Type", "application/json");
  21. }
  22. }
  23. void main() {
  24. var router = new EndpointRouter()
  25. .add_endpoint(new HelloEndpoint())
  26. .add_endpoint(new JsonEndpoint());
  27. var pipeline = new Pipeline()
  28. .add_component(router);
  29. var server = new Server(8080, pipeline);
  30. server.run();
  31. }