app.vala 833 B

123456789101112131415161718192021222324252627282930313233
  1. using GLib;
  2. using VSGI;
  3. public class App : Handler
  4. {
  5. public override bool handle (Request req, Response res) throws Error
  6. {
  7. res.headers.replace ("Server", "VSGI/0.3");
  8. switch (req.uri.path)
  9. {
  10. case "/json":
  11. res.headers.set_content_type ("application/json", null);
  12. var builder = new Json.Builder ();
  13. builder.begin_object ();
  14. builder.set_member_name ("message");
  15. builder.add_string_value ("Hello, World!");
  16. builder.end_object ();
  17. var gen = new Json.Generator ();
  18. gen.root = builder.get_root ();
  19. return res.expand (gen.to_data (null).data);
  20. case "/plaintext":
  21. res.headers.set_content_type ("text/plain", null);
  22. return res.expand ("Hello, World!".data);
  23. default:
  24. return false;
  25. }
  26. }
  27. }
  28. Server.new ("http", handler: new App ()).run ({"app", "--address=0.0.0.0:8080"});