-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm.rb
executable file
·141 lines (112 loc) · 2.77 KB
/
mm.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env ruby
require 'sinatra'
require 'yaml'
require 'json'
require 'log4r'
include Log4r
MOCK_FILE = File.join(File.dirname(__FILE__), "etc", "mock.yaml")
LOG_FILE = File.join(File.dirname(__FILE__), 'log', 'mm.log')
LOG_LEVEL = 'INFO'
# ---------------------------------------------------
JSON_CONTENT_TYPE = { "Content-Type" => "application/json;charset=utf-8" }
HTML_CONTENT_TYPE = { "Content-Type" => "text/html; charset=utf-8" }
if RUBY_VERSION < '1.9'
puts "Please upgrade to ruby 1.9"
exit 1
end
# initialzie logging
outputter = Log4r::FileOutputter.new('MM_LOG_FILE', \
:filename => LOG_FILE)
outputter.level = Log4r::Log4rConfig::LogLevels.index(LOG_LEVEL) + 1
$log = Logger.new 'Monitoring Mock'
$log.trace = false
$log.add(outputter)
$log.info "Mock up and running!"
# --- helper ---
helpers do
include Rack::Utils
alias_method :h, :escape_html
end
# --- filter ---
before do
# default content type
headers JSON_CONTENT_TYPE
end
# --- route matcher ---
class RouteMatcher
Match = Struct.new(:captures)
def initialize
@captures = Match.new([])
end
def match(str)
$routes = YAML.load(File.new(MOCK_FILE))
@captures unless $routes[str].nil?
end
end
def route_from_yaml
RouteMatcher.new
end
# --- initiate status hash ---
$status = Hash.new
$routes = YAML.load(File.new(MOCK_FILE))
$routes.each do |route|
$status[route.first] = true
end
# --- getter ---
get route_from_yaml do
path = request.path_info
paths = Array.new
result = true
$status[path] = true if $status[path].nil?
path.split('/').each do |step|
if not step.empty?
prefix = paths.last || ''
paths << prefix + '/' + step
end
end
paths.each do |step|
if not $status[step]
result = false
end
end
if result
{:status => 0,
:output => "Service OK",
:options => $routes[path]}.to_json
else
{:status => 2,
:output => "Service is broken",
:options => $routes[path]}.to_json
end
end
get '/dashboard' do
headers HTML_CONTENT_TYPE
@routes = $routes
@status = $status
erb :dashboard
end
# --- setter ---
post '/toggle*' do
path = params[:splat].last
$status[path] = !$status[path]
$log.info "Status - " + $status.inspect
{path.to_sym => $status[path]}.to_json
end
get '/*' do
return_error( 404, "Nothing found by that route" )
end
post '/*' do
return_error( 404, "Nothing found by that route" )
end
put '/*' do
return_error( 404, "Nothing found by that route" )
end
delete '/*' do
return_error( 404, "Nothing found by that route" )
end
# --- private functions ---
private
def return_error(code, message)
$log.error message
redirect '/', code, {:status => 3, :output => message}.to_json
end