Class: Jets::Application

Inherits:
Object
  • Object
show all
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

Methods included from Middleware

#assemble_app, #call

Instance Method Details

#awsObject



139
140
141
# File 'lib/jets/application.rb', line 139

def aws
  Jets::AwsInfo.new
end

#configObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jets/application.rb', line 19

def config
  config = ActiveSupport::OrderedOptions.new

  config.prewarm = ActiveSupport::OrderedOptions.new
  config.prewarm.enable = true
  config.prewarm.rate = '2 hours'
  config.prewarm.concurrency = 2
  config.prewarm.public_ratio = 10

  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_pathsObject



45
46
47
48
49
50
51
52
53
# File 'lib/jets/application.rb', line 45

def internal_autoload_paths
  internal = File.expand_path("../internal", __FILE__)
  paths = %w[
    app/controllers
    app/models
    app/jobs
  ]
  paths.map { |path| "#{internal}/#{path}" }
end

#load_configsObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jets/application.rb', line 55

def load_configs
  # The Jets default/application.rb is loaded.
  require File.expand_path("../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_configObject



114
115
116
117
118
119
120
121
122
123
# File 'lib/jets/application.rb', line 114

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_routesObject



134
135
136
137
# File 'lib/jets/application.rb', line 134

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.



105
106
107
108
109
110
111
112
# File 'lib/jets/application.rb', line 105

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

#routesObject

Naming it routes because config/routes.rb requires

Jets.application.routes.draw do

for scaffolding to work.



130
131
132
# File 'lib/jets/application.rb', line 130

def routes
  @router ||= Jets::Router.new
end

#set_aliases!Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/jets/application.rb', line 79

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 default 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.}:log-group:/aws/lambda/#{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_pathsObject



37
38
39
40
41
42
43
# File 'lib/jets/application.rb', line 37

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