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

#configObject



19
20
21
22
23
24
25
26
27
# File 'lib/jets/application.rb', line 19

def config
  config = ActiveSupport::OrderedOptions.new
  config.prewarm = ActiveSupport::OrderedOptions.new
  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



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

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



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jets/application.rb', line 48

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



96
97
98
99
100
101
102
103
104
105
# File 'lib/jets/application.rb', line 96

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



116
117
118
119
# File 'lib/jets/application.rb', line 116

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.



87
88
89
90
91
92
93
94
# File 'lib/jets/application.rb', line 87

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.



112
113
114
# File 'lib/jets/application.rb', line 112

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

#set_aliases!Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jets/application.rb', line 72

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
  config.project_namespace = [config.project_name, config.short_env, config.env_extra].compact.join('-')
  # table_namespace does not have the env_extra, more common case desired.
  config.table_namespace = [config.project_name, config.short_env].compact.join('-')
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



30
31
32
33
34
35
36
# File 'lib/jets/application.rb', line 30

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