forked from erlehmann/logformat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb.rb
executable file
·113 lines (99 loc) · 2.34 KB
/
web.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env ruby
require 'sinatra'
require_relative '../lib/setup'
require_relative '../lib/models/message'
require_relative '../lib/models/channel'
require_relative '../lib/models/user'
include Logformat
set :views, File.join(File.dirname(__FILE__),'..','views')
set :public_folder, File.join(File.dirname(__FILE__),'..','public')
set :port, WEB_PORT
set :bind, WEB_BIND
set :server, 'thin'
helpers do
def h(text)
Rack::Utils.escape_html(text)
end
def check_channel_access!
user = User.from_env(request.env)
channel = Channel.find(:slug => params[:channel])
if channel.nil?
halt 404, erb(:error, :locals => {
:title => 'Logs: Not found',
:text => 'No such channel',
})
else
if channel.allowed?(user)
channel
else
headers['WWW-Authenticate'] = "Basic realm=\"Logs for #{channel.name}\""
halt 401
end
end
end
def parse_date
begin
DateTime.strptime(params[:date], '%Y-%m-%d')
rescue ArgumentError => e
halt 400, erb(:error, :locals => {
:title => "Logs: Invalid Date",
:text => "Invalid date",
})
end
end
end
get '/' do
content_type :html
erb :channel_list, :locals => {
:title => 'Logs',
:channels => Channel.all,
}
end
get '/-/whoami' do
content_type :text
User.from_env(request.env).name
end
get '/-/health' do
content_type :text
'OK'
end
get '/:channel/:date.txt' do
content_type :txt
channel = check_channel_access!
date = parse_date
channel
.messages_for_day(date)
.map { |m| m.to_s }
.join("\n")
end
get '/:channel/:date' do
content_type :html
channel = check_channel_access!
date = parse_date
erb :channel, :locals => {
:title => "Logs for #{channel.name}, #{date.strftime('%Y-%m-%d')}",
:messages => channel.messages_for_day(date),
:date => date,
:channel => channel,
}
end
# NOTE: this is quite expensive (sequential scan on messages)
get '/:channel' do
content_type :html
channel = check_channel_access!
erb :channel_days, :locals => {
:title => "Logs for #{channel.name}",
:channel => channel,
:days => Message
.filter(:channel => channel)
.select(
Sequel.as(
Sequel.function(:date, :time),
:date
)
)
.group(:date)
.order(Sequel.desc(:date))
.map(:date)
}
end