Class: Jets::Application
- Inherits:
-
Object
- Object
- Jets::Application
- Extended by:
- Memoist
- Includes:
- Middleware
- Defined in:
- lib/jets/application.rb
Defined Under Namespace
Modules: Middleware
Constant Summary collapse
- ENV_MAP =
Use the shorter name in stack names, but use the full name when it comes to checking for the env.
Example:
Jets.env: 'development' Jets.config.project_namespace: 'demo-dev'
{ development: 'dev', production: 'prod', staging: 'stag', }
Instance Method Summary collapse
- #aws ⇒ Object
- #config ⇒ Object
- #configure(&block) ⇒ Object
- #internal_autoload_paths ⇒ Object
- #load_configs ⇒ Object
- #load_db_config ⇒ Object
- #load_routes ⇒ Object
-
#normalize_env_vars! ⇒ Object
It is pretty easy to attempt to set environment variables without the correct AWS Environment.Variables path struture.
-
#routes ⇒ Object
Naming it routes because config/routes.rb requires.
- #set_aliases! ⇒ Object
- #setup! ⇒ Object
- #setup_auto_load_paths ⇒ Object
Methods included from Middleware
Instance Method Details
#config ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jets/application.rb', line 19 def config config = ActiveSupport::OrderedOptions.new config.prewarm = ActiveSupport::OrderedOptions.new config.prewarm.enable = true config.prewarm.concurrency = 2 config.prewarm.rate = '30 minutes' config.lambdagems = ActiveSupport::OrderedOptions.new config.lambdagems.sources = [ 'https://gems.lambdagems.com' ] config end |
#configure(&block) ⇒ Object
9 10 11 |
# File 'lib/jets/application.rb', line 9 def configure(&block) instance_eval(&block) if block end |
#internal_autoload_paths ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/jets/application.rb', line 44 def internal_autoload_paths internal = File.("../internal", __FILE__) paths = %w[ app/controllers app/models app/jobs ] paths.map { |path| "#{internal}/#{path}" } end |
#load_configs ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/jets/application.rb', line 54 def load_configs # The Jets default/application.rb is loaded. require File.("../default/application.rb", __FILE__) # Then project config/application.rb is loaded. app_config = "#{Jets.root}config/application.rb" require app_config if File.exist?(app_config) # Normalize config and setup some shortcuts set_aliases! normalize_env_vars! load_db_config end |
#load_db_config ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/jets/application.rb', line 113 def load_db_config config.database = {} database_yml = "#{Jets.root}config/database.yml" if File.exist?(database_yml) text = Jets::Erb.result(database_yml) db_config = YAML.load(text) config.database = db_config end end |
#load_routes ⇒ Object
133 134 135 136 |
# File 'lib/jets/application.rb', line 133 def load_routes routes_file = "#{Jets.root}config/routes.rb" require routes_file if File.exist?(routes_file) end |
#normalize_env_vars! ⇒ Object
It is pretty easy to attempt to set environment variables without the correct AWS Environment.Variables path struture. Auto-fix it for convenience.
104 105 106 107 108 109 110 111 |
# File 'lib/jets/application.rb', line 104 def normalize_env_vars! environment = config.function.environment if environment and !environment.to_h.key?(:variables) config.function.environment = { variables: environment.to_h } end end |
#routes ⇒ Object
Naming it routes because config/routes.rb requires
Jets.application.routes.draw do
for scaffolding to work.
129 130 131 |
# File 'lib/jets/application.rb', line 129 def routes @router ||= Jets::Router.new end |
#set_aliases! ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/jets/application.rb', line 78 def set_aliases! # env_extra can be also be set with JETS_ENV_EXTRA. # JETS_ENV_EXTRA higher precedence than config.env_extra config.env_extra = ENV['JETS_ENV_EXTRA'] if ENV['JETS_ENV_EXTRA'] # IE: With env_extra: project-dev-1 # Without env_extra: project-dev config.short_env = ENV_MAP[Jets.env.to_sym] || Jets.env # table_namespace does not have the env_extra, more common case desired. config.table_namespace = [config.project_name, config.short_env].compact.join('-') project_namespace = [config.project_name, config.short_env, config.env_extra].compact.join('-') config.project_namespace = project_namespace # Must set defaul t iam_policy here instead of `def config` because we need access to # the project_namespace and if we call it from `def config` we get an infinit loop config.iam_policy = [{ sid: "Statement1", action: ["logs:*"], effect: "Allow", resource: "arn:aws:logs:#{Jets.aws.region}:#{Jets.aws.account}:log-group:#{project_namespace}-*", }] end |
#setup! ⇒ Object
13 14 15 16 17 |
# File 'lib/jets/application.rb', line 13 def setup! load_configs # load config object so following methods can use it setup_auto_load_paths load_routes end |
#setup_auto_load_paths ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/jets/application.rb', line 36 def setup_auto_load_paths autoload_paths = config.autoload_paths + config.extra_autoload_paths autoload_paths = autoload_paths.uniq.map { |p| "#{Jets.root}#{p}" } # internal_autoload_paths are last autoload_paths += internal_autoload_paths ActiveSupport::Dependencies.autoload_paths += autoload_paths end |