Module: Clarity::Server

Includes:
BasicAuth, ChunkHttp, EventMachine::HttpServer
Defined in:
lib/clarity/server.rb

Constant Summary

Constants included from ChunkHttp

ChunkHttp::LeadIn

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChunkHttp

#logfiles, #params, #path, #public_file, #render, #respond_with, #respond_with_chunks, #template

Methods included from BasicAuth

#authentication_data, #decode_credentials, #user_name_and_password

Instance Attribute Details

#log_filesObject

Returns the value of attribute log_files.



19
20
21
# File 'lib/clarity/server.rb', line 19

def log_files
  @log_files
end

#required_passwordObject

Returns the value of attribute required_password.



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

def required_password
  @required_password
end

#required_usernameObject

Returns the value of attribute required_username.



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

def required_username
  @required_username
end

Class Method Details

.run(options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/clarity/server.rb', line 21

def self.run(options)
  
  EventMachine::run do
    EventMachine.epoll
    EventMachine::start_server(options[:address], options[:port], self) do |a|
      a.log_files = options[:log_files]
      a.required_username = options[:username]
      a.required_password = options[:password]
    end

    STDERR.puts "Listening #{options[:address]}:#{options[:port]}..."
    
    if options[:user]
      STDERR.puts "Running as user #{options[:user]}"
      EventMachine.set_effective_user(options[:user])
    end
    
    STDERR.puts "Adding log files: #{options[:log_files].inspect}"
  end      
end

Instance Method Details

#error_page(error) ⇒ Object



94
95
96
97
# File 'lib/clarity/server.rb', line 94

def error_page(error)
  @error = error
  render "error.html.erb"
end

#process_http_requestObject



42
43
44
45
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
83
84
85
86
87
88
89
90
91
92
# File 'lib/clarity/server.rb', line 42

def process_http_request    
  authenticate!

  puts "action: #{path}"
  puts "params: #{params.inspect}"

  case path
  when '/'
    respond_with(200, welcome_page)

  when '/perform'
    if params.empty?
      respond_with(200, welcome_page)
    else
      # get command
      command = case params['tool']
        when 'grep' then CommandBuilder.new(params).command
        when 'tail' then TailCommandBuilder.new(params).command
        else raise InvalidParameterError, "Invalid Tool parameter"
      end
      response = respond_with_chunks
      response.chunk results_page # display page header

      puts "Running: #{command}"
      
      EventMachine::popen(command, GrepRenderer) do |grepper|
        @grepper = grepper          
        @grepper.response = response 
      end
    end

  when '/test'
    response = init_chunk_response
    EventMachine::add_periodic_timer(1) do 
      response.chunk "Lorem ipsum dolor sit amet<br/>"        
      response.send_chunks
    end

  else                          
    respond_with(200, public_file(path), :content_type => Mime.for(path))
  end

rescue InvalidParameterError => e
  respond_with(500, error_page(e))
rescue NotFoundError => e
  respond_with(404, "<h1>Not Found</h1>")
rescue NotAuthenticatedError => e
  puts "Could not authenticate user"
  headers = { "WWW-Authenticate" => %(Basic realm="Clarity")}
  respond_with(401, "HTTP Basic: Access denied.\n", :content_type => 'text/plain', :headers => headers)
end

#results_pageObject



103
104
105
# File 'lib/clarity/server.rb', line 103

def results_page
  render "index.html.erb"
end

#unbindObject



107
108
109
110
# File 'lib/clarity/server.rb', line 107

def unbind
  @grepper.close_connection if @grepper
  close_connection
end

#welcome_pageObject



99
100
101
# File 'lib/clarity/server.rb', line 99

def welcome_page
  render "index.html.erb"
end