Class: Gack::Application
- Inherits:
-
Object
- Object
- Gack::Application
- Defined in:
- lib/gack/application.rb
Overview
The main DSL for making Gemini apps with Gack
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
- .route(path, &handler) ⇒ Object
- .routes ⇒ Object
-
.run!(**opts) ⇒ Object
‘run!(port: 1234)` to change the port the server runs on.
Instance Method Summary collapse
-
#initialize(routes, port: nil, server: Gack::Server) ⇒ Application
constructor
A new instance of Application.
- #match_route(location) ⇒ Object
- #run! ⇒ Object
- #server_loop_handler(request) ⇒ Object
Constructor Details
#initialize(routes, port: nil, server: Gack::Server) ⇒ Application
Returns a new instance of Application.
21 22 23 24 25 |
# File 'lib/gack/application.rb', line 21 def initialize(routes, port: nil, server: Gack::Server) @routes = routes @server = server @port = port end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
19 20 21 |
# File 'lib/gack/application.rb', line 19 def routes @routes end |
Class Method Details
.route(path, &handler) ⇒ Object
6 7 8 |
# File 'lib/gack/application.rb', line 6 def self.route(path, &handler) routes << Gack::Route.new(path, &handler) end |
.routes ⇒ Object
10 11 12 |
# File 'lib/gack/application.rb', line 10 def self.routes @routes ||= [] end |
.run!(**opts) ⇒ Object
‘run!(port: 1234)` to change the port the server runs on
15 16 17 |
# File 'lib/gack/application.rb', line 15 def self.run!(**opts) new(routes, **opts).run! end |
Instance Method Details
#match_route(location) ⇒ Object
47 48 49 |
# File 'lib/gack/application.rb', line 47 def match_route(location) routes.find { |s| s.path_match?(location) } end |
#run! ⇒ Object
27 28 29 30 31 |
# File 'lib/gack/application.rb', line 27 def run! server.event_loop do |request| server_loop_handler(request) end end |
#server_loop_handler(request) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/gack/application.rb', line 33 def server_loop_handler(request) route = match_route(request.location) if route result = route.handle_request(request) if result.is_a?(Response) result else Response.new(Response::StatusCodes::SUCCESS, Response::MIME[:text], result) end else Response.new(Response::StatusCodes::NOT_FOUND) end end |