Class: Whoosh::CLI::Main
- Inherits:
-
Thor
- Object
- Thor
- Whoosh::CLI::Main
- Defined in:
- lib/whoosh/cli/main.rb
Instance Method Summary collapse
- #check ⇒ Object
- #ci ⇒ Object
- #console ⇒ Object
- #describe ⇒ Object
- #mcp ⇒ Object
- #new(name) ⇒ Object
- #routes ⇒ Object
- #server ⇒ Object
- #version ⇒ Object
- #worker ⇒ Object
Instance Method Details
#check ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/whoosh/cli/main.rb', line 190 def check app_file = File.join(Dir.pwd, "app.rb") unless File.exist?(app_file) puts "✗ No app.rb found" exit 1 end puts "=> Whoosh App Check" puts "" issues = [] warnings = [] # Check .env if File.exist?(".env") env_content = File.read(".env") if env_content.include?("change_me") || env_content.include?("CHANGE_ME") issues << "JWT_SECRET in .env still has default value — run: ruby -e \"puts SecureRandom.hex(32)\"" end else warnings << "No .env file — copy .env.example to .env" end # Check .gitignore includes .env if File.exist?(".gitignore") unless File.read(".gitignore").include?(".env") issues << ".gitignore does not exclude .env — secrets may be committed" end else warnings << "No .gitignore file" end # Load and validate the app begin require app_file app = ObjectSpace.each_object(Whoosh::App).first if app puts " ✓ App loads successfully" # Check auth if app.authenticator puts " ✓ Auth configured" else warnings << "No auth configured — API is open to everyone" end # Check rate limiting if app.rate_limiter_instance puts " ✓ Rate limiting configured" else warnings << "No rate limiting — vulnerable to abuse" end # Check routes route_count = app.routes.length puts " ✓ #{route_count} routes registered" # Check MCP app.to_rack mcp_count = app.mcp_server.list_tools.length puts " ✓ #{mcp_count} MCP tools" else issues << "No Whoosh::App instance found in app.rb" end rescue => e issues << "App failed to load: #{e.}" end # Check dependencies puts "" %w[falcon oj sequel].each do |gem_name| begin require gem_name puts " ✓ #{gem_name} available" rescue LoadError label = case gem_name when "falcon" then "(recommended server)" when "oj" then "(5-10x faster JSON)" when "sequel" then "(database)" end warnings << "#{gem_name} not installed #{label}" end end # Report puts "" if issues.empty? && warnings.empty? puts "=> All checks passed! ✓" else issues.each { |i| puts " ✗ #{i}" } warnings.each { |w| puts " ⚠ #{w}" } puts "" puts issues.empty? ? "=> #{warnings.length} warning(s)" : "=> #{issues.length} issue(s), #{warnings.length} warning(s)" exit 1 unless issues.empty? end end |
#ci ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/whoosh/cli/main.rb', line 407 def ci puts "=> Whoosh CI Pipeline" puts "=" * 50 puts "" steps = [] skipped = [] # Step 1: Rubocop (lint) if system("bundle exec rubocop --version > /dev/null 2>&1") steps << { name: "Rubocop (lint)", cmd: "bundle exec rubocop --format simple" } else skipped << "Rubocop (add rubocop to Gemfile)" end # Step 2: Brakeman (security scan) if system("bundle exec brakeman --version > /dev/null 2>&1") steps << { name: "Brakeman (security)", cmd: "bundle exec brakeman -q --no-pager" } else skipped << "Brakeman (add brakeman to Gemfile)" end # Step 3: Bundle audit (CVE check) if system("bundle exec bundle-audit version > /dev/null 2>&1") steps << { name: "Bundle Audit (CVE)", cmd: "bundle exec bundle-audit check --update" } elsif system("bundle audit --help > /dev/null 2>&1") steps << { name: "Bundle Audit (CVE)", cmd: "bundle audit" } else skipped << "Bundle Audit (add bundler-audit to Gemfile)" end # Step 4: Secret leak scan (built-in, no gem needed) steps << { name: "Secret Scan", type: :secret_scan } # Step 5: RSpec (tests) if system("bundle exec rspec --version > /dev/null 2>&1") steps << { name: "RSpec (tests)", cmd: "bundle exec rspec --format progress" } else skipped << "RSpec (add rspec to Gemfile)" end # Step 6: Coverage check (built-in) steps << { name: "Coverage Check", type: :coverage_check } skipped.each { |s| puts " [skip] #{s}" } puts "" unless skipped.empty? failed = [] steps.each_with_index do |step, i| puts "--- [#{i + 1}/#{steps.length}] #{step[:name]} ---" success = case step[:type] when :secret_scan run_secret_scan when :coverage_check run_coverage_check else system(step[:cmd]) end if success puts " ✓ #{step[:name]} passed" else puts " ✗ #{step[:name]} FAILED" failed << step[:name] end puts "" end puts "=" * 50 if failed.empty? puts "=> All #{steps.length} checks passed! ✓" exit 0 else puts "=> FAILED: #{failed.join(', ')}" exit 1 end end |
#console ⇒ Object
289 290 291 292 293 294 |
# File 'lib/whoosh/cli/main.rb', line 289 def console app_file = File.join(Dir.pwd, "app.rb") require app_file if File.exist?(app_file) require "irb" IRB.start end |
#describe ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/whoosh/cli/main.rb', line 133 def describe app = load_app return unless app app.to_rack # ensure everything is built output = {} unless [:schemas] output[:routes] = app.routes.map do |r| match = app.instance_variable_get(:@router).match(r[:method], r[:path]) handler = match[:handler] if match route_info = { method: r[:method], path: r[:path], auth: r[:metadata]&.dig(:auth), mcp: r[:metadata]&.dig(:mcp) || false } if handler && handler[:request_schema] route_info[:request_schema] = OpenAPI::SchemaConverter.convert(handler[:request_schema]) end if handler && handler[:response_schema] route_info[:response_schema] = OpenAPI::SchemaConverter.convert(handler[:response_schema]) end route_info end end unless [:routes] # Collect all Schema subclasses schemas = {} ObjectSpace.each_object(Class).select { |k| k < Schema && k != Schema }.each do |klass| schemas[klass.name] = OpenAPI::SchemaConverter.convert(klass) if klass.name end output[:schemas] = schemas unless schemas.empty? end output[:config] = { app_name: app.config.app_name, port: app.config.port, env: app.config.env, docs_enabled: app.config.docs_enabled?, auth_configured: !!app.authenticator, rate_limit_configured: !!app.rate_limiter_instance, mcp_tools: app.mcp_server.list_tools.map { |t| t[:name] } } output[:framework] = { version: Whoosh::VERSION, ruby: RUBY_VERSION, yjit: Performance.yjit_enabled?, json_engine: Serialization::Json.engine } puts JSON.pretty_generate(output) end |
#mcp ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/whoosh/cli/main.rb', line 299 def mcp app_file = File.join(Dir.pwd, "app.rb") unless File.exist?(app_file) puts "Error: app.rb not found" exit 1 end require app_file app = ObjectSpace.each_object(Whoosh::App).first unless app puts "No Whoosh::App instance found" exit 1 end app.to_rack if [:list] app.mcp_server.list_tools.each { |t| puts " #{t[:name]} - #{t[:description]}" } return end $stdin.each_line do |line| next if line.strip.empty? begin request = MCP::Protocol.parse(line) response = app.mcp_server.handle(request) if response $stdout.puts(MCP::Protocol.encode(response)) $stdout.flush end rescue MCP::Protocol::ParseError => e err = MCP::Protocol.error_response(id: nil, code: -32700, message: e.) $stdout.puts(MCP::Protocol.encode(err)) $stdout.flush end end end |
#new(name) ⇒ Object
559 560 561 562 |
# File 'lib/whoosh/cli/main.rb', line 559 def new(name) require "whoosh/cli/project_generator" ProjectGenerator.create(name, minimal: [:minimal], full: [:full]) end |
#routes ⇒ Object
124 125 126 127 128 |
# File 'lib/whoosh/cli/main.rb', line 124 def routes app = load_app return unless app app.routes.each { |r| puts " #{r[:method].ljust(8)} #{r[:path]}" } end |
#server ⇒ Object
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 |
# File 'lib/whoosh/cli/main.rb', line 21 def server app_file = File.join(Dir.pwd, "app.rb") config_ru = File.join(Dir.pwd, "config.ru") unless File.exist?(app_file) || File.exist?(config_ru) puts "Error: No app.rb or config.ru found in #{Dir.pwd}" exit 1 end port = [:port] host = [:host] puts "=> Whoosh v#{Whoosh::VERSION} starting..." puts "=> http://#{host}:#{port}" puts "=> Ctrl-C to stop" puts "" if [:reload] puts "=> Watching for file changes (polling)..." pid = nil start = -> { pid = Process.spawn( {"WHOOSH_PORT" => port.to_s, "WHOOSH_HOST" => host}, RbConfig.ruby, "-e", "require 'rackup'; app, _ = Rack::Builder.parse_file('#{config_ru || "config.ru"}'); Rackup::Server.start(app: app, Port: #{port}, Host: '#{host}')" ) } start.call trap("INT") { Process.kill("TERM", pid) rescue nil; exit 0 } trap("TERM") { Process.kill("TERM", pid) rescue nil; exit 0 } mtimes = {} loop do sleep 1.5 changed = false Dir.glob("{**/*.rb,config/**/*.yml}").each do |f| mt = File.mtime(f) rescue next if mtimes[f] && mtimes[f] != mt puts "=> Changed: #{f}, restarting..." changed = true end mtimes[f] = mt end if changed Process.kill("TERM", pid) rescue nil Process.wait(pid) rescue nil start.call end end return end # Load the app if File.exist?(config_ru) rack_app, _ = Rack::Builder.parse_file(config_ru) else require app_file whoosh_app = ObjectSpace.each_object(Whoosh::App).first unless whoosh_app puts "Error: No Whoosh::App instance found in app.rb" exit 1 end rack_app = whoosh_app.to_rack end # Auto-detect server based on platform # macOS: Puma (threads) — Falcon forks crash due to ObjC runtime + native C extensions # Linux: Falcon (fibers) — faster async I/O, no fork issue require "rackup" server_opts = { app: rack_app, Port: port, Host: host } if RUBY_PLATFORM.include?("darwin") # Force Puma on macOS to avoid fork crashes begin require "puma" server_opts[:server] = "puma" puts "=> Using Puma (macOS — Falcon forks are unsafe here)" rescue LoadError puts "=> Using default server (install puma for best macOS experience)" end else # Linux: prefer Falcon for performance begin require "falcon" server_opts[:server] = "falcon" puts "=> Using Falcon (Linux — async fibers for best performance)" rescue LoadError begin require "puma" server_opts[:server] = "puma" puts "=> Using Puma" rescue LoadError puts "=> Using default server" end end end Rackup::Server.start(**server_opts) end |
#version ⇒ Object
12 13 14 |
# File 'lib/whoosh/cli/main.rb', line 12 def version puts "Whoosh v#{Whoosh::VERSION}" end |
#worker ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/whoosh/cli/main.rb', line 337 def worker app_file = File.join(Dir.pwd, "app.rb") unless File.exist?(app_file) puts "Error: app.rb not found" exit 1 end require app_file whoosh_app = ObjectSpace.each_object(Whoosh::App).first unless whoosh_app puts "Error: No Whoosh::App found" exit 1 end whoosh_app.to_rack # boots everything including Jobs concurrency = [:concurrency] puts "=> Whoosh worker (#{concurrency} threads)..." workers = concurrency.times.map do w = Jobs::Worker.new(backend: Jobs.backend, di: Jobs.di, max_retries: whoosh_app.config.data.dig("jobs", "retry") || 3, retry_delay: whoosh_app.config.data.dig("jobs", "retry_delay") || 5) Thread.new { w.run_loop } w end trap("INT") { workers.each(&:stop); exit 0 } trap("TERM") { workers.each(&:stop); exit 0 } sleep end |