class DCell::Explorer

Web UI for DCell TODO: rewrite this entire thing with less hax

Constants

ASSET_ROOT

Public Class Methods

new(host = "127.0.0.1", port = 7778) click to toggle source
Calls superclass method
# File lib/dcell/explorer.rb, line 12
def initialize(host = "127.0.0.1", port = 7778)
  super(host, port, &method(:on_connection))
end

Public Instance Methods

node_path(node) click to toggle source
# File lib/dcell/explorer.rb, line 70
def node_path(node)
  "/nodes/#{node.id}"
end
on_connection(connection) click to toggle source
# File lib/dcell/explorer.rb, line 16
def on_connection(connection)
  request = connection.request
  return unless request
  route connection, request
end
render_resource(connection, path) click to toggle source
# File lib/dcell/explorer.rb, line 38
def render_resource(connection, path)
  if node_id = path[%r{^nodes/(.*)$}, 1]
    node = DCell::Node[node_id]
    path = "index.html"
  else
    node = DCell.me
  end

  asset_path = ASSET_ROOT.join(path)
  if asset_path.exist?
    asset_path.open("r") do |file|
      connection.respond :ok, file
    end

    Logger.info "200 OK: /#{path}"
  elsif File.exist?(asset_path.to_s + ".erb") and node
    connection.respond :ok, render_template(asset_path.to_s + ".erb", node)
    Logger.info "200 OK: /#{path}"
  else
    connection.respond :not_found, "Not found"
    Logger.info "404 Not Found: /#{path}"
  end
end
render_template(template, node) click to toggle source
# File lib/dcell/explorer.rb, line 62
def render_template(template, node)
  @node = node
  @info = @node[:info].to_hash

  template = ERB.new File.read(template, :mode => 'rb')
  template.result(binding)
end
route(connection, request) click to toggle source
# File lib/dcell/explorer.rb, line 22
def route(connection, request)
  if request.url == "/"
    path = "index.html"
  else
    path = request.url[%r{^/([a-z0-9\.\-_]+(/[a-z0-9\.\-_]+)*)$}, 1]
  end

  if !path or path[".."]
    Logger.info "404 Not Found: #{request.path}"
    connection.respond :not_found, "Not found"
    return
  end

  render_resource connection, path
end