Class: Guaraci::Server
- Inherits:
-
Object
- Object
- Guaraci::Server
- Defined in:
- lib/guaraci/server.rb
Overview
HTTP server that handles incoming requests with a simple block-based API. Built on top of Async::HTTP for high performance and non-blocking I/O.
Class Method Summary collapse
-
.run(host: "localhost", port: 8000, &block) ⇒ Object
Starts an HTTP server on the specified host and port.
Instance Method Summary collapse
-
#call(request) ⇒ Protocol::HTTP::Response
Process an incoming HTTP request.
-
#initialize(&block) {|request| ... } ⇒ Server
constructor
Creates a new server instance with the given handler block.
Constructor Details
#initialize(&block) {|request| ... } ⇒ Server
Creates a new server instance with the given handler block.
59 60 61 |
# File 'lib/guaraci/server.rb', line 59 def initialize(&block) @handler = block end |
Class Method Details
.run(host: "localhost", port: 8000, &block) ⇒ Object
Starts an HTTP server on the specified host and port. The server runs indefinitely until stopped.
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/guaraci/server.rb', line 87 def self.run(host: "localhost", port: 8000, &block) app = new(&block) url = "http://#{host}:#{port}" Async do endpoint = Async::HTTP::Endpoint.parse(url) server = Async::HTTP::Server.new(app.method(:call), endpoint, protocol: Async::HTTP::Protocol::HTTP1) puts "Guaraci running on #{url}" server.run end end |
Instance Method Details
#call(request) ⇒ Protocol::HTTP::Response
Note:
This method is typically called internally by the server infrastructure and not directly by user code.
Process an incoming HTTP request.
This method is called by the Async::HTTP server for each incoming request. It wraps the raw request in a Request object and executes the configured handler block.
75 76 77 78 79 |
# File 'lib/guaraci/server.rb', line 75 def call(request) request = Request.new(request) instance_exec(request, &@handler) end |