Class: Kirei::App

Inherits:
Routing::Base show all
Extended by:
T::Sig
Defined in:
lib/kirei/app.rb

Overview

This is the entrypoint into the application; it implements the Rack interface.

Constant Summary

Constants inherited from Routing::Base

Routing::Base::NOT_FOUND

Instance Attribute Summary

Attributes inherited from Routing::Base

#params

Class Method Summary collapse

Methods inherited from Routing::Base

#add_cors_headers, #call, #default_headers, #initialize, #render

Constructor Details

This class inherits a constructor from Kirei::Routing::Base

Class Method Details

.configObject



17
18
19
# File 'lib/kirei/app.rb', line 17

def config
  T.must(Kirei.configuration)
end

.default_db_nameObject



55
56
57
# File 'lib/kirei/app.rb', line 55

def default_db_name
  @default_db_name ||= T.let("#{config.app_name}_#{environment}".freeze, T.nilable(String))
end

.default_db_urlObject



64
65
66
67
68
69
# File 'lib/kirei/app.rb', line 64

def default_db_url
  @default_db_url ||= T.let(
    ENV.fetch("DATABASE_URL", "postgresql://localhost:5432/#{default_db_name}"),
    T.nilable(String),
  )
end

.environmentObject



46
47
48
# File 'lib/kirei/app.rb', line 46

def environment
  ENV.fetch("RACK_ENV", "development")
end

.raw_db_connectionObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kirei/app.rb', line 72

def raw_db_connection
  @raw_db_connection = T.let(@raw_db_connection, T.nilable(Sequel::Database))
  return @raw_db_connection unless @raw_db_connection.nil?

  # calling "Sequel.connect" creates a new connection
  @raw_db_connection = Sequel.connect(App.config.db_url || default_db_url)

  config.db_extensions.each do |ext|
    T.cast(@raw_db_connection, Sequel::Database).extension(ext)
  end

  if config.db_extensions.include?(:pg_json)
    # https://github.com/jeremyevans/sequel/blob/5.75.0/lib/sequel/extensions/pg_json.rb#L8
    @raw_db_connection.wrap_json_primitives = true
  end

  @raw_db_connection
end

.rootObject



22
23
24
# File 'lib/kirei/app.rb', line 22

def root
  defined?(::APP_ROOT) ? Pathname.new(::APP_ROOT) : Pathname.new(Dir.pwd)
end

.versionObject



33
34
35
36
37
38
39
40
# File 'lib/kirei/app.rb', line 33

def version
  @version = T.let(@version, T.nilable(String))
  @version ||= ENV.fetch("APP_VERSION", nil)
  @version ||= ENV.fetch("GIT_SHA", nil)
  @version ||= T.must(
    `command -v git && git rev-parse --short HEAD`.to_s.split("\n").last,
  ).freeze # localhost
end