class Application < Protocol::HTTP::Middleware

Inherits from
Protocol::HTTP::Middleware

The protocol-facing entrypoint for a Utopia application.

This object accepts Protocol::HTTP::Request instances, dispatches to the Utopia application stack, and normalizes the result back to a Protocol::HTTP::Response.

Definitions

def self.build(default_app = Response::NotFound, &block)

Build a Utopia application stack using the protocol HTTP middleware builder.

Signature

parameter default_app Protocol::HTTP::Middleware

The default application used when the block does not call run.

parameter block Proc

The middleware builder block.

returns Application

The protocol-facing Utopia application.

Implementation

def self.build(default_app = Response::NotFound, &block)
	builder = Protocol::HTTP::Middleware::Builder.new(default_app)
	
	if block
		if block.arity.zero?
			builder.instance_exec(&block)
		else
			block.call(builder)
		end
	end
	
	return self.new(builder.to_app)
end

def self.default

Build the default Utopia application.

Signature

returns Application

The default protocol-facing Utopia application.

Implementation

def self.default
	self.build
end

def self.load(path = PATH, **options)

Load a Utopia application from a conventional configuration file.

If the file defines an Application constant, it will be returned directly. If the constant is a class, it will be instantiated. If the file does not exist, or does not define Application, the default application is returned.

Signature

parameter path String

The application configuration path.

parameter options Hash

Options passed to the application constructor.

returns Protocol::HTTP::Middleware

The loaded protocol-facing application.

Implementation

def self.load(path = PATH, **options)
	if File.exist?(path)
		top = Module.new
		top.class_eval(File.read(path), path)
		
		if top.const_defined?(:Application, false)
			application = top.const_get(:Application)
			
			if application.is_a?(Class)
				return application.new(**options)
			else
				return application
			end
		end
	end
	
	return self.default
end

def call(request)

Process a protocol HTTP request.

Signature

parameter request Protocol::HTTP::Request

The incoming protocol request.

returns Protocol::HTTP::Response

The normalized protocol response.

Implementation

def call(request)
	request = Request.new(request)
	
	return Response.wrap(super(request))
end