1+ #!/usr/bin/env ruby
2+ # frozen_string_literal: true
3+
4+ $LOAD_PATH. unshift File . expand_path ( "../lib" , __dir__ )
5+
6+ puts "Loading hyper_ruby"
7+
8+ require "hyper_ruby"
9+ require "json"
10+
11+ # Create and configure the server
12+ server = HyperRuby ::Server . new
13+ config = {
14+ bind_address : ENV . fetch ( "BIND_ADDRESS" , "127.0.0.1:3000" ) ,
15+ tokio_threads : ENV . fetch ( "TOKIO_THREADS" , "1" ) . to_i ,
16+ debug : ENV . fetch ( "DEBUG" , "false" ) == "true" ,
17+ recv_timeout : ENV . fetch ( "RECV_TIMEOUT" , "30000" ) . to_i
18+ }
19+ server . configure ( config )
20+
21+ puts "Starting server with config: #{ config } "
22+
23+ # Start the server
24+ server . start
25+
26+ puts "Server started"
27+
28+ # Create a worker thread to handle requests
29+ worker = Thread . new do
30+ server . run_worker do |request |
31+ buffer = String . new ( capacity : 1024 )
32+ request . fill_body ( buffer )
33+
34+ # Create a response that echoes back request details
35+ response_data = {
36+ method : request . http_method ,
37+ path : request . path ,
38+ headers : request . headers ,
39+ body : buffer
40+ }
41+
42+ HyperRuby ::Response . new (
43+ 200 ,
44+ { "Content-Type" => "application/json" } ,
45+ JSON . pretty_generate ( response_data )
46+ )
47+ end
48+ end
49+
50+ puts "Server running at #{ config [ :bind_address ] } "
51+ puts "Press Ctrl+C to stop"
52+
53+ # Wait for Ctrl+C
54+ begin
55+ sleep
56+ rescue Interrupt
57+ puts "\n Shutting down..."
58+ server . stop
59+ worker . join
60+ end
0 commit comments