fly-atc 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 84eacfefce4abf1b07ac2dd417b1cc98027d64241af482c353bbb99f8c89b16f
4
- data.tar.gz: 34a3bea58d8b09bd99b4da8633d1b8252b967dd0b588ae1dfc41a1a0ca2015ed
3
+ metadata.gz: 84df5f5fc4b0f255472486726d523a894466b53ea70b02eba40bdcd7ec923566
4
+ data.tar.gz: de42c80a9fdf476eab9d417f591d213b7ad6cfe11187e646542ac0c87c0e41bc
5
5
  SHA512:
6
- metadata.gz: '09c2649081775270809efec3abe745b8514b4ecd450b10e587f231d3f4cacdae1bc05c96c5adbdc3a286e23b70b2587fa205fa4e3bf7e30d7ac3367a553cde07'
7
- data.tar.gz: 1c97f8b362c03cff6b26f94a250cb3b5fd332adb82506218da54f8f4a8c139940348dc7a2096941ce8327e23156f6dda8d3a90ee821517a0b8a4a5f7175e4bc5
6
+ metadata.gz: 928a5034c7cd7baf41c4f482aa83ba0e855dc615ae1f357b4a899654e48b937876cd489f43bb23641af20943c4fa79ae9f7e691ab08a593cadf6c575c8c345d5
7
+ data.tar.gz: 281627f56d61c7b099625829bd69509a3cbc06b76237defd6a71eadc9a851aa25e5b965a91b74f720a2d2cce32d30c3276252a25469d95dd49ce591aa3600a84
data/README.md CHANGED
@@ -6,15 +6,23 @@ A SaaS toolkit for converting a personal application into a efficient, siloed, m
6
6
 
7
7
  ## Usage
8
8
 
9
- This is all TBD at this point, but for Rails projects it is likely to go something like this:
9
+ ### Quickstart (single tenant):
10
10
 
11
- * Replace thruster with fly-atc in Gemfile and Dockerfile
12
- * Define your tenants in a config file, probably YAML.
11
+ ```
12
+ bundle add fly-atc
13
+ bundle binstubs fly-atc
14
+ ```
13
15
 
14
- For non-Rails projects, the process is likely going to be similar:
16
+ Replace `thruster` with `fly-atc` in Dockerfile.
15
17
 
16
- * Follow the instructions for using thruster with your framework, but substitute fly-atc for thruster.
17
- * Define your tenants in a config file, probably YAML but JSON could also be supported.
18
+ ### Quickstart (multi-tenant):
19
+
20
+ ```
21
+ bundle add fly-atc
22
+ bin/rails generate atc
23
+ ```
24
+
25
+ Edit `config/atc.yml` as needed.
18
26
 
19
27
  Fly.io's dockerfile generators will be able to help with this.
20
28
 
@@ -1,3 +1,3 @@
1
1
  module FlyAtc
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/fly-atc.rb CHANGED
@@ -2,3 +2,10 @@ module FlyAtc
2
2
  end
3
3
 
4
4
  require_relative "fly-atc/version"
5
+ require_relative "helpers/atc-cable"
6
+
7
+ class FlyAtcRailtie < Rails::Railtie
8
+ rake_tasks do
9
+ Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
10
+ end
11
+ end
@@ -0,0 +1,44 @@
1
+ class AtcGenerator < Rails::Generators::Base
2
+ def generate_app
3
+ source_paths.push File.expand_path("./templates", __dir__)
4
+
5
+ ### config/routes.rb
6
+
7
+ @routes = IO.read("config/routes.rb")
8
+
9
+ unless @routes.include? "fly_atc_scope"
10
+ _, prolog, routes = routes.split(/(.*Rails.application.routes.draw do\n)/m,2)
11
+ routes, epilog, _ = @routes.split(/^(end.*)/m,2)
12
+ routes = routes.split(/\n\s*\n/)
13
+ scoped = routes.select {|route| route =~ /^\s*\w/ && !route.include?('as:')}
14
+
15
+ @routes = <<~EOF
16
+ #{prolog.rstrip}
17
+ fly_atc_scope = ENV.fetch("FLY_ATC_SCOPE", "")
18
+
19
+ unless fly_atc_scope == ""
20
+ mount ActionCable.server => "/\#{fly_atc_scope}/cable"
21
+ end
22
+
23
+ scope fly_atc_scope do
24
+ #{scoped.join("\n\n").gsub(/^ /, " ")}
25
+ end
26
+
27
+ #{(routes-scoped).join("\n\n").rstrip}
28
+ #{epilog.rstrip}
29
+ EOF
30
+ end
31
+
32
+ template "routes.erb", "config/routes.rb"
33
+
34
+ ### app/views/layouts/application.html.erb
35
+
36
+ @layout = IO.read("app/views/layouts/application.html.erb")
37
+
38
+ unless @layout.include? "action_cable_meta_tag_dynamic"
39
+ @layout[/<meta.*?\n()\r?\n/m, 1] = " <%= action_cable_meta_tag_dynamic %>\n"
40
+ end
41
+
42
+ template "application.html.erb", "app/views/layouts/application.html.erb"
43
+ end
44
+ end
@@ -0,0 +1 @@
1
+ <%= @layout -%>
@@ -0,0 +1 @@
1
+ <%= @routes -%>
@@ -0,0 +1,17 @@
1
+ module ApplicationHelper
2
+ def action_cable_meta_tag_dynamic
3
+ scheme = (request.env['HTTP_X_FORWARDED_PROTO'] || request.env["rack.url_scheme"] || '').split(',').last
4
+ return '' if scheme.blank?
5
+ host = request.env['HTTP_X_FORWARDED_HOST'] || request.env["HTTP_HOST"]
6
+ scope = ENV.fetch('FLY_ATC_SCOPE', "")
7
+ root = request.env['RAILS_RELATIVE_URL_ROOT']
8
+
9
+ if scope != ""
10
+ websocket = "#{scheme.sub('http', 'ws')}://#{host}#{root}#{scope}/cable"
11
+ else
12
+ websocket = "#{scheme.sub('http', 'ws')}://#{host}#{root}/cable"
13
+ end
14
+
15
+ "<meta name=\"action-cable-url\" content=\"#{websocket}\" />".html_safe
16
+ end
17
+ end
@@ -0,0 +1,49 @@
1
+ actions = Rake::Task["db:prepare"].actions.clone
2
+
3
+ namespace :db do
4
+ task :atc_prepare => "db:load_config" do
5
+ actions.each {|action| action.call}
6
+ end
7
+ end
8
+
9
+ Rake::Task["db:prepare"].clear
10
+
11
+ namespace :litestream do
12
+ task :atc_config => "db:load_config" do
13
+ require 'erubi'
14
+
15
+ @dbs =
16
+ ActiveRecord::Base
17
+ .configurations
18
+ .configs_for(env_name: "production", include_hidden: true)
19
+ .select { |config| ["sqlite3", "litedb"].include? config.adapter }
20
+ .map(&:database)
21
+
22
+ @config = ENV["LITESTREAM_CONFIG"] || Rails.root.join("config/litestream.yml")
23
+
24
+ template = File.read(File.join(File.dirname(__FILE__), "templates/litestream.yml.erb"))
25
+ result = eval(Erubi::Engine.new(template).src)
26
+
27
+ unless File.exist?(@config) && File.read(@config) == result
28
+ File.write(@config, result)
29
+ end
30
+ end
31
+
32
+ task :atc_restore => "litestream:atc_config" do
33
+ next unless ENV["BUCKET_NAME"]
34
+
35
+ @dbs.each do |database|
36
+ next if File.exist? database
37
+ sh "bundle exec litestream restore -config #{@config} -if-replica-exists #{database}"
38
+ end
39
+ end
40
+
41
+ task :atc_replicate => "litestream:atc_config" do
42
+ next unless ENV["BUCKET_NAME"]
43
+ sh "bundle exec litestream replicate -config #{@config}"
44
+ end
45
+ end
46
+
47
+ namespace :atc do
48
+ task :prepare => ["litestream:atc_restore", "db:atc_prepare"]
49
+ end
@@ -0,0 +1,14 @@
1
+ # This is the configuration file for litestream.
2
+ #
3
+ # For more details, see: https://litestream.io/reference/config/
4
+ #
5
+ dbs:
6
+ <% for db in @dbs -%>
7
+ - path: <%= db %>
8
+ replicas:
9
+ - type: s3
10
+ bucket: $BUCKET_NAME
11
+ path: storage/<%= File.basename(db) %>
12
+ access-key-id: $AWS_ACCESS_KEY_ID
13
+ secret-access-key: $AWS_SECRET_ACCESS_KEY
14
+ <% end -%>
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fly-atc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-20 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: litestream
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.10'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0.10'
13
41
  description: An HTTP/2 proxy for mutli-tenant production deployments
14
42
  email: rubys@intertwingly.net
15
43
  executables:
@@ -22,6 +50,12 @@ files:
22
50
  - exe/fly-atc
23
51
  - lib/fly-atc.rb
24
52
  - lib/fly-atc/version.rb
53
+ - lib/generators/atc_generator.rb
54
+ - lib/generators/templates/application.html.erb
55
+ - lib/generators/templates/routes.erb
56
+ - lib/helpers/atc-cable.rb
57
+ - lib/tasks/atc.rake
58
+ - lib/tasks/templates/litestream.yml.erb
25
59
  homepage: https://github.com/rubys/fly-atc
26
60
  licenses:
27
61
  - MIT