-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
60 lines (52 loc) · 1.84 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
# frozen_string_literal: true
require 'sinatra/base'
require 'sinatra/config_file'
require 'sinatra/json'
require 'json'
unless ENV['RACK_ENV'] == 'production'
require 'sinatra/reloader'
require 'pry'
end
class GelfProxy < Sinatra::Base
before do
content_type :json
end
register Sinatra::ConfigFile
config_file 'config/proxy.yml'
#############################################################################
# load configuration from ENV or file or default
#############################################################################
APP_NAME = ENV['APP_NAME'] || settings.app_name || 'gelf proxy'
LOGGER_USERNAME = ENV['LOGGER_USERNAME'] || settings.logger_username || 'gelf'
LOGGER_PASSWORD = ENV['LOGGER_PASSWORD'] || settings.logger_password || 'fleg'
#############################################################################
# production env settings, force auth
#############################################################################
if ENV['RACK_ENV'] == 'production'
use Rack::Auth::Basic, 'Protected Area' do |username, password|
username == LOGGER_USERNAME && password == LOGGER_PASSWORD
end
else
register Sinatra::Reloader
also_reload 'lib/*'
end
#############################################################################
# index
#############################################################################
get '/' do
{ hello: 'world' }.to_json
end
#############################################################################
# log
#############################################################################
post '/log' do
request.body.rewind
log = JSON.parse(request.body.read)
log.store('facility', APP_NAME)
puts log.to_json
rescue
puts request.body.read
end
## Run the app when file called ##
run! if $PROGRAM_NAME == __FILE__
end