Repository navigation
Expand file tree
/
Copy pathapp.cr
More file actions
114 lines (92 loc) · 3.18 KB
/
Copy pathapp.cr
File metadata and controls
114 lines (92 loc) · 3.18 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
114
require "option_parser"
require "./constants"
module App
# Server defaults
port = DEFAULT_PORT
host = DEFAULT_HOST
thread_count = DEFAULT_THREAD_COUNT
docs = nil
docs_file = nil
# Command line options
OptionParser.parse(ARGV.dup) do |parser|
parser.banner = "Usage: #{PROGRAM_NAME} [arguments]"
parser.on("-b HOST", "--bind=HOST", "Specifies the server host") { |bind_host| host = bind_host }
parser.on("-p PORT", "--port=PORT", "Specifies the server port") { |bind_port| port = bind_port.to_i }
parser.on("-w COUNT", "--workers=COUNT", "Specifies the number of threads to handle requests") do |workers|
thread_count = workers.to_i
end
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 "#{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
error.inspect_with_backtrace(STDOUT)
exit 2
end
end
parser.on("-d", "--docs", "Outputs OpenAPI documentation for this service") do
docs = ActionController::OpenAPI.generate_open_api_docs(
title: NAME,
version: VERSION,
description: "App description for OpenAPI docs"
).to_yaml
parser.on("-f FILE", "--file=FILE", "Save the docs to a file") do |file|
docs_file = file
end
end
parser.on("--mcp=FILE", "Saves the MCP tool descriptions, generated from your code comments") do |file|
ActionController::MCPServer.write_description(file)
puts "MCP description written to: #{file}"
exit 0
end
parser.on("-h", "--help", "Show this help") do
puts parser
exit 0
end
end
if docs
File.write(docs_file.as(String), docs) if docs_file
puts docs_file ? "OpenAPI written to: #{docs_file}" : docs
exit 0
end
# Load the routes
puts "Launching #{NAME} v#{VERSION}"
end
# Requiring config here ensures that the option parser runs before
# attempting to connect to databases etc.
require "./config"
module App
server = ActionController::Server.new(port, host)
# Model Context Protocol endpoint for LLM clients
ActionController::MCPServer.mount(server, MCP_PATH) unless MCP_PATH.empty?
# `System.cpu_count` is not always accurate
# Clustering using processes, forking is deprecated
# server.cluster(thread_count, "-w", "--workers")
# alternatively use threads
server.threads(thread_count)
Process.on_terminate do
puts "\n > terminating gracefully"
server.close
end
{% unless flag?(:win32) %}
# Allow signals to change the log level at run-time
# Toggle TRACE level logging `kill -s USR1 %PID`
register_severity_switch_signals
{% end %}
# Start the server
server.run do
puts "Listening on #{server.print_addresses}"
end
# Shutdown message
puts "#{NAME} leaps through the veldt\n"
end