app-main.vala 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Astralis;
  2. using Inversion;
  3. using InvercargillSqlInversion;
  4. using Spry;
  5. using Spry.Authentication;
  6. using Statum;
  7. using {{NS}};
  8. int main(string[] args) {
  9. int port = args.length > 1 ? int.parse(args[1]) : 8080;
  10. try {
  11. var application = new WebApplication(port);
  12. application.use_compression();
  13. application.add_module<StatumModule>();
  14. application.add_module<SpryModule>();
  15. var db_path = Environment.get_variable("SPRY_DB_PATH") ?? "";
  16. if (db_path.strip().length == 0) {
  17. db_path = "./{{APP_NAME}}.sqlite";
  18. }
  19. var db = application.container.configure_with<DatabaseConfigurator>();
  20. db.register_connection(@"sqlite://$(db_path)");
  21. db.migrate_on_startup();
  22. var statum = application.configure_with<StatumConfigurator>();
  23. // spry:pages-begin
  24. statum.add_page<HomePage, HomeEntrypoint>();
  25. // spry:pages-end
  26. // spry:actions-begin
  27. // spry:actions-end
  28. // spry:resources-begin
  29. // spry:resources-end
  30. // spry:auth-begin
  31. // spry:auth-end
  32. GLib.Idle.add(() => {
  33. seed_admin.begin(application.container);
  34. return false;
  35. });
  36. application.run();
  37. return 0;
  38. } catch (Error e) {
  39. printerr("Error: %s\n", e.message);
  40. return 1;
  41. }
  42. }
  43. /**
  44. * Seeds the admin account when BOTH SPRY_SEED_ADMIN_USERNAME and
  45. * SPRY_SEED_ADMIN_PASSWORD are set; skipped silently otherwise. The
  46. * password is never logged.
  47. */
  48. private async void seed_admin(Container container) {
  49. var username = Environment.get_variable("SPRY_SEED_ADMIN_USERNAME") ?? "";
  50. var password = Environment.get_variable("SPRY_SEED_ADMIN_PASSWORD") ?? "";
  51. if (username.length == 0 || password.length == 0) {
  52. return;
  53. }
  54. try {
  55. var scope = container.create_scope();
  56. yield SpryAuth.ensure_admin(scope.resolve<UserService>(),
  57. username, @"$(username)@example.com", password);
  58. print("[{{APP_NAME}}] Seeded admin account: %s\n", username);
  59. } catch (Error e) {
  60. printerr("[{{APP_NAME}}] Admin seeding failed: %s\n", e.message);
  61. }
  62. }