-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebserver.rb
94 lines (74 loc) · 1.92 KB
/
webserver.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
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
#
# Include requierments
require "sinatra/base"
require "logger"
require "webrick"
require "yaml"
require "prometheus/client"
#
# Load the model
require "./model.rb"
#
# Sync to standard-out
$stdout.sync = true
class Webserver < Sinatra::Base
#
# Start the configuration/pre-run
configure do
#
# Get JSON-logging
$logger = Logger.new(STDOUT)
$logger.formatter = proc do |severity, datetime, progname, msg|
if msg.class == Hash
hash = {timestamp: datetime.strftime("%F %T.%L"), severity: severity}.merge(msg)
puts hash.to_json
else
hash = {timestamp: datetime.strftime("%F %T.%L"), severity: severity, message: msg}
puts hash.to_json
end
end
#
# Fix more logging
$logger.info({:message => "Starting the webserver"})
$logger.level = Logger::INFO
set :logger, $logger
#
# Set up env and variabels
username = ENV["USERNAME"]
password = ENV["PASSWORD"]
#
# Set up Prometheus
prometheus = Prometheus::Client.registry
@@requests_total = Prometheus::Client::Counter.new(:requests_total, docstring: "total_requests", labels: [:endpoint])
prometheus.register(@@requests_total)
@@model = Model.new(username, password)
$logger.info({:message => "Configuration done!"})
#
# Set up dummy-data
@@ok_json = {status: "ok"}.to_json
end
#
# Base-endpoint for Kubernetes
get "/readiness" do
content_type :json
[200, [@@ok_json]]
end
#
# Base-endpoint for Kubernetes
get "/ping" do
$logger.info({:message => "rack GET /ping"})
@@requests_total.increment(labels: { endpoint: "/ping" })
content_type :json
[200, [@@ok_json]]
end
#
# Base-endpoint
get "/" do
$logger.info({:message => "rack GET /"})
@@requests_total.increment(labels: { endpoint: "/" })
content_type :json
[200, [@@ok_json]]
end
end