Class: App
- Inherits:
-
Object
- Object
- App
- Defined in:
- lib/TeleRuby/app.rb
Instance Method Summary collapse
- #add_router(path, router) ⇒ Object
- #call(env) ⇒ Object
- #handle_request(env) ⇒ Object
-
#initialize ⇒ App
constructor
A new instance of App.
- #run ⇒ Object
Constructor Details
#initialize ⇒ App
Returns a new instance of App.
9 10 11 |
# File 'lib/TeleRuby/app.rb', line 9 def initialize @routers = {} end |
Instance Method Details
#add_router(path, router) ⇒ Object
13 14 15 |
# File 'lib/TeleRuby/app.rb', line 13 def add_router(path, router) @routers[path] = router end |
#call(env) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/TeleRuby/app.rb', line 17 def call(env) # Use a Thread to handle the request asynchronously result = nil thread = Thread.new do result = handle_request(env) end thread.join # Ensure the thread completes before returning result # Return the result from the thread end |
#handle_request(env) ⇒ Object
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 |
# File 'lib/TeleRuby/app.rb', line 28 def handle_request(env) begin if env["CONTENT_TYPE"] == "application/json" request_body = JSON.parse(env["rack.input"].read) env["parsed_body"] = request_body end query_params = Rack::Utils.parse_query(env["QUERY_STRING"]) env["query_params"] = query_params full_path = env["PATH_INFO"] method = env["REQUEST_METHOD"].to_sym @routers.each do |base_path, router| if full_path.start_with?(base_path) sub_path = full_path.sub(base_path, "") return router.call(sub_path, method, env, full_path) end end json_response(404, { error: "Not Found" }) rescue => e puts "[ERROR] #{e.}" puts e.backtrace.join("\n") json_response(500, { error: "Internal Server Error" }) end end |
#run ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/TeleRuby/app.rb', line 57 def run puts "Starting Puma Server..." server = Puma::Server.new(self) server.add_tcp_listener('127.0.0.1', 3000) # Port setup trap(:INT) do puts "\nStopping the server..." server.stop end puts "Server successfully started." server.run server.thread.join # Ensure server blocks the main thread end |