-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
executable file
·64 lines (54 loc) · 1.34 KB
/
app.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
# -*- coding: utf-8 -*-
require 'rubygems'
require 'sinatra/base'
require 'fluent-logger'
require 'haml'
require 'json'
require 'date'
require 'time'
class FluentdJsonReceiver < Sinatra::Base
#require './helpers/render_partial'
def initialize(app = nil, params = {})
super(app)
@fluentd = Fluent::Logger::FluentLogger.open(nil,
host = 'localhost',
port = '29999')
@root = Sinatra::Application.environment == :production ? '/post/' : '/'
end
def logger
env['app.logger'] || env['rack.logger']
end
helpers do
def protected!
return if authorized?
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
halt 401, "Not authorized\n"
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? and @auth.basic? and @auth.credentials and
@auth.credentials == ['username', 'password']
end
end
# Logging
configure :development, :production do
enable :logging
end
# Reloader
configure :development do
register Sinatra::Reloader
end
# Root Index
get '/' do
protected!
haml :index
end
# Generic Routing
post '/?' do
protected!
json = params[:data].class == Hash ? params[:data] : JSON.parse(params[:data])
tag = params[:tag]
@fluentd.post(tag, json)
end
run! if app_file == $0
end