jets-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aa38672c783a5787e052ab8be240f86328ec9229a7d7c6a4070373214702fa3d
4
+ data.tar.gz: df5c21cc068c6779b29842dd5bfaf3b5cde6c4e6b1f4da41b58f3bbc791de6d4
5
+ SHA512:
6
+ metadata.gz: 90967fdc3032a772512e93723d28e7e806de7d766e704f70563da9df09b82900ead17cec86b336813031e944c53c2fe24d0c0e67ac273a9ad6574e8c687545c5
7
+ data.tar.gz: 14d2ae683f2e5b4e238928163b9b254ec72bad8fbfe010963635d8cf494a61347b5217fc6b78751aaf1cc09ff3295eb6cef192362e37b8dd5e1fd666b18ca5a1
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Tung Nguyen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # Jets Rails for Mega Mode
2
+
3
+ This gem works in conjuction with Jets to allow you to run a Rails application on AWS Lambda. This gem is not meant to be used standalone. Instead, jets automatically adds this gem to a Rails application as part of [Mega Mode Rails Support](http://rubyonjets.com/docs/rails-support/).
4
+
5
+ ## Usage
6
+
7
+ This gem is not meant to be used standalone. You can enable it though by adding to your Rails Gemfile and setting the `JETS_MEGAMODE=1` env var.
8
+
9
+ ## More Info
10
+
11
+ For more information about Jets and Rails Support refer to:
12
+
13
+ * [Ruby on Jets](http://rubyonjets.com)
14
+ * [AWS Lambda Ruby Support at Native Speed with Jets](https://blog.boltops.com/2018/09/02/aws-lambda-ruby-support-at-native-speed-with-jets)
15
+ * [Jets Rails Support](http://rubyonjets.com/docs/rails-support/)
16
+ * [Toronto Serverless Presentation: Jets Framework on AWS Lambda](https://blog.boltops.com/2018/09/25/toronto-serverless-presentation-jets-framework-on-aws-lambda)
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'JetsRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
data/lib/jets-rails.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative "jets_rails"
data/lib/jets_rails.rb ADDED
@@ -0,0 +1,24 @@
1
+ # JETS_MEGAMODE is set in the bin/rackup wrapper
2
+ # We do this check so we can include jets-rails in the Gemfile and allow users
3
+ # to check to see if the bundle install will work though this is not activated
4
+ # logically without JETS_MEGAMODE set.
5
+ return unless ENV['JETS_MEGAMODE']
6
+
7
+ # = Configuration
8
+ #
9
+ # Configure with Rails initializer. Example:
10
+ #
11
+ # config/initializer/jets.rb:
12
+ #
13
+ # JetsRails.stage = ENV['JETS_STAGE'] || 'dev'
14
+ #
15
+ module JetsRails
16
+ cattr_accessor :stage
17
+ self.stage = "dev" # default. should be set to in Rails initializer
18
+
19
+ autoload :StageMiddleware, 'jets_rails/stage_middleware'
20
+ end
21
+
22
+ require "jets_rails/core_ext/kernel"
23
+ require "jets_rails/logger" # eager load rails override
24
+ require "jets_rails/railtie"
@@ -0,0 +1,35 @@
1
+ # With this implementation we write to disk immedaitely. This simplifies the
2
+ # implementation because we do not have to flush to io buffer by passing signals
3
+ # back and forth from the jets ruby server process to the rack server process.
4
+ module Kernel
5
+ # List from https://ruby-doc.org/core-2.5.1/Kernel.html
6
+ # Note, will lose pp format in the @io_buffer but looks like a lot of work to keep the pp format.
7
+ # Must override stdout which can be messy quick: https://www.ruby-forum.com/topic/43725
8
+ OVERRIDE_METHODS = %w[
9
+ p
10
+ pp
11
+ print
12
+ printf
13
+ putc
14
+ puts
15
+ ]
16
+ # NOTE adding sprintf produces #<%s: %s:%s/%s> with puma? So not including sprintf
17
+ OVERRIDE_METHODS.each do |meth|
18
+ # Example of generated code:
19
+ #
20
+ # alias_method :original_puts, :puts
21
+ # def puts(*args, &block)
22
+ # original_puts(*args, &block)
23
+ # end
24
+ #
25
+ class_eval <<~CODE
26
+ alias_method :original_#{meth}, :#{meth}
27
+ def #{meth}(*args, &block)
28
+ # Write immediately for debugging
29
+ message = "Rails: " + args.first.to_s + "\n"
30
+ IO.write("/tmp/jets-output.log", message, mode: 'a')
31
+ original_#{meth}(*args, &block)
32
+ end
33
+ CODE
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_support'
2
+
3
+ # Tap into rails logging to show logs in CloudWatch eventually.
4
+ #
5
+ # Overriding Rails SimpleFormatter directly is pretty simple approach.
6
+ # The definition is short.
7
+ class ActiveSupport::Logger::SimpleFormatter
8
+ # This method is invoked when a log event occurs
9
+ def call(severity, timestamp, progname, msg)
10
+ result = "#{String === msg ? msg : msg.inspect}\n"
11
+ IO.write("/tmp/jets-output.log", "Rails: #{result}", mode: 'a')
12
+ result
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module JetsRails
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "jets_rails.configure_middleware" do
4
+ Rails.application.middleware.insert_before(Rack::Sendfile, StageMiddleware)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ module JetsRails
2
+ class StageMiddleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ add_stage_name(env) if on_aws?(env)
9
+ status, headers, body = @app.call(env)
10
+ [status, headers, body]
11
+ end
12
+
13
+ private
14
+ # Add API Gateway Stage Name
15
+ def add_stage_name(env)
16
+ # changes links that Rails generates
17
+ env[Rack::SCRIPT_NAME] = "/#{JetsRails.stage}"
18
+ end
19
+
20
+ def on_aws?(env)
21
+ return true if ENV['JETS_ON_AWS'] # for local testing
22
+ host = env['HTTP_X_FORWARDED_HOST'] # from Jets::Rack::Request#set_headers!
23
+ host&.include?("amazonaws.com")
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module JetsRails
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :jets_rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jets-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tung Nguyen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-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: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Reconfigures the Rails application and injects the necessary changes
42
+ to run the rack application on AWS Lambda. Meant to be used in conjunction with
43
+ Jets. Not meant to be used standalone.
44
+ email:
45
+ - tongueroo@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - MIT-LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - lib/jets-rails.rb
54
+ - lib/jets_rails.rb
55
+ - lib/jets_rails/core_ext/kernel.rb
56
+ - lib/jets_rails/logger.rb
57
+ - lib/jets_rails/railtie.rb
58
+ - lib/jets_rails/stage_middleware.rb
59
+ - lib/jets_rails/version.rb
60
+ - lib/tasks/jets_rails_tasks.rake
61
+ homepage: https://github.com/tongueroo/jets-rails
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.7.6
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Jets Mega Mode Rails Support
85
+ test_files: []