| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using Astralis;
- using Inversion;
- using InvercargillSqlInversion;
- using Spry;
- using Spry.Authentication;
- using Statum;
- using {{NS}};
- int main(string[] args) {
- int port = args.length > 1 ? int.parse(args[1]) : 8080;
- try {
- var application = new WebApplication(port);
- application.use_compression();
- application.add_module<StatumModule>();
- application.add_module<SpryModule>();
- var db_path = Environment.get_variable("SPRY_DB_PATH") ?? "";
- if (db_path.strip().length == 0) {
- db_path = "./{{APP_NAME}}.sqlite";
- }
- var db = application.container.configure_with<DatabaseConfigurator>();
- db.register_connection(@"sqlite://$(db_path)");
- db.migrate_on_startup();
- var statum = application.configure_with<StatumConfigurator>();
- // spry:pages-begin
- statum.add_page<HomePage, HomeEntrypoint>();
- // spry:pages-end
- // spry:actions-begin
- // spry:actions-end
- // spry:resources-begin
- // spry:resources-end
- // spry:auth-begin
- // spry:auth-end
- GLib.Idle.add(() => {
- seed_admin.begin(application.container);
- return false;
- });
- application.run();
- return 0;
- } catch (Error e) {
- printerr("Error: %s\n", e.message);
- return 1;
- }
- }
- /**
- * Seeds the admin account when BOTH SPRY_SEED_ADMIN_USERNAME and
- * SPRY_SEED_ADMIN_PASSWORD are set; skipped silently otherwise. The
- * password is never logged.
- */
- private async void seed_admin(Container container) {
- var username = Environment.get_variable("SPRY_SEED_ADMIN_USERNAME") ?? "";
- var password = Environment.get_variable("SPRY_SEED_ADMIN_PASSWORD") ?? "";
- if (username.length == 0 || password.length == 0) {
- return;
- }
- try {
- var scope = container.create_scope();
- yield SpryAuth.ensure_admin(scope.resolve<UserService>(),
- username, @"$(username)@example.com", password);
- print("[{{APP_NAME}}] Seeded admin account: %s\n", username);
- } catch (Error e) {
- printerr("[{{APP_NAME}}] Admin seeding failed: %s\n", e.message);
- }
- }
|