Class: Tzispa::Application

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Helpers::ErrorView
Defined in:
lib/tzispa/app.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain_name) ⇒ Application

Returns a new instance of Application.



71
72
73
74
75
# File 'lib/tzispa/app.rb', line 71

def initialize(domain_name)
  @domain = Domain.new(domain_name)
  @middleware = Middleware.new self
  @config = Config::AppConfig.new(@domain).load!
end

Class Attribute Details

.routesObject

Returns the value of attribute routes.



34
35
36
# File 'lib/tzispa/app.rb', line 34

def routes
  @routes
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



27
28
29
# File 'lib/tzispa/app.rb', line 27

def config
  @config
end

#domainObject (readonly)

Returns the value of attribute domain.



27
28
29
# File 'lib/tzispa/app.rb', line 27

def domain
  @domain
end

#engineObject (readonly)

Returns the value of attribute engine.



27
28
29
# File 'lib/tzispa/app.rb', line 27

def engine
  @engine
end

#loggerObject (readonly)

Returns the value of attribute logger.



27
28
29
# File 'lib/tzispa/app.rb', line 27

def logger
  @logger
end

#middlewareObject (readonly)

Returns the value of attribute middleware.



27
28
29
# File 'lib/tzispa/app.rb', line 27

def middleware
  @middleware
end

#repositoryObject (readonly)

Returns the value of attribute repository.



27
28
29
# File 'lib/tzispa/app.rb', line 27

def repository
  @repository
end

Class Method Details

.applicationsObject



45
46
47
48
49
# File 'lib/tzispa/app.rb', line 45

def applications
  synchronize do
    @@applications ||= Set.new
  end
end

.inherited(base) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/tzispa/app.rb', line 36

def inherited(base)
  super
  base.class_eval do
    synchronize do
      applications.add(base)
    end
  end
end

.mount(mount_point, builder) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/tzispa/app.rb', line 57

def mount(mount_point, builder)
  self.new.tap { |app|
    self.routes ||= Routes.new(mount_point)
    yield(routes)
    app.middleware.map mount_point, builder
  }
end

.routerObject



65
66
67
# File 'lib/tzispa/app.rb', line 65

def router
  self.routes&.router
end

.synchronizeObject



51
52
53
54
55
# File 'lib/tzispa/app.rb', line 51

def synchronize
  Mutex.new.synchronize {
    yield
  }
end

Instance Method Details

#call(env) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tzispa/app.rb', line 77

def call(env)
  env[Tzispa::ENV_TZISPA_APP] = self
  context = Tzispa::Http::Context.new(env)
  env[Tzispa::ENV_TZISPA_CONTEXT] = context
  begin
    middleware.call(env)
  rescue StandardError, ScriptError => ex
    logger.error "#{ex.message}\n#{ex.backtrace.map { |trace| "\t #{trace}" }.join('\n') if ex.respond_to?(:backtrace) && ex.backtrace}"
    if config.developing
      context.error_500 error_report(ex)
    else
      context.error_500 error_page(domain)
    end
    context.response.finish
  end
end

#load!Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/tzispa/app.rb', line 94

def load!
  unless @loaded
    Mutex.new.synchronize {
      load_locales
      @repository = Data::Repository.new(@config.repository.to_h).load! if @config.respond_to? :repository
      @engine = Rig::Engine.new(self, @config.template_cache.enabled, @config.template_cache.size)
      @logger = Logger.new("logs/#{@domain.name}.log", 'weekly')
      @logger.level = @config.respond_to?(:developing) && @config.developing ? Logger::DEBUG : Logger::INFO
      @domain.require_dir 'helpers'
      @domain.require_dir 'api'
      @domain.require_dir 'middleware'
      @middleware.load!
      @loaded = true
    }
  end
  self
end