Class: Etna::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/etna/server.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



86
87
88
89
90
91
92
93
94
# File 'lib/etna/server.rb', line 86

def initialize
  # Setup logging.
  application.setup_logger

  # This needs to be required before yabeda invocation, but cannot belong at the top of any module since clients
  # do not install yabeda.
  require 'yabeda'
  application.setup_yabeda
end

Class Attribute Details

.routesObject (readonly)

Returns the value of attribute routes.



63
64
65
# File 'lib/etna/server.rb', line 63

def routes
  @routes
end

Class Method Details

.delete(path, options = {}, &block) ⇒ Object



52
53
54
# File 'lib/etna/server.rb', line 52

def delete(path, options={}, &block)
  route('DELETE', path, options, &block)
end

.find_route(request) ⇒ Object



28
29
30
31
32
# File 'lib/etna/server.rb', line 28

def find_route(request)
  @routes.find do |route|
    route.matches? request
  end
end

.get(path, options = {}, &block) ⇒ Object



40
41
42
# File 'lib/etna/server.rb', line 40

def get(path, options={}, &block)
  route('GET', path, options, &block)
end

.post(path, options = {}, &block) ⇒ Object



44
45
46
# File 'lib/etna/server.rb', line 44

def post(path, options={}, &block)
  route('POST', path, options, &block)
end

.put(path, options = {}, &block) ⇒ Object



48
49
50
# File 'lib/etna/server.rb', line 48

def put(path, options={}, &block)
  route('PUT', path, options, &block)
end

.route(method, path, options = {}, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/etna/server.rb', line 5

def route(method, path, options={}, &block)
  # For healthchecks, set up servers
  #   with an OPTIONS route on /, with noauth
  @routes ||= [
    Etna::Route.new(
      'OPTIONS',
      '/',
      {
        auth: {
          noauth: true
        }
      }
    )
  ]

  @routes << Etna::Route.new(
    method,
    path,
    (@default_options || {}).merge(options),
    &block
  )
end

.route_path(request, name, params = {}) ⇒ Object



56
57
58
59
60
61
# File 'lib/etna/server.rb', line 56

def route_path(request,name,params={})
  route = routes.find do |route|
    route.name.to_s == name.to_s
  end
  return route ? route.path(params) : nil
end

.with(options = {}, &block) ⇒ Object



34
35
36
37
38
# File 'lib/etna/server.rb', line 34

def with(options={}, &block)
  @default_options = options
  instance_eval(&block)
  @default_options = nil
end

Instance Method Details

#call(env) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/etna/server.rb', line 66

def call(env)
  request = Rack::Request.new(env)

  request.env['etna.server'] = self

  application.logger.log_request(request)

  route = self.class.find_route(request)

  if route
    @params = request.env['rack.request.params']
    return route.call(self, request)
  end

  [404, {}, ["There is no such path '#{request.path}'"]]
rescue => e
  application.logger.log_error(e)
  raise
end