-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cr
More file actions
113 lines (90 loc) · 3.16 KB
/
app.cr
File metadata and controls
113 lines (90 loc) · 3.16 KB
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
require "option_parser"
require "./config"
require "./source/constants"
module PlaceOS::Source
# Server defaults
host = DEFAULT_HOST
port = DEFAULT_PORT
# Command line options
OptionParser.parse(ARGV.dup) do |parser|
parser.banner = "Usage: #{APP_NAME} [arguments]"
# Server flags
parser.on("-b HOST", "--bind=HOST", "Specifies the server host") { |h| host = h }
parser.on("-p PORT", "--port=PORT", "Specifies the server port") { |p| port = p.to_i }
parser.on("-r", "--routes", "List the application routes") do
ActionController::Server.print_routes
exit 0
end
parser.on("-v", "--version", "Display the application version") do
puts "#{APP_NAME} v#{VERSION}"
exit 0
end
parser.on("-c URL", "--curl=URL", "Perform a basic health check by requesting the URL") do |url|
begin
response = HTTP::Client.get url
exit 0 if (200..499).includes? response.status_code
puts "health check failed, received response code #{response.status_code}"
exit 1
rescue error
puts error.inspect_with_backtrace(STDOUT)
exit 2
end
end
parser.on("-d", "--docs", "Outputs OpenAPI documentation for this service") do
puts ActionController::OpenAPI.generate_open_api_docs(
title: PlaceOS::Source::APP_NAME,
version: PlaceOS::Source::VERSION,
description: "PlaceOS Source Service, saves state to InfluxDB and handles MQTT output"
).to_yaml
exit 0
end
parser.invalid_option do |flag|
STDERR.puts "ERROR: #{flag} unrecognised"
puts parser
exit 1
end
parser.on("-h", "--help", "Show this help") do
puts parser
exit 0
end
end
# Configure the database connection. First check if PG_DATABASE_URL environment variable
# is set. If not, assume database configuration are set via individual environment variables
if pg_url = ENV["PG_DATABASE_URL"]?
PgORM::Database.parse(pg_url)
else
PgORM::Database.configure { |_| }
end
publisher_managers = [] of PublisherManager
publisher_managers << MqttBrokerManager.new
influx_host, influx_api_key = INFLUX_HOST, INFLUX_API_KEY
# Add Influx to sources if adequate environmental configuration is present
if influx_host.nil? || influx_api_key.nil?
puts "Influx Manager not enabled as no host or key provided"
else
publisher_managers << InfluxManager.new(influx_host, influx_api_key)
end
# Start application manager
manager = Manager.new(publisher_managers)
manager.start
Manager.instance = manager
# timezone cache management
spawn { PlaceOS::Source::InfluxPublisher.timezone_cache_reset }
# Server Configuration
server = ActionController::Server.new(port, host)
terminate = Proc(Signal, Nil).new do |signal|
Log.info { "terminating gracefully" }
spawn { server.close }
signal.ignore
end
# Detect ctr-c to shutdown gracefully
# Docker containers use the term signal
Signal::INT.trap &terminate
Signal::TERM.trap &terminate
# Start the server
server.run do
Log.info { "listening on #{server.print_addresses}" }
end
# Shutdown message
puts "#{APP_NAME} leaps through the veldt\n"
end