github.com/superfly/fly.cr
0.1.0 / published Feb 13, 2022 / repository
Crystal HTTP middleware to handle fly.io multi-region postgres replays.
fly.cr
Fly.io multi-region postgres support for crystal.
Installation
-
Add the dependency to your
shard.yml:dependencies: fly: github: grepsedawk/fly.cr version: ~> 0.1 -
Run
shards install
Usage
Postgres via Avram
-
Add Multi-region postgres regional database readers and multi-region web application deployments via fly.io
-
Configure primary (writer) region by setting ENV
$PRIMARY_REGION -
Require
fly/pg/error_handlerandfly/avramafter avram# ... require "avram" # ... require "fly/pg/error_handler" require "fly/avram" # ...This will add patching into avram in order to automatically change the port to read only port (5433) when the
ENV["FLY_REGION"], set automatically by fly.io, based on yourFlyconfiguration. -
Add middleware to your HTTP stack. This supports any core Crystal HTTP server. The middleware must appear after any Error Handler middleware, such as the
Lucky::ErrorHandler.For Lucky, this looks like this:
def middleware : Array(HTTP::Handler) [ # ... Lucky::ErrorHandler.new(action: Errors::Show), Raven::Lucky::ErrorHandler.new, Fly::PG::ErrorHandler.new, # ..,. ] of HTTP::Handler end
Configuration (Optional)
To override the use of ENV['PRIMARY_REGION'], you can set the Fly primary region
by writing a fly config:
# config/fly.cr
Fly.configure do |settings|
settings.primary_region = ENV["WRITER_REGION"] # Can be any string
end
Development
For now, make a Fly.io deploy using lucky_jumpstart and use that to test.
It's a bit of a pain because deploys take 3-5min, but it's the best development option as it provides integration testing.
Contributing
- Fork it (https://github.com/grepsedawk/fly-crystal/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- Alex Piechowski - creator and maintainer