From 90986786ff912971fc7b907131898a10d47cccc5 Mon Sep 17 00:00:00 2001 From: crimson-knight Date: Mon, 9 Nov 2026 06:23:43 -0500 Subject: [PATCH 1/4] chore(new): pin generated apps to Amber v2-dev, Grant and asset_pipeline commits --- README.md | 2 +- docs/BETA_WEB_APP.md | 8 ++++---- docs/ONBOARDING.md | 4 ++-- scripts/check_beta_contract.sh | 12 ++++++------ scripts/smoke_generated_web.ps1 | 12 ++++++------ scripts/smoke_generated_web.sh | 18 +++++++++--------- spec/generators/native_app_spec.cr | 2 +- src/amber_cli/amber_release.cr | 6 +++--- src/amber_cli/templates/app/shard.yml.ecr | 20 ++++++-------------- 9 files changed, 38 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 539080d..c99cdff 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ or a file under `app/assets/` changes. Open . The web template is deliberately small: - Amber from `amberframework/amber`, pinned to `2.0.0-beta.10` by commit - (`d291e0930dd22b14a2d10f75a9de063abb0e64e4`), with its checksum (`sha256-v2:8d354db3b41625db0362162a34836fe10a8b11a378f3837d34cc6bbcece1cb94`) checked in `shard.lock` + (`d775841a0af43bf512f1c9b704906fccdf8863f7`), with its checksum (`sha256-v2:e1099a4f7a25db316b2abdf80f999a2753a0fcc056834f3abe6d9b5a88ae945d`) checked in `shard.lock` - ECR views (Slang and Kilt are not supported in Amber V2) - typed development, test, and production YAML - branded homepage, controller spec, and fingerprinted CSS, JavaScript, SVG, diff --git a/docs/BETA_WEB_APP.md b/docs/BETA_WEB_APP.md index 7106c7c..9e6c690 100644 --- a/docs/BETA_WEB_APP.md +++ b/docs/BETA_WEB_APP.md @@ -35,11 +35,11 @@ grep -E '^(template|database|model):' .amber.yml ``` The dependency must be `amberframework/amber` at commit -`d291e0930dd22b14a2d10f75a9de063abb0e64e4` (`v2.0.0-beta.10`); the +`d775841a0af43bf512f1c9b704906fccdf8863f7` (`2.0.0-beta.10` on `v2-dev`); the template must be `ecr`, the database must be `sqlite`, and the model layer must -be `grant`. `shard.yml` must include Grant, Asset Pipeline, and -`crystal-sqlite3`, but it must not include Gemma, Slang, PostgreSQL, or MySQL -unless you selected that database. +be `grant`. `shard.yml` must include Grant and Asset Pipeline pinned by commit, +and no database driver shard unless you selected MySQL. Grant ships its own +Postgres and SQLite drivers. The first page carries a compact version of Amber's V2 visual language: warm paper colors, a fingerprinted SVG crystal, editorial heading scale, status diff --git a/docs/ONBOARDING.md b/docs/ONBOARDING.md index a55021a..3242976 100644 --- a/docs/ONBOARDING.md +++ b/docs/ONBOARDING.md @@ -191,12 +191,12 @@ Pin the release's commit and move the lock: dependencies: amber: github: amberframework/amber - commit: d291e0930dd22b14a2d10f75a9de063abb0e64e4 # v2.0.0-beta.10 + commit: d775841a0af43bf512f1c9b704906fccdf8863f7 # 2.0.0-beta.10 on v2-dev ``` ```bash minecart update amber -grep -A3 '^ amber:' shard.lock # checksum: sha256-v2:8d354db3b41625db0362162a34836fe10a8b11a378f3837d34cc6bbcece1cb94 +grep -A3 '^ amber:' shard.lock # checksum: sha256-v2:e1099a4f7a25db316b2abdf80f999a2753a0fcc056834f3abe6d9b5a88ae945d amber setup:agent ``` diff --git a/scripts/check_beta_contract.sh b/scripts/check_beta_contract.sh index e8c0e62..e5f56d4 100755 --- a/scripts/check_beta_contract.sh +++ b/scripts/check_beta_contract.sh @@ -7,18 +7,18 @@ test "$shard_version" = "2.0.10" test "$cli_version" = "$shard_version" grep -F 'github: amberframework/amber' src/amber_cli/commands/new_command.cr -grep -F 'commit: d291e0930dd22b14a2d10f75a9de063abb0e64e4 # v2.0.0-beta.10' src/amber_cli/commands/new_command.cr -grep -F 'COMMIT = "d291e0930dd22b14a2d10f75a9de063abb0e64e4"' src/amber_cli/amber_release.cr -grep -F 'TREE = "fb4574c6b15b60357abd43ed0a1a8d7a0957e574"' src/amber_cli/amber_release.cr -grep -F 'CHECKSUM = "sha256-v2:8d354db3b41625db0362162a34836fe10a8b11a378f3837d34cc6bbcece1cb94"' src/amber_cli/amber_release.cr +grep -F 'commit: #{AmberRelease::COMMIT} # #{AmberRelease::VERSION} on v2-dev' src/amber_cli/commands/new_command.cr +grep -F 'COMMIT = "d775841a0af43bf512f1c9b704906fccdf8863f7"' src/amber_cli/amber_release.cr +grep -F 'TREE = "e06f26daef02a30e91114b2c71224920e0c097de"' src/amber_cli/amber_release.cr +grep -F 'CHECKSUM = "sha256-v2:e1099a4f7a25db316b2abdf80f999a2753a0fcc056834f3abe6d9b5a88ae945d"' src/amber_cli/amber_release.cr grep -F 'template: ecr' src/amber_cli/commands/new_command.cr grep -F 'model: grant' src/amber_cli/commands/new_command.cr grep -F 'database: #{database}' src/amber_cli/commands/new_command.cr grep -F 'github: crimson-knight/grant' src/amber_cli/commands/new_command.cr grep -F 'github: amberframework/asset_pipeline' src/amber_cli/commands/new_command.cr -grep -F 'version: 0.37.1' src/amber_cli/commands/new_command.cr +grep -F 'commit: ac630a4c948080bf45e9b1e66451c48da9a69349 # 0.37.1' src/amber_cli/commands/new_command.cr grep -F 'github: amberframework/asset_pipeline' src/amber_cli/templates/app/shard.yml.ecr -grep -F 'version: 0.37.1' src/amber_cli/templates/app/shard.yml.ecr +grep -F 'commit: ac630a4c948080bf45e9b1e66451c48da9a69349 # 0.37.1' src/amber_cli/templates/app/shard.yml.ecr grep -F 'github: amberframework/amber' src/amber_cli/generators/native_app.cr grep -F 'commit: d291e0930dd22b14a2d10f75a9de063abb0e64e4 # v2.0.0-beta.10' src/amber_cli/generators/native_app.cr grep -F 'version: ~> 0.37.1' src/amber_cli/generators/native_app.cr diff --git a/scripts/smoke_generated_web.ps1 b/scripts/smoke_generated_web.ps1 index 0ca98e9..b4a2b57 100644 --- a/scripts/smoke_generated_web.ps1 +++ b/scripts/smoke_generated_web.ps1 @@ -43,19 +43,19 @@ $amberConfig = [System.IO.File]::ReadAllText($amberConfigPath) if (-not $manifest.Contains("github: amberframework/amber")) { throw "Generated shard.yml does not include Amber" } -if (-not $manifest.Contains("commit: d291e0930dd22b14a2d10f75a9de063abb0e64e4 # v2.0.0-beta.10")) { +if (-not $manifest.Contains("commit: d775841a0af43bf512f1c9b704906fccdf8863f7 # 2.0.0-beta.10 on v2-dev")) { throw "Generated shard.yml does not pin the supported Amber beta" } if (-not $manifest.Contains("github: crimson-knight/grant")) { throw "Generated shard.yml does not include Grant" } -if (-not $manifest.Contains("github: crystal-lang/crystal-sqlite3")) { - throw "Generated shard.yml does not include SQLite" +if ($manifest.Contains("crystal-sqlite3")) { + throw "Generated shard.yml lists a SQLite driver; Grant owns it" } if (-not $manifest.Contains("github: amberframework/asset_pipeline")) { throw "Generated shard.yml does not include Asset Pipeline" } -if (-not $manifest.Contains("version: 0.37.1")) { +if (-not $manifest.Contains("commit: ac630a4c948080bf45e9b1e66451c48da9a69349 # 0.37.1")) { throw "Generated shard.yml does not pin the supported Asset Pipeline release" } if (-not (Test-Path (Join-Path $appPath ".claude/CLAUDE.md"))) { @@ -74,7 +74,7 @@ if (-not $amberConfig.Contains("database: sqlite") -or -not $amberConfig.Contain if ($FrameworkCommit) { $releasedRepository = " github: amberframework/amber" - $releasedFramework = " commit: d291e0930dd22b14a2d10f75a9de063abb0e64e4 # v2.0.0-beta.10" + $releasedFramework = " commit: d775841a0af43bf512f1c9b704906fccdf8863f7 # 2.0.0-beta.10 on v2-dev" if (-not $manifest.Contains($releasedRepository)) { throw "Generated shard.yml does not contain the expected Amber repository" } @@ -118,7 +118,7 @@ try { $expectedAmberCommit = $FrameworkCommit $expectedAmberGit = "https://github.com/$FrameworkRepository.git" } else { - $expectedAmberCommit = "d291e0930dd22b14a2d10f75a9de063abb0e64e4" + $expectedAmberCommit = "d775841a0af43bf512f1c9b704906fccdf8863f7" $expectedAmberGit = "https://github.com/amberframework/amber.git" } if ($amberEntry -notmatch "(?m)^ git: $([regex]::Escape($expectedAmberGit))\r?$") { diff --git a/scripts/smoke_generated_web.sh b/scripts/smoke_generated_web.sh index 46dc307..ae264f6 100755 --- a/scripts/smoke_generated_web.sh +++ b/scripts/smoke_generated_web.sh @@ -46,12 +46,12 @@ grep -F 'AMBER_ENV=test crystal spec' "$script_dir/../src/amber_cli/templates/ap ! grep -F 'crystal-alpha' "$script_dir/../src/amber_cli/templates/app/.amber.yml.ecr" grep -F "github: amberframework/amber" "$app_path/shard.yml" -grep -F "commit: d291e0930dd22b14a2d10f75a9de063abb0e64e4 # v2.0.0-beta.10" "$app_path/shard.yml" +grep -F "commit: d775841a0af43bf512f1c9b704906fccdf8863f7 # 2.0.0-beta.10 on v2-dev" "$app_path/shard.yml" grep -F "github: crimson-knight/grant" "$app_path/shard.yml" grep -F "github: amberframework/asset_pipeline" "$app_path/shard.yml" -grep -F "version: 0.37.1" "$app_path/shard.yml" -grep -F "commit: 6c9da3c8180e612f76ff9ad9c76242a0402ce6d9" "$app_path/shard.yml" -grep -F "github: crystal-lang/crystal-sqlite3" "$app_path/shard.yml" +grep -F "commit: ac630a4c948080bf45e9b1e66451c48da9a69349 # 0.37.1" "$app_path/shard.yml" +grep -F "commit: 623c4bcad284fcee000244c8b26651f925e23ceb" "$app_path/shard.yml" +! grep -F "crystal-sqlite3" "$app_path/shard.yml" grep -F "template: ecr" "$app_path/.amber.yml" grep -F "database: sqlite" "$app_path/.amber.yml" grep -F "model: grant" "$app_path/.amber.yml" @@ -68,7 +68,7 @@ fi if [[ -n "$framework_commit" ]]; then sed -i.bak -E "s# github: amberframework/amber# github: ${framework_repository}#" "$app_path/shard.yml" - sed -i.bak -E "s/ commit: d291e0930dd22b14a2d10f75a9de063abb0e64e4( # v[^ ]+)?$/ commit: ${framework_commit}/" "$app_path/shard.yml" + sed -i.bak -E "s/ commit: d775841a0af43bf512f1c9b704906fccdf8863f7( # [^#]+)?$/ commit: ${framework_commit}/" "$app_path/shard.yml" rm -f "$app_path/shard.yml.bak" fi @@ -86,7 +86,7 @@ else minecart install --frozen fi test -s shard.lock -grep -F "version: 0.23.4+git.commit.6c9da3c8180e612f76ff9ad9c76242a0402ce6d9" shard.lock +grep -F "version: 0.23.4+git.commit.623c4bcad284fcee000244c8b26651f925e23ceb" shard.lock grep -F "version: 0.14.0" shard.lock # Each shard entry carries exactly one checksum. awk ' @@ -100,14 +100,14 @@ if [[ -n "$framework_commit" ]]; then expected_amber_commit="$framework_commit" expected_amber_git="https://github.com/${framework_repository}.git" else - expected_amber_commit="d291e0930dd22b14a2d10f75a9de063abb0e64e4" + expected_amber_commit="d775841a0af43bf512f1c9b704906fccdf8863f7" expected_amber_git="https://github.com/amberframework/amber.git" fi grep -qxF " git: ${expected_amber_git}" <<<"$amber_entry" grep -qE "^ version: [^ ]+\+git\.commit\.${expected_amber_commit}$" <<<"$amber_entry" if [[ -z "$framework_commit" ]]; then - grep -qxF " version: 2.0.0-beta.10+git.commit.d291e0930dd22b14a2d10f75a9de063abb0e64e4" <<<"$amber_entry" - grep -qxF " checksum: sha256-v2:8d354db3b41625db0362162a34836fe10a8b11a378f3837d34cc6bbcece1cb94" <<<"$amber_entry" + grep -qxF " version: 2.0.0-beta.10+git.commit.d775841a0af43bf512f1c9b704906fccdf8863f7" <<<"$amber_entry" + grep -qxF " checksum: sha256-v2:e1099a4f7a25db316b2abdf80f999a2753a0fcc056834f3abe6d9b5a88ae945d" <<<"$amber_entry" fi "$cli_path" assets build "$cli_path" assets check diff --git a/spec/generators/native_app_spec.cr b/spec/generators/native_app_spec.cr index fe80a83..152c5b9 100644 --- a/spec/generators/native_app_spec.cr +++ b/spec/generators/native_app_spec.cr @@ -42,7 +42,7 @@ describe AmberCLI::Generators::NativeApp do # Must have amber (patterns only) shard_content.should contain("amber:") shard_content.should contain("github: amberframework/amber") - shard_content.should contain("commit: #{AmberCLI::AmberRelease::COMMIT} # v#{AmberCLI::AmberRelease::VERSION}") + shard_content.should contain("commit: d291e0930dd22b14a2d10f75a9de063abb0e64e4 # v2.0.0-beta.10") shard_content.should contain("github: crimson-knight/grant") shard_content.should contain("commit: 6c9da3c8180e612f76ff9ad9c76242a0402ce6d9") File.exists?(File.join(project_path, "shard.lock")).should be_false diff --git a/src/amber_cli/amber_release.cr b/src/amber_cli/amber_release.cr index 192405d..a924cab 100644 --- a/src/amber_cli/amber_release.cr +++ b/src/amber_cli/amber_release.cr @@ -5,9 +5,9 @@ module AmberCLI # them together. module AmberRelease VERSION = "2.0.0-beta.10" - COMMIT = "d291e0930dd22b14a2d10f75a9de063abb0e64e4" - TREE = "fb4574c6b15b60357abd43ed0a1a8d7a0957e574" + COMMIT = "d775841a0af43bf512f1c9b704906fccdf8863f7" + TREE = "e06f26daef02a30e91114b2c71224920e0c097de" # What Minecart 2025.11.25.9 and newer record for this tree in `shard.lock`. - CHECKSUM = "sha256-v2:8d354db3b41625db0362162a34836fe10a8b11a378f3837d34cc6bbcece1cb94" + CHECKSUM = "sha256-v2:e1099a4f7a25db316b2abdf80f999a2753a0fcc056834f3abe6d9b5a88ae945d" end end diff --git a/src/amber_cli/templates/app/shard.yml.ecr b/src/amber_cli/templates/app/shard.yml.ecr index 1a78c86..c3d9926 100644 --- a/src/amber_cli/templates/app/shard.yml.ecr +++ b/src/amber_cli/templates/app/shard.yml.ecr @@ -15,24 +15,16 @@ targets: dependencies: amber: github: amberframework/amber - commit: d291e0930dd22b14a2d10f75a9de063abb0e64e4 # v2.0.0-beta.10 + commit: d775841a0af43bf512f1c9b704906fccdf8863f7 # 2.0.0-beta.10 on v2-dev grant: github: crimson-knight/grant - commit: 6c9da3c8180e612f76ff9ad9c76242a0402ce6d9 + commit: 623c4bcad284fcee000244c8b26651f925e23ceb # 0.23.4 asset_pipeline: github: amberframework/asset_pipeline - version: 0.37.1 -<% case @database -when "pg" -%> - pg: - github: will/crystal-pg - version: 0.30.0 -<% when "mysql" -%> + commit: ac630a4c948080bf45e9b1e66451c48da9a69349 # 0.37.1 +<%# Grant ships its own Postgres and SQLite drivers; only MySQL needs a shard. -%> +<% if @database == "mysql" -%> mysql: github: crimson-knight/crystal-mysql - commit: c061324dcef89a200a7a3f86a59b2ebf03f83602 -<% else -%> - sqlite3: - github: crystal-lang/crystal-sqlite3 - version: 0.23.0 + commit: c061324dcef89a200a7a3f86a59b2ebf03f83602 # 0.17.0 <% end -%> From 4ab257f8cf6ab0bc0a7f1564a6f588c143b782b8 Mon Sep 17 00:00:00 2001 From: crimson-knight Date: Mon, 9 Nov 2026 06:23:43 -0500 Subject: [PATCH 2/4] feat(new): generate apps that start tuned --- spec/commands/new_command_spec.cr | 96 ++++++++- src/amber_cli/commands/new_command.cr | 268 +++++++++++++++++++++----- 2 files changed, 314 insertions(+), 50 deletions(-) diff --git a/spec/commands/new_command_spec.cr b/spec/commands/new_command_spec.cr index b4cf527..2ed91c7 100644 --- a/spec/commands/new_command_spec.cr +++ b/spec/commands/new_command_spec.cr @@ -172,15 +172,16 @@ describe AmberCLI::Commands::NewCommand do shard = File.read(File.join(destination, "shard.yml")) shard.should contain("github: amberframework/amber") - shard.should contain("commit: #{AmberCLI::AmberRelease::COMMIT} # v#{AmberCLI::AmberRelease::VERSION}") + shard.should contain("commit: #{AmberCLI::AmberRelease::COMMIT} # #{AmberCLI::AmberRelease::VERSION} on v2-dev") shard.should contain("grant:") shard.should contain("github: crimson-knight/grant") - shard.should contain("commit: 6c9da3c8180e612f76ff9ad9c76242a0402ce6d9") + shard.should contain("commit: 623c4bcad284fcee000244c8b26651f925e23ceb") shard.should contain("asset_pipeline:") shard.should contain("github: amberframework/asset_pipeline") - shard.should contain("version: 0.37.1") + shard.should contain("commit: ac630a4c948080bf45e9b1e66451c48da9a69349") shard.should_not contain("version: ~> 0.37.1") - shard.should contain("github: crystal-lang/crystal-sqlite3") + shard.should_not contain("crystal-sqlite3") + shard.should_not contain("crystal-pg") shard.should_not contain("slang") File.exists?(File.join(destination, "shard.lock")).should be_false @@ -203,7 +204,8 @@ describe AmberCLI::Commands::NewCommand do database_config = File.read(File.join(destination, "config/database.cr")) database_config.should contain(%(require "grant/adapter/sqlite")) - database_config.should contain(%(name: "primary")) + database_config.should contain(%(database: "primary")) + database_config.should contain("Grant::ConnectionRegistry.establish_connection") database_config.should contain(%(ENV["DATABASE_URL"]? || Amber.settings.database_url)) application_config = File.read(File.join(destination, "config/application.cr")) @@ -312,6 +314,88 @@ describe AmberCLI::Commands::NewCommand do end end + describe "tuned defaults" do + it "pins Grant's own drivers and lists no database driver shard for Postgres" do + SpecHelper.within_temp_directory do |temp_dir| + destination = File.join(temp_dir, "tuned_pg") + AmberCLI::Commands::NewCommand.new("new").parse_and_execute([destination, "--type=web", "--no-deps", "--skip-agent-setup", "-d", "pg"]) + + shard = File.read(File.join(destination, "shard.yml")) + shard.should_not contain("crystal-pg") + + production = YAML.parse(File.read(File.join(destination, "config/environments/production.yml"))) + production["database"]["url"].as_s.should eq("postgres:///tuned_pg_production?host=/var/run/postgresql") + File.read(File.join(destination, "README.md")).should contain("DATABASE_URL") + end + end + + it "keeps the mysql shard pinned to the version Grant locks" do + SpecHelper.within_temp_directory do |temp_dir| + destination = File.join(temp_dir, "tuned_mysql") + AmberCLI::Commands::NewCommand.new("new").parse_and_execute([destination, "--type=web", "--no-deps", "--skip-agent-setup", "-d", "mysql"]) + + shard = File.read(File.join(destination, "shard.yml")) + shard.should contain("github: crimson-knight/crystal-mysql") + shard.should contain("commit: c061324dcef89a200a7a3f86a59b2ebf03f83602") + end + end + + it "generates a sized pool, the SQLite page cache and production defaults" do + SpecHelper.within_temp_directory do |temp_dir| + destination = File.join(temp_dir, "tuned_sqlite") + AmberCLI::Commands::NewCommand.new("new").parse_and_execute([destination, "--type=web", "--no-deps", "--skip-agent-setup"]) + + database_config = File.read(File.join(destination, "config/database.cr")) + database_config.should contain(%(ENV["DB_POOL_SIZE"]? || "4")) + database_config.should contain("pool_size: connections_in_pool") + database_config.should contain(%({"cache_size" => "-8000"})) + + production = YAML.parse(File.read(File.join(destination, "config/environments/production.yml"))) + production["database"]["url"].as_s.should eq("sqlite3:./db/tuned_sqlite_production.db") + production["session"]["expires"].as_i.should eq(1_209_600) + production["static"]["headers"]["Cache-Control"].as_s.should eq("no-cache") + + routes = File.read(File.join(destination, "config/routes.cr")) + lean_block = routes[routes.index!("pipeline :lean_read do")...routes.index!("pipeline :web do")] + lean_block.should contain("Amber::Pipe::Error.new") + lean_block.should contain("Amber::Pipe::SecurityHeaders.new") + lean_block.should_not contain("Session") + routes.should contain("plug Amber::Pipe::Logger.new unless Amber.env.production?") + routes.should_not contain("plug Amber::Pipe::Logger.new\n") + routes.should contain("plug CompressedResponsePipe.new") + routes.should contain(%(get "/", HomeController, :index)) + routes.index!("routes :lean_read do").should be < routes.index!(%(get "/", HomeController)) + + pipe = File.read(File.join(destination, "src/pipes/compressed_response_pipe.cr")) + pipe.should contain("HTTP::CompressHandler.new") + pipe.should contain(%("Vary", "Accept-Encoding")) + + page_cache_config = File.read(File.join(destination, "config/initializers/page_cache.cr")) + page_cache_config.should contain("config.use_memory_cache") + page_cache_config.should contain(%(ENV["PAGE_CACHE_MB"]? || "32")) + + main = File.read(File.join(destination, "src/tuned_sqlite.cr")) + main.index!("Components::StatelessView.warm_all").should be < main.index!("Amber::Server.start") + + File.read(File.join(destination, "src/components/site_header.cr")).should contain("struct SiteHeader < Components::StatelessView") + File.read(File.join(destination, "src/components/site_footer.cr")).should contain("struct SiteFooter < Components::StatelessView") + layout = File.read(File.join(destination, "src/views/layouts/application.ecr")) + layout.should contain("SiteHeader.new.cached_html") + layout.should contain("SiteFooter.new.cached_html") + + home = File.read(File.join(destination, "src/controllers/home_controller.cr")) + home.should contain("Components::Cache.page_cache.answer") + + File.read(File.join(destination, "db/seeds.cr")).should contain(%(require "../src/pipes/**")) + + readme = File.read(File.join(destination, "README.md")) + %w[DB_POOL_SIZE PAGE_CACHE_MB lean_read CompressedResponsePipe per-visitor process_count].each do |phrase| + readme.should contain(phrase) + end + end + end + end + it "requires config/initializers before the generated config files that read Amber.settings" do SpecHelper.within_temp_directory do |temp_dir| destination = File.join(temp_dir, "initializer_app") @@ -321,7 +405,7 @@ describe AmberCLI::Commands::NewCommand do initializers_directory = File.join(destination, "config/initializers") Dir.exists?(initializers_directory).should be_true - Dir.children(initializers_directory).should eq([".keep"]) + Dir.children(initializers_directory).sort.should eq([".keep", "page_cache.cr"]) File.read(File.join(initializers_directory, ".keep")).should be_empty application_config = File.read(File.join(destination, "config/application.cr")) diff --git a/src/amber_cli/commands/new_command.cr b/src/amber_cli/commands/new_command.cr index 9bd96dc..91c4f56 100644 --- a/src/amber_cli/commands/new_command.cr +++ b/src/amber_cli/commands/new_command.cr @@ -354,7 +354,8 @@ module AmberCLI::Commands "spec/jobs", "spec/mailers", "spec/channels", "spec/requests", # Source directories "src", "src/controllers", "src/models", - "src/views", "src/views/layouts", "src/views/home", + "src/views", "src/views/layouts", "src/views/home", "src/views/components", + "src/pipes", "src/components", "src/schemas", "src/jobs", "src/mailers", "src/channels", "src/sockets", ] @@ -376,6 +377,8 @@ module AmberCLI::Commands create_environment_files(path, name) create_home_controller(path, name) create_application_controller(path) + create_compression_pipe(path) + create_layout_components(path) create_views(path, name) create_spec_helper(path, name) create_home_controller_spec(path) @@ -407,13 +410,13 @@ targets: dependencies: amber: github: amberframework/amber - commit: d291e0930dd22b14a2d10f75a9de063abb0e64e4 # v2.0.0-beta.10 + commit: #{AmberRelease::COMMIT} # #{AmberRelease::VERSION} on v2-dev grant: github: crimson-knight/grant - commit: 6c9da3c8180e612f76ff9ad9c76242a0402ce6d9 + commit: 623c4bcad284fcee000244c8b26651f925e23ceb # 0.23.4 asset_pipeline: github: amberframework/asset_pipeline - version: 0.37.1 + commit: ac630a4c948080bf45e9b1e66451c48da9a69349 # 0.37.1 #{database_shard_dependency} SHARD @@ -439,7 +442,7 @@ AMBER readme_content = <<-README # #{name} -An ECR web application generated by Amber CLI for Amber `2.0.0-beta.10`. +An ECR web application generated by Amber CLI for Amber `#{AmberRelease::VERSION}`. ## Run it @@ -483,8 +486,51 @@ server database. Set `DATABASE_URL` to override the environment YAML URL. ## Production configuration -Set at least `AMBER_SERVER_SECRET_KEY_BASE` and `DATABASE_URL`. See the +Set at least `AMBER_SERVER_SECRET_KEY_BASE`. See the [Amber V2 beta guide](https://github.com/amberframework/amber/blob/v2.0.0-beta.10/docs/beta-installation.md). + +## What is on by default + +This application starts tuned. Each setting below is on from the first run, and +each one says where to change it. + +The connection pool holds 4 connections. It is set in `config/database.cr` through +Grant's connection registry; set `DB_POOL_SIZE` to change it. + +#{production_database_note(name)} + +Public read pages run through the `lean_read` pipeline in `config/routes.cr`, with +only the error and security-header pipes. Forms and anything else that needs a +session stay on the `web` pipeline, with the session, flash and CSRF pipes. To +give a page a session, move its route to `routes :web`. + +Production writes no per-request log line. The edge proxy keeps access logs. The +`Amber::Pipe::Logger` lines in `config/routes.cr` show where to turn it back on. + +Whole pages come from memory. A scaffold's `index` and `show` actions answer through +`Components::Cache.page_cache`, and its write actions clear exactly the pages they +changed. The first request for a page stores it; the second stores a gzip copy and +an ETag, and later requests are answered from those bytes with a `304` when the +browser already has the page. `config/initializers/page_cache.cr` sets the memory +limit, 32 MiB unless `PAGE_CACHE_MB` says otherwise. + +The page cache lives inside one process, so a write clears it at once. Keep +`server.process_count` at 1, its default, or a write in one process leaves the +other processes showing the old page. + +Pages in the page cache are the same for every visitor, so per-visitor content +stays out of them. To mark a page per-visitor, move its route to `routes :web` and +replace its `answer` block with a plain `render`; nothing is stored for it then. + +Layout pieces with no runtime state, such as `SiteHeader` and `SiteFooter` in +`src/components/`, are `Components::StatelessView` structs. They are rendered once +at boot, before the server accepts traffic, and the boot log reports how many. + +HTML on the `web` pipeline and static files are gzipped for clients that accept it +by `CompressedResponsePipe` in `src/pipes/`. Remove its `plug` line from a pipeline +in `config/routes.cr` to turn that off. Fingerprinted files under `/assets` are sent +with `Cache-Control: public, max-age=31536000, immutable`; other static files use +the `static.headers` setting in `config/environments/`. README write_text(File.join(path, "README.md"), readme_content) @@ -545,6 +591,8 @@ POLICY private def create_main_file(path : String, name : String) main_content = <<-MAIN require "../config/*" +require "./pipes/**" +require "./components/**" require "./controllers/**" require "./models/**" require "./schemas/**" @@ -552,6 +600,12 @@ require "./jobs/**" require "./mailers/**" require "./channels/**" +# Stateless views render once here, before the server accepts traffic. +warm_report = Components::StatelessView.warm_all +Log.for("boot").info do + "warmed \#{warm_report.list_of_warmed_view_names.size} stateless views (\#{warm_report.held_bytes} bytes, \#{warm_report.list_of_failed_view_names.size} failed)" +end + Amber::Server.start MAIN @@ -575,13 +629,34 @@ require "amber" require "grant" require "grant/adapter/#{database_adapter_require}" -Grant::Connections << Grant::Adapter::#{database_adapter_class}.new( - name: "primary", - url: ENV["DATABASE_URL"]? || Amber.settings.database_url +# One app process gains nothing from a large pool, and each connection costs +# memory on the database server. DB_POOL_SIZE changes it. +connections_in_pool = (ENV["DB_POOL_SIZE"]? || "4").to_i + +#{connection_url_source}Grant::ConnectionRegistry.establish_connection( + database: "primary", + adapter: Grant::Adapter::#{database_adapter_class}, + url: connection_url, + pool_size: connections_in_pool, + initial_pool_size: connections_in_pool, + max_idle_pool_size: connections_in_pool ) CONFIG write_text(File.join(path, "config/database.cr"), database_config) + + page_cache_config = <<-CONFIG +require "asset_pipeline/components/base/stateless_view" +require "asset_pipeline/components/cache/configuration" + +# Whole pages, their gzip copies and ETags live in this process's memory, up to this +# many bytes; the least recently used pages leave first. PAGE_CACHE_MB changes it. +Components::Cache.configure do |config| + config.use_memory_cache((ENV["PAGE_CACHE_MB"]? || "32").to_i64 * 1_048_576) +end +CONFIG + + write_text(File.join(path, "config/initializers/page_cache.cr"), page_cache_config) end private def create_assets_config(path : String) @@ -592,6 +667,27 @@ CONFIG write_text(File.join(path, "config/assets.cr"), assets_config) end + private def create_compression_pipe(path : String) + pipe_content = <<-PIPE +# Gzips a pipeline's responses for clients that accept it, through Crystal's own handler. +class CompressedResponsePipe < Amber::Pipe::Base + def initialize + @compress_handler = HTTP::CompressHandler.new + end + + # A byte range refers to the uncompressed file, so a range request is answered uncompressed. + def call(context : HTTP::Server::Context) : Nil + return call_next(context) if context.request.headers.has_key?("Range") + context.response.headers.add("Vary", "Accept-Encoding") + @compress_handler.next = self.next + @compress_handler.call(context) + end +end +PIPE + + write_text(File.join(path, "src/pipes/compressed_response_pipe.cr"), pipe_content) + end + private def create_application_controller(path : String) controller_content = <<-CONTROLLER class ApplicationController < Amber::Controller::Base @@ -608,16 +704,25 @@ CONTROLLER private def create_routes_file(path : String, name : String) routes_content = <<-ROUTES Amber::Server.configure do + # Public read pages: no session, no flash, no CSRF token, nothing per visitor. + pipeline :lean_read do + plug Amber::Pipe::Error.new + plug Amber::Pipe::SecurityHeaders.new + end + + # Forms and anything else that needs a session. pipeline :web do + plug CompressedResponsePipe.new plug Amber::Pipe::Error.new plug Amber::Pipe::SecurityHeaders.new - plug Amber::Pipe::Logger.new + plug Amber::Pipe::Logger.new unless Amber.env.production? # the edge proxy keeps production access logs plug Amber::Pipe::Session.new plug Amber::Pipe::Flash.new plug Amber::Pipe::CSRF.new end pipeline :static do + plug CompressedResponsePipe.new plug Amber::Pipe::Error.new plug Amber::Pipe::SecurityHeaders.new plug Amber::Pipe::Static.new("./public") @@ -626,13 +731,16 @@ Amber::Server.configure do pipeline :api do plug Amber::Pipe::Error.new plug Amber::Pipe::SecurityHeaders.new - plug Amber::Pipe::Logger.new + plug Amber::Pipe::Logger.new unless Amber.env.production? end - routes :web do + routes :lean_read do get "/", HomeController, :index end + routes :web do + end + # API routes must stay above the static block, whose wildcard GET route # would otherwise answer /api/... requests. # routes :api do @@ -659,7 +767,7 @@ ROUTES dev_config = environment_config(name, database_urls[0], "debug") test_config = environment_config(name, database_urls[1], "warn") - prod_config = environment_config(name, "", "info", host: "0.0.0.0", secret: "") + prod_config = environment_config(name, production_database_url(name), "info", host: "0.0.0.0", secret: "") write_text(File.join(path, "config/environments/development.yml"), dev_config) write_text(File.join(path, "config/environments/test.yml"), test_config) @@ -692,6 +800,12 @@ session: # end when the browser closes and lets server-side sessions live until evicted. expires: 1209600 +static: + # Fingerprinted files under /assets get `public, max-age=31536000, immutable` from + # Amber::Pipe::Static. This value applies to every other file in public/. + headers: + Cache-Control: "no-cache" + logging: severity: "#{severity}" colorize: true @@ -701,8 +815,11 @@ YML private def create_home_controller(path : String, name : String) home_controller = <<-CONTROLLER class HomeController < ApplicationController + # The page is the same for every visitor, so it comes from the page cache. def index - render("index.#{template}") + Components::Cache.page_cache.answer(context, "home") do + render("index.#{template}") + end end end CONTROLLER @@ -722,7 +839,9 @@ CONTROLLER <%= stylesheet_link_tag("stylesheets/app.css") %> + <%= SiteHeader.new.cached_html %> <%= content %> + <%= SiteFooter.new.cached_html %> <%= javascript_importmap_tag({"app" => "javascript/app.js"}, preload: ["javascript/app.js"]) %> @@ -732,14 +851,6 @@ LAYOUT index_content = <<-VIEW
-
- - <%= image_tag("images/amber-crystal.svg", class: "starter-crystal", alt: "") %> - <%= Amber.settings.name %> - - Amber V2 beta -
-

Amber V2 ยท Web application

@@ -764,16 +875,50 @@ LAYOUT
- -
- Generated by Amber CLI - Built with Crystal -
VIEW write_text(File.join(path, "src/views/home/index.ecr"), index_content) end + private def create_layout_components(path : String) + header_component = <<-COMPONENT +# The site header. It reads only build-time settings, so it renders once at boot. +struct SiteHeader < Components::StatelessView + include Amber::Controller::Helpers::AssetHelpers + + ECR.def_to_s "src/views/components/site_header.ecr" +end +COMPONENT + write_text(File.join(path, "src/components/site_header.cr"), header_component) + + footer_component = <<-COMPONENT +# The site footer. It has no runtime state, so it renders once at boot. +struct SiteFooter < Components::StatelessView + ECR.def_to_s "src/views/components/site_footer.ecr" +end +COMPONENT + write_text(File.join(path, "src/components/site_footer.cr"), footer_component) + + header_view = <<-VIEW +
+ + <%= image_tag("images/amber-crystal.svg", class: "starter-crystal", alt: "") %> + <%= escape_text(Amber.settings.name) %> + + Amber V2 beta +
+VIEW + write_text(File.join(path, "src/views/components/site_header.ecr"), header_view) + + footer_view = <<-VIEW +
+ Generated by Amber CLI + Built with Crystal +
+VIEW + write_text(File.join(path, "src/views/components/site_footer.ecr"), footer_view) + end + private def create_spec_helper(path : String, name : String) spec_helper = <<-SPEC # Specs use the test database. This must run before config/ reads the @@ -782,6 +927,8 @@ ENV["AMBER_ENV"] ||= "test" require "spec" require "../config/*" +require "../src/pipes/**" +require "../src/components/**" require "../src/controllers/**" require "../src/models/**" require "../src/schemas/**" @@ -820,7 +967,14 @@ SPEC private def create_seeds_file(path : String) seeds_content = <<-SEEDS require "../config/*" +require "../src/pipes/**" +require "../src/components/**" +require "../src/controllers/**" require "../src/models/**" +require "../src/schemas/**" +require "../src/jobs/**" +require "../src/mailers/**" +require "../src/channels/**" # Database seed file # @@ -901,7 +1055,7 @@ a { .amber-starter { position: relative; - min-height: 100vh; + min-height: calc(100vh - 146px); overflow: hidden; background: radial-gradient(circle at 78% 21%, rgba(243, 163, 72, 0.22), transparent 22rem), @@ -1291,27 +1445,53 @@ ROBOTS File.write(path, content.ends_with?("\n") ? content : "#{content}\n") end + # Grant ships its own Postgres and SQLite drivers; only MySQL needs a shard. private def database_shard_dependency : String + return "" unless database == "mysql" + + <<-YAML + mysql: + github: crimson-knight/crystal-mysql + commit: c061324dcef89a200a7a3f86a59b2ebf03f83602 # 0.17.0 + YAML + end + + private def production_database_url(name : String) : String + case database + when "pg" then "postgres:///#{name}_production?host=/var/run/postgresql" + when "sqlite" then "sqlite3:./db/#{name}_production.db" + else "" + end + end + + private def production_database_note(name : String) : String case database when "pg" - <<-YAML - pg: - github: will/crystal-pg - version: 0.30.0 - YAML - when "mysql" - <<-YAML - mysql: - github: crimson-knight/crystal-mysql - commit: c061324dcef89a200a7a3f86a59b2ebf03f83602 - YAML + "Production connects to Postgres on this machine through its Unix socket\n" \ + "(`#{production_database_url(name)}` in `config/environments/production.yml`).\n" \ + "Set `DATABASE_URL` to use a database on another machine." + when "sqlite" + "SQLite gets an 8 MiB page cache per connection (`cache_size=-8000`). It is set in\n" \ + "`config/database.cr`; a `cache_size` written in the URL takes its place." else - <<-YAML - sqlite3: - github: crystal-lang/crystal-sqlite3 - version: 0.23.0 - YAML + "Set `DATABASE_URL` to the production MySQL server." + end + end + + private def connection_url_source : String + unless database == "sqlite" + return %(connection_url = ENV["DATABASE_URL"]? || Amber.settings.database_url\n\n) end + + <<-SOURCE + # An 8 MiB page cache per connection; a cache_size in the URL takes its place. + connection_url = Grant::Adapter::Sqlite.url_with_pragmas( + ENV["DATABASE_URL"]? || Amber.settings.database_url, + {"cache_size" => "-8000"} + ) + + + SOURCE end private def database_adapter_require : String From ce985be713953f3aa35d3b387ab374e5bb2b81f3 Mon Sep 17 00:00:00 2001 From: crimson-knight Date: Mon, 9 Nov 2026 06:23:44 -0500 Subject: [PATCH 3/4] fix(database): connect migrations through a Postgres socket directory URL --- spec/commands/database_command_spec.cr | 24 ++++++++++++++++++++++++ src/amber_cli/commands/database.cr | 17 ++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 spec/commands/database_command_spec.cr diff --git a/spec/commands/database_command_spec.cr b/spec/commands/database_command_spec.cr new file mode 100644 index 0000000..1b0fbd7 --- /dev/null +++ b/spec/commands/database_command_spec.cr @@ -0,0 +1,24 @@ +require "../spec_helper" + +describe AmberCLI::Commands::DatabaseCommand do + describe ".connection_url_for_migrations" do + it "points the migration driver at the socket file of a socket directory URL" do + url = "postgres:///app_production?host=/var/run/postgresql" + expected = URI.parse("postgres:///app_production?host=#{URI.encode_www_form("/var/run/postgresql/.s.PGSQL.5432")}").to_s + + AmberCLI::Commands::DatabaseCommand.connection_url_for_migrations(url).should eq(expected) + end + + it "leaves a URL that already names the socket file alone" do + url = "postgres:///app?host=/tmp/.s.PGSQL.5432" + AmberCLI::Commands::DatabaseCommand.connection_url_for_migrations(url).should eq(url) + end + + it "leaves non-socket URLs alone" do + tcp_url = "postgres://localhost:5432/app" + sqlite_url = "sqlite3:./db/app.db" + AmberCLI::Commands::DatabaseCommand.connection_url_for_migrations(tcp_url).should eq(tcp_url) + AmberCLI::Commands::DatabaseCommand.connection_url_for_migrations(sqlite_url).should eq(sqlite_url) + end + end +end diff --git a/src/amber_cli/commands/database.cr b/src/amber_cli/commands/database.cr index f41b19f..707a19d 100644 --- a/src/amber_cli/commands/database.cr +++ b/src/amber_cli/commands/database.cr @@ -97,7 +97,7 @@ module AmberCLI::Commands private def process_commands(commands) commands.each do |command| - url = database_url + url = self.class.connection_url_for_migrations(database_url) case command when "drop" Micrate::Cli.drop_database(url) @@ -169,6 +169,21 @@ module AmberCLI::Commands end end + # The crystal-pg 0.29 driver this CLI links takes the socket file as `host`, while a + # generated application's database URL names the socket directory, as libpq does. + def self.connection_url_for_migrations(url : String) : String + return url unless url.starts_with?("postgres") + + parsed = URI.parse(url) + params = parsed.query_params + host = params["host"]? + return url unless host && host.starts_with?('/') && !host.includes?(".s.PGSQL.") + + params["host"] = File.join(host, ".s.PGSQL.#{parsed.port || 5432}") + parsed.query_params = params + parsed.to_s + end + private def database_url : String ENV["DATABASE_URL"]? || ENV["AMBER_DATABASE_URL"]? || environment_database_url || default_database_url end From d648a75f3845aa111324395c90db7027aafbc7c4 Mon Sep 17 00:00:00 2001 From: crimson-knight Date: Mon, 9 Nov 2026 06:23:44 -0500 Subject: [PATCH 4/4] feat(generate): answer scaffold reads through the page cache and clear what writes change --- spec/commands/generate_command_spec.cr | 66 +++++++- src/amber_cli/commands/generate.cr | 208 ++++++++++++++++++++++--- 2 files changed, 249 insertions(+), 25 deletions(-) diff --git a/spec/commands/generate_command_spec.cr b/spec/commands/generate_command_spec.cr index 662d770..8c3d258 100644 --- a/spec/commands/generate_command_spec.cr +++ b/spec/commands/generate_command_spec.cr @@ -139,13 +139,77 @@ describe AmberCLI::Commands::GenerateCommand do AmberCLI::Commands::GenerateCommand.new("generate").parse_and_execute(["scaffold", "Pet", "name:string"]) AmberCLI::Commands::GenerateCommand.new("generate").parse_and_execute(["api", "Toy", "name:string"]) - File.read("src/controllers/pet_controller.cr").should contain("Pet.order(id: :desc).to_a") + File.read("src/controllers/pet_controller.cr").should contain("Pet.order(id: :desc).limit(ROWS_PER_PAGE + 1)") File.read("src/controllers/pet_controller.cr").should_not contain("Pet.all.to_a") File.read("src/controllers/api/toy_controller.cr").should contain("Toy.order(id: :desc).to_a") File.read("src/controllers/api/toy_controller.cr").should_not contain("Toy.all.to_a") end end + it "answers scaffold reads from the page cache and clears exactly what a write changed" do + SpecHelper.within_temp_directory do + File.write(".amber.yml", "template: ecr\ndatabase: sqlite\nmodel: grant\n") + + AmberCLI::Commands::GenerateCommand.new("generate").parse_and_execute(["scaffold", "Pet", "name:string:required", "adopted:bool"]) + + controller = File.read("src/controllers/pet_controller.cr") + controller.should contain(%(page_cache.answer(context, list_page_key(page_number)))) + controller.should contain(%(page_cache.answer(context, show_page_key(record_id)))) + controller.should contain("clear_pages_after_create") + controller.should contain("clear_pages_after_update(pet)") + controller.should contain("clear_pages_after_destroy(pet, list_page_number)") + controller.should contain("clear_pages_numbered_from") + controller.should contain("clear_fragments(fragment_keys_of(pet))") + controller.should contain("per-visitor") + + row = File.read("src/views/pet/_row.ecr") + row.should contain("escape_text(@pet.name)") + row.should contain("escape_text(@pet.adopted)") + File.read("src/views/pet/_details.ecr").should contain("escape_text(@pet.name)") + File.read("src/components/pet_row.cr").should contain("struct PetRow < Components::StatelessView") + File.read("src/components/pet_details.cr").should contain(%("details:pet:\#{@pet.id}")) + + spec = File.read("spec/controllers/pet_controller_spec.cr") + spec.should contain("Components::Cache.configure(&.use_memory_cache)") + spec.should contain("shows the change on the next read of a stored page") + end + end + + it "puts scaffold reads on the lean read routes of a tuned application" do + SpecHelper.within_temp_directory do + File.write(".amber.yml", "template: ecr\ndatabase: sqlite\nmodel: grant\n") + Dir.mkdir_p("config") + File.write("config/routes.cr", <<-CRYSTAL) + Amber::Server.configure do + routes :lean_read do + end + + routes :web do + end + end + CRYSTAL + + AmberCLI::Commands::GenerateCommand.new("generate").parse_and_execute(["scaffold", "Pet", "name:string"]) + + routes = File.read("config/routes.cr") + routes.should contain(%(get "/pets", PetController, :index)) + routes.should contain(%(get "/pets/:id", PetController, :show, constraints: {"id" => :numeric})) + routes.should contain(%(resources "/pets", PetController, except: [:index, :show])) + routes.index!(%(get "/pets", PetController)).should be < routes.index!("routes :web do") + end + end + + it "indexes the foreign key column of a reference field" do + SpecHelper.within_temp_directory do + File.write(".amber.yml", "template: ecr\ndatabase: sqlite\nmodel: grant\n") + + AmberCLI::Commands::GenerateCommand.new("generate").parse_and_execute(["scaffold", "Comment", "post:reference"]) + + migration = File.read(Dir.glob("db/migrations/*_create_comments.sql").first) + migration.should contain("CREATE INDEX IF NOT EXISTS index_comments_on_post_id ON comments (post_id);") + end + end + it "generates a belongs_to and a foreign key for reference fields" do SpecHelper.within_temp_directory do File.write(".amber.yml", "template: ecr\ndatabase: sqlite\nmodel: grant\n") diff --git a/src/amber_cli/commands/generate.cr b/src/amber_cli/commands/generate.cr index 4b6f742..8efcdff 100644 --- a/src/amber_cli/commands/generate.cr +++ b/src/amber_cli/commands/generate.cr @@ -79,6 +79,9 @@ module AmberCLI::Commands DEFAULT_AUTH_MODEL_NAME = "User" + WEB_ROUTES_ANCHOR = " routes :web do" + LEAN_READ_ROUTES_ANCHOR = " routes :lean_read do" + getter generator_type : String = "" getter name : String = "" getter fields : Array(Tuple(String, String)) = [] of Tuple(String, String) @@ -854,6 +857,7 @@ SPEC generate_scaffold_schema generate_controller_for_scaffold generate_views + generate_scaffold_components add_resource_route success "Scaffold #{class_name} generated successfully!" @@ -917,23 +921,55 @@ class #{controller_name} < ApplicationController schema :create, #{class_name}Schema schema :update, #{class_name}Schema - @#{plural_variable_name} = [] of #{class_name} + # Rows on one page of the list. Pagination keeps a page's cost and size bounded. + ROWS_PER_PAGE = 25 + + # A larger page number reads as the first page, so a query cannot ask for a huge offset. + MAXIMUM_PAGE_NUMBER = 1_000_000 + + LIST_PAGE_KEY_PREFIX = "#{plural_name}:page:" + + @#{variable_name}_rows = [] of String + @#{variable_name}_details = "" + @page_number = 1 + @has_next_page = false @#{variable_name} = #{class_name}.new @errors = [] of Amber::Schema::Error + # Everything built inside `Components::Cache.page_cache.answer` is the same for every + # visitor, and the page cache holds exactly that. To mark a page per-visitor (a name, + # a flash message, a form with a CSRF token), move its route to `routes :web` in + # config/routes.cr and replace its `answer` block with a plain `render`. Nothing is + # stored for that page then. def index - @#{plural_variable_name} = #{class_name}.order(id: :desc).to_a - render("index.#{template_ext}") + page_number = requested_page_number + Components::Cache.page_cache.answer(context, list_page_key(page_number)) do |build| + first_row = (page_number - 1) * ROWS_PER_PAGE + list_of_#{plural_variable_name} = #{class_name}.order(id: :desc).limit(ROWS_PER_PAGE + 1).offset(first_row).to_a + @page_number = page_number + @has_next_page = list_of_#{plural_variable_name}.size > ROWS_PER_PAGE + @#{variable_name}_rows = list_of_#{plural_variable_name}.first(ROWS_PER_PAGE).map do |#{variable_name}| + row = #{class_name}Row.new(#{variable_name}) + build.fragment(row.cache_key) { row.to_s } + end + render("index.#{template_ext}") + end end def show - if #{variable_name} = #{class_name}.find(params[:id]) + record_id = params[:id].to_i64? + return redirect_to("/#{plural_name}") unless record_id + + Components::Cache.page_cache.answer(context, show_page_key(record_id)) do |build| + #{variable_name} = #{class_name}.find!(record_id) @#{variable_name} = #{variable_name} + details = #{class_name}Details.new(#{variable_name}) + @#{variable_name}_details = build.fragment(details.cache_key) { details.to_s } render("show.#{template_ext}") - else - flash[:danger] = "#{class_name} not found" - redirect_to "/#{plural_name}" end + rescue Grant::Querying::NotFound + # This route has no session, so there is no flash message to carry. + redirect_to("/#{plural_name}") end def new @@ -947,6 +983,7 @@ class #{controller_name} < ApplicationController #{schema_field_assignments} if #{variable_name}.save + clear_pages_after_create flash[:success] = "#{class_name} created successfully" redirect_to "/#{plural_name}/\#{#{variable_name}.id}" else @@ -972,6 +1009,7 @@ class #{controller_name} < ApplicationController #{update_field_assignments} if #{variable_name}.save + clear_pages_after_update(#{variable_name}) flash[:success] = "#{class_name} updated successfully" redirect_to "/#{plural_name}/\#{#{variable_name}.id}" else @@ -987,7 +1025,9 @@ class #{controller_name} < ApplicationController def destroy if #{variable_name} = #{class_name}.find(params[:id]) + list_page_number = list_page_number_of(#{variable_name}) #{variable_name}.destroy + clear_pages_after_destroy(#{variable_name}, list_page_number) flash[:success] = "#{class_name} deleted successfully" else flash[:danger] = "#{class_name} not found" @@ -1021,10 +1061,70 @@ class #{controller_name} < ApplicationController super end end + + # A new row moves every row below it, so every list page changes. + private def clear_pages_after_create : Nil + Components::Cache.page_cache.clear_pages_numbered_from(LIST_PAGE_KEY_PREFIX, 1) + end + + # An edited row changes its own page and one row of one list page. + private def clear_pages_after_update(#{variable_name} : #{class_name}) : Nil + page_cache = Components::Cache.page_cache + page_cache.clear_fragments(fragment_keys_of(#{variable_name})) + page_cache.clear_pages([show_page_key(#{variable_name}.id), list_page_key(list_page_number_of(#{variable_name}))]) + end + + # The rows after a deleted row move up a page, and the page before it may lose its next link. + private def clear_pages_after_destroy(#{variable_name} : #{class_name}, list_page_number : Int32) : Nil + page_cache = Components::Cache.page_cache + page_cache.clear_fragments(fragment_keys_of(#{variable_name})) + page_cache.clear_pages([show_page_key(#{variable_name}.id)]) + page_cache.clear_pages_numbered_from(LIST_PAGE_KEY_PREFIX, Math.max(list_page_number - 1, 1)) + end + + private def fragment_keys_of(#{variable_name} : #{class_name}) : Array(String) + [#{class_name}Row.new(#{variable_name}).cache_key, #{class_name}Details.new(#{variable_name}).cache_key] + end + + private def show_page_key(record_id : Int64?) : String + "#{variable_name}:\#{record_id}" + end + + private def list_page_key(page_number : Int32) : String + "\#{LIST_PAGE_KEY_PREFIX}\#{page_number}" + end + + # Rows are newest first, so the rows newer than this one set which page it sits on. + private def list_page_number_of(#{variable_name} : #{class_name}) : Int32 + newer_rows = #{class_name}.where(:id, :gt, #{variable_name}.id).size + (newer_rows // ROWS_PER_PAGE).to_i32 + 1 + end + + private def requested_page_number : Int32 + page_number = request.query_params["page"]?.to_s.to_i? || 1 + page_number.in?(1..MAXIMUM_PAGE_NUMBER) ? page_number : 1 + end end CONTROLLER end + # An update must show on the next read of a page that was already stored. + private def cached_page_update_spec(changed_field : Tuple(String, String)?, updated_hash : String) : String + return "" unless changed_field + + <<-SPEC + + it "shows the change on the next read of a stored page" do + saved = create_sample_#{variable_name} + get("/#{plural_name}/\#{saved.id}") + get("/#{plural_name}/\#{saved.id}") + headers = csrf_headers("/#{plural_name}/\#{saved.id}/edit") + put("/#{plural_name}/\#{saved.id}", body: HTTP::Params.encode(#{updated_hash}), headers: headers) + get("/#{plural_name}/\#{saved.id}").body.should contain("Updated") + end +SPEC + end + private def scaffold_spec_template changed_field = fields.find { |_, type| %w[string text email].includes?(type) } sample_hash = form_hash_literal @@ -1057,7 +1157,9 @@ end describe #{controller_name} do before_each do #{class_name}.clear -#{parent_clear_source} end +#{parent_clear_source} # These specs save records directly, so the page cache needs clearing by hand. + Components::Cache.configure(&.use_memory_cache) + end describe "GET /#{plural_name}" do it "responds successfully" do @@ -1119,6 +1221,7 @@ describe #{controller_name} do assert_response_redirect(response) #{update_check} end +#{cached_page_update_spec(changed_field, updated_hash)} end describe "DELETE /#{plural_name}/:id" do @@ -1134,6 +1237,51 @@ end SPEC end + # Row and detail views are stateless views, so record values leave them escaped. + private def generate_scaffold_components + create_file("src/components/#{file_name}_row.cr", scaffold_component_source("Row", "_row", "row")) + create_file("src/components/#{file_name}_details.cr", scaffold_component_source("Details", "_details", "details")) + + create_file("src/views/#{file_name}/_row.ecr", row_view_source) + create_file("src/views/#{file_name}/_details.ecr", details_view_source) + end + + private def scaffold_component_source(suffix : String, view_name : String, key_prefix : String) : String + <<-COMPONENT +struct #{class_name}#{suffix} < Components::StatelessView + def initialize(@#{variable_name} : #{class_name}) + end + + def cache_key : String + "#{key_prefix}:#{variable_name}:\#{@#{variable_name}.id}" + end + + ECR.def_to_s "src/views/#{file_name}/#{view_name}.ecr" +end +COMPONENT + end + + private def row_view_source : String + <<-VIEW + + <%= @#{variable_name}.id %> +#{fields.map { |field_name, _| " <%= escape_text(@#{variable_name}.#{field_name}) %>" }.join("\n")} + + Show + Edit + + +VIEW + end + + private def details_view_source : String + <<-VIEW +
+#{fields.map { |field_name, _| "
#{field_name.camelcase}
\n
<%= escape_text(@#{variable_name}.#{field_name}) %>
" }.join("\n")} +
+VIEW + end + private def generate_views views_dir = "src/views/#{file_name}" @@ -1761,6 +1909,9 @@ SPEC end.join(",\n") field_sql = column_definitions.empty? ? "" : "#{column_definitions},\n" + foreign_key_indexes = fields.select { |_, field_type| field_type == "reference" }.join do |field_name, _| + "CREATE INDEX IF NOT EXISTS index_#{table_name}_on_#{field_name} ON #{table_name} (#{field_name});\n" + end <<-SQL -- +micrate Up @@ -1770,7 +1921,7 @@ CREATE TABLE IF NOT EXISTS #{table_name} ( #{field_sql} created_at TIMESTAMP, updated_at TIMESTAMP ); - +#{foreign_key_indexes} -- +micrate Down DROP TABLE IF EXISTS #{table_name}; SQL @@ -1817,18 +1968,20 @@ VIEW - <% @#{plural_variable_name}.each do |#{variable_name}| %> - - <%= #{variable_name}.id %> -#{fields.map { |f, _| " <%= #{variable_name}.#{f} %>" }.join("\n")} - - Show - Edit - - + <% @#{variable_name}_rows.each do |row| %> + <%= row %> <% end %> + + VIEW end end @@ -1848,9 +2001,7 @@ VIEW <<-VIEW

#{class_name}

-
-#{fields.map { |f, _| "
#{f.camelcase}
\n
<%= @#{variable_name}.#{f} %>
" }.join("\n")} -
+<%= @#{variable_name}_details %> Back Edit @@ -2198,8 +2349,18 @@ SUPPORT end end + # Public reads go on the lean pipeline; everything else keeps the session pipes. private def add_resource_route - add_routes([" resources \"/#{plural_name}\", #{controller_name}"]) + routes_path = "config/routes.cr" + if File.exists?(routes_path) && File.read(routes_path).includes?(LEAN_READ_ROUTES_ANCHOR) + add_routes([ + " get \"/#{plural_name}\", #{controller_name}, :index", + " get \"/#{plural_name}/:id\", #{controller_name}, :show, constraints: {\"id\" => :numeric}", + ], LEAN_READ_ROUTES_ANCHOR) + add_routes([" resources \"/#{plural_name}\", #{controller_name}, except: [:index, :show]"]) + else + add_routes([" resources \"/#{plural_name}\", #{controller_name}"]) + end end private def add_api_route @@ -2234,7 +2395,7 @@ SUPPORT end end - private def add_routes(route_lines : Array(String)) + private def add_routes(route_lines : Array(String), anchor : String = WEB_ROUTES_ANCHOR) routes_path = "config/routes.cr" unless File.exists?(routes_path) warning "Could not add routes because #{routes_path} does not exist." @@ -2242,7 +2403,6 @@ SUPPORT end content = File.read(routes_path) - anchor = " routes :web do" unless content.includes?(anchor) warning "Could not find the web routes block in #{routes_path}." return