Class: Nyara::NyaraConfig

Inherits:
ConfigHash show all
Defined in:
lib/nyara/config.rb

Overview

Options

  • env - environment, default is 'development'
  • port - listen port number, default is 3000. you can pass -p in the command line to override it
  • daemon - whether run in the background, default is false. you can pass -d in the command line to override it
  • workers - number of workers
  • host - host name used in url_to helper
  • root - root path, default is Dir.pwd
  • views - views (templates) directory, relative to root, default is "views"
  • public - static files directory, relative to root, default is "public"
  • x_send_file - header field name for X-Sendfile or X-Accel-Redirect, see Nyara::Controller#send_file for details
  • session - see Nyara::Session for sub options
  • prefer_erb - use ERB instead of ERubis for .erb templates
  • logger - if set, every request is logged, and you can use Nyara.logger to do your own logging.
  • before_fork - a proc to run before forking
  • after_fork - a proc to run after forking
  • watch - if true, watch change under project dir and reload automaticly, useful for development. default is false. if set to a dir name (under project root), watch only changes under the dir.
  • watch_assets - if true, watch change with linner (you need gem install linner first), useful for development. default is false. the asset dir to be watched is configured in Linnerfile.
  • timeout - after (at least) how many seconds do we delete an inactive request. default is 120.

logger example

# use default logger
set :logger, true

# use daily logger, the lambda is invoked to create the logger
set :logger, lambda{ ::Logger.new '/var/server.log', 'daily' }

# use other logger class
set :logger, MySimpleLogger

# disable logger
set :logger, false

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ConfigHash

#[], #[]=

Methods inherited from ParamHash

#[], #[]=, #_aref, #_aset, #key?, #nested_aref, #nested_aset, parse_cookie, parse_param, split_name

Methods inherited from Hash

#to_param

Instance Attribute Details

#loggerObject

Returns the value of attribute logger


84
85
86
# File 'lib/nyara/config.rb', line 84

def logger
  @logger
end

Instance Method Details

#assets_path(path, strict = true) ⇒ Object

Get absoute path under assets path

Options

  • strict - return nil if path is not under the dir

137
138
139
# File 'lib/nyara/config.rb', line 137

def assets_path path, strict=true
  path_under "assets", path, strict
end

#configure(&blk) ⇒ Object


190
191
192
# File 'lib/nyara/config.rb', line 190

def configure &blk
  instance_eval &blk
end

#create_loggerObject

Create a logger with the 'logger' option


87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/nyara/config.rb', line 87

def create_logger
  l = self['logger']

  if l == true or l.nil?
    ::Logger.new(production? ? project_path('production.log') : STDOUT)
  elsif l.is_a?(Class)
    l.new(production? ? project_path('production.log') : STDOUT)
  elsif l.is_a?(Proc)
    l.call
  elsif l
    raise 'bad logger configure, should be: `true` / `false` / Class / Proc'
  end
end

#development?Boolean

Returns:

  • (Boolean)

169
170
171
172
# File 'lib/nyara/config.rb', line 169

def development?
  e = env
  e.empty? or e == 'development'
end

#envObject

Get environment


161
162
163
# File 'lib/nyara/config.rb', line 161

def env
  self['env'].to_s
end

#initObject

Init and check configures


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nyara/config.rb', line 46

def init
  self['env'] ||= 'development'

  n = (self['port'] || 3000).to_i
  assert n >= 0 && n <= 65535
  self['port'] = n

  n = (self['workers'] || self['worker'] || ((CpuCounter.count + 1)/ 2)).to_i
  assert n > 0 && n < 1000
  self['workers'] = n

  unless self['root']
    set :root, Dir.pwd
  end
  self['root'] = File.realpath File.expand_path self['root']

  # todo warn paths not under project?
  self['views'] = project_path(self['views'] || 'views')
  if self['public']
    self['public'] = project_path(self['public'])
  end

  if self['assets']
    self['assets'] = project_path(self['assets'])
  end

  self.logger = create_logger

  assert !self['before_fork'] || self['before_fork'].respond_to?('call')
  assert !self['after_fork'] || self['after_fork'].respond_to?('call')

  self['timeout'] ||= 120
  timeout = self['timeout'].to_i
  assert timeout > 0 && timeout < 2**30
  self['timeout'] = timeout
  Ext.set_inactive_timeout timeout
end

#map(prefix, controller) ⇒ Object

Pass requests under a prefix to a controller


156
157
158
# File 'lib/nyara/config.rb', line 156

def map prefix, controller
  Route.register_controller prefix, controller
end

#path_under(key, path, strict = true) ⇒ Object

Get path under the dir configured Nyara.config[key]

Options

  • strict - return nil if path is not under the dir

147
148
149
150
151
152
153
# File 'lib/nyara/config.rb', line 147

def path_under key, path, strict=true
  dir = self[key]
  path = File.expand_path File.join(dir, path)
  if !strict or path.start_with?(dir)
    path
  end
end

#production?Boolean

Returns:

  • (Boolean)

174
175
176
# File 'lib/nyara/config.rb', line 174

def production?
  env == 'production'
end

#project_path(path, strict = true) ⇒ Object

Get absoute path under project path

Options

  • strict - return nil if path is not under the dir

107
108
109
# File 'lib/nyara/config.rb', line 107

def project_path path, strict=true
  path_under 'root', path, strict
end

#public_path(path, strict = true) ⇒ Object

Get absoute path under public path

Options

  • strict - return nil if path is not under the dir

117
118
119
# File 'lib/nyara/config.rb', line 117

def public_path path, strict=true
  path_under 'public', path, strict
end

#resetObject

Clear all settings


40
41
42
43
# File 'lib/nyara/config.rb', line 40

def reset
  clear
  Route.clear
end

#rootObject


165
166
167
# File 'lib/nyara/config.rb', line 165

def root
  self['root'].to_s
end

#test?Boolean

Returns:

  • (Boolean)

178
179
180
# File 'lib/nyara/config.rb', line 178

def test?
  env == 'test'
end

#views_path(path, strict = true) ⇒ Object

Get absoute path under views path

Options

  • strict - return nil if path is not under the dir

127
128
129
# File 'lib/nyara/config.rb', line 127

def views_path path, strict=true
  path_under 'views', path, strict
end