class Gack::Application

The main DSL for making Gemini apps with Gack

Attributes

routes[R]

Public Class Methods

new(routes, port: nil, server: Gack::Server) click to toggle source
# File lib/gack/application.rb, line 21
def initialize(routes, port: nil, server: Gack::Server)
  @routes = routes
  @server = server
  @port = port
end
route(path, &handler) click to toggle source
# File lib/gack/application.rb, line 6
def self.route(path, &handler)
  routes << Gack::Route.new(path, &handler)
end
routes() click to toggle source
# File lib/gack/application.rb, line 10
def self.routes
  @routes ||= []
end
run!(**opts) click to toggle source

`run!(port: 1234)` to change the port the server runs on

# File lib/gack/application.rb, line 15
def self.run!(**opts)
  new(routes, **opts).run!
end

Public Instance Methods

match_route(location) click to toggle source
# File lib/gack/application.rb, line 47
def match_route(location)
  routes.find { |s| s.path_match?(location) }
end
run!() click to toggle source
# File lib/gack/application.rb, line 27
def run!
  server.event_loop do |request|
    server_loop_handler(request)
  end
end
server_loop_handler(request) click to toggle source
# 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

Private Instance Methods

server() click to toggle source
# File lib/gack/application.rb, line 53
def server
  if @port
    @server.new(port: @port)
  else
    @server.new
  end
end