Fly.io multi-region postgres support for crystal.
-
Add the dependency to your
shard.yml
:dependencies: fly: github: superfly/fly.cr version: ~> 0.1
-
Run
shards install
-
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_handler
andfly/avram
after avram# src/shards.cr # ... 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 yourFly
configuration. -
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
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["PRIMARY_REGION"] # Can be any string
end
Deploy a Lucky app using the fly.io Run a Crystal App guide.
It's a bit of a pain because deploys take 3-5min, but it's the best development option as it provides integration testing.
- Fork it (https://github.com/superfly/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
- Alex Piechowski - creator and maintainer