Class: Tap::Controllers::Server

Inherits:
Tap::Controller show all
Includes:
Utils
Defined in:
lib/tap/controllers/server.rb

Overview

:startdoc::controller remotely controls and monitor a server

Server provides several uris to control and monitor the server behavior. Importantly, server allows the remote shutdown of a Tap::Server if a shutdown_key is set. This makes it possible to run servers in the background and still have a shutdown handle on them.

Constant Summary

Constants inherited from Tap::Controller

Tap::Controller::ServerError

Instance Attribute Summary

Attributes inherited from Tap::Controller

#controller_path, #request, #response, #server

Instance Method Summary collapse

Methods inherited from Tap::Controller

#action?, call, #dispatch, get, inherited, #initialize, #module_path, #module_render, nest, #redirect, #render, #render_erb, #render_layout, #route, set, set_variables, #template_path

Constructor Details

This class inherits a constructor from Tap::Controller

Instance Method Details

#accessObject

Essentially a login for server administration



29
30
31
32
33
34
35
36
37
# File 'lib/tap/controllers/server.rb', line 29

def access
  if request.get?
    render 'access.erb', :locals => {
      :secret => request['secret']
    }, :layout => true
  else
    redirect uri("admin", :secret => request['secret'])
  end
end

#admin(secret = nil) ⇒ Object

Administrate this server



40
41
42
43
# File 'lib/tap/controllers/server.rb', line 40

def admin(secret=nil)
  template = server.admin?(secret) ? 'admin.erb' : 'access.erb'
  render template, :locals => {:secret => secret}, :layout => true
end

#call(rack_env) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/tap/controllers/server.rb', line 69

def call(rack_env)
  path = rack_env['tap.server'].env.path(:public, rack_env['PATH_INFO']) {|file| File.file?(file) }
  if path
    static_file(path)
  else
    super
  end
end

#help(type = nil, *key) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/tap/controllers/server.rb', line 58

def help(type=nil, *key)
  if constant = server.env[type][key.join('/')]
    module_render 'help.erb', constant
  else
    "unknown #{type}: #{key.join('/')}"
  end
end

#help_uri(type, key) ⇒ Object

Returns a help uri for the specified resource, currently only sketched out.



90
91
92
# File 'lib/tap/controllers/server.rb', line 90

def help_uri(type, key)
  uri("help/#{type}/#{key}")
end

#indexObject



18
19
20
# File 'lib/tap/controllers/server.rb', line 18

def index
  render('index.erb', :layout => true)
end

#pingObject

Returns pong



23
24
25
26
# File 'lib/tap/controllers/server.rb', line 23

def ping
  response['Content-Type'] = "text/plain"
  "pong"
end

#shutdown(secret = nil) ⇒ Object

Terminates app and stops self (on post).



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tap/controllers/server.rb', line 46

def shutdown(secret=nil)
  response['Content-Type'] = "text/plain"
  
  if server.admin?(secret) && request.post?
    # wait a bit to shutdown, so the response is sent out.
    Thread.new { sleep(0.1); server.stop! }
    "shutdown"
  else
    ""
  end
end

#stop!Object

Stops the server.



95
96
97
# File 'lib/tap/controllers/server.rb', line 95

def stop!
  server.stop!
end

#uri(action = nil, params = {}) ⇒ Object

Returns a controller uri, attaching the secret to the action, if specified.



79
80
81
82
83
84
85
86
87
# File 'lib/tap/controllers/server.rb', line 79

def uri(action=nil, params={})
  secret = params.delete(:secret)
  
  if secret
    action = action ? "#{action}/#{secret}" : secret
  end
  
  super(action, params)
end