Main.vala 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Astralis;
  2. using Invercargill.DataStructures;
  3. using Inversion;
  4. using Statum;
  5. using Example;
  6. void main(string[] args) {
  7. int port = args.length > 1 ? int.parse(args[1]) : 8080;
  8. try {
  9. var application = new WebApplication(port);
  10. application.use_compression();
  11. application.add_module<StatumModule>();
  12. var statum = application.configure_with<StatumConfigurator>();
  13. // HomePage is generated from home.html; HomeEntrypoint hydrates it and
  14. // embeds the login/bump action references. Routes come from each page's
  15. // <pstm-uri>; actions are auto-mapped to GUID endpoints; the Statum
  16. // client scripts are embedded by default.
  17. statum.add_page<HomePage, HomeEntrypoint>();
  18. statum.add_page<DashboardPage, DashboardEntrypoint>();
  19. statum.action<LoginAction>();
  20. statum.action<LogoutAction>();
  21. statum.action<BumpCounterAction>();
  22. statum.action<UpdateAnnouncementAction>();
  23. statum.add_resource<StylesResource>();
  24. // Register a global broadcast slot (deterministic key "global:announcement").
  25. var announcement = new AnnouncementPublic();
  26. announcement.text = "Welcome to the Statum demo!";
  27. statum.global<AnnouncementPublic>("announcement", announcement);
  28. // Background timer: cycle the announcement every 10 seconds. Every
  29. // subscribed tab receives the update via the SSE push — open two tabs
  30. // side by side to see the broadcast in action.
  31. string[] messages = {
  32. "Welcome to the Statum demo!",
  33. "Slots are client-carried and signed.",
  34. "Try logging in as \"admin\".",
  35. "Global slots use deterministic keys for broadcast.",
  36. "The counter uses typed models with action references."
  37. };
  38. int msg_index = 0;
  39. Timeout.add_seconds(10, () => {
  40. msg_index = (msg_index + 1) % messages.length;
  41. try {
  42. var model = new AnnouncementPublic();
  43. model.text = messages[msg_index];
  44. var state = new State() {
  45. type_name = "announcement",
  46. public_data = GObjectMapping.to_properties(model),
  47. private_data = new PropertyDictionary()
  48. };
  49. statum.state_service.update_global.begin("announcement", state);
  50. } catch (GLib.Error e) {
  51. warning("Failed to update announcement: %s", e.message);
  52. }
  53. return true;
  54. });
  55. application.run();
  56. } catch (Error e) {
  57. printerr("Error: %s\n", e.message);
  58. Process.exit(1);
  59. }
  60. }