require "http/server" require "./config" require "./model" require "./handlers/*" module Guff module Handlers HANDLERS = [{ # TODO: add production error handler env: %w{development}, init: ->(models : Models) { HTTP::ErrorHandler.new as HTTP::Handler }, }, { init: ->(models : Models) { HTTP::LogHandler.new as HTTP::Handler }, }, { init: ->(models : Models) { HTTP::DeflateHandler.new as HTTP::Handler }, }, { init: ->(models : Models) { GuffStuffHandler.new(models) as HTTP::Handler }, }, { env: %w{development}, init: ->(models : Models) { SessionHandler.new(models) as HTTP::Handler }, }, { env: %w{development}, init: ->(models : Models) { TestAuthHandler.new(models) as HTTP::Handler }, }, { env: %w{development}, init: ->(models : Models) { TestBlogHandler.new(models) as HTTP::Handler }, }, { init: ->(models : Models) { APIHandler.new(models) as HTTP::Handler }, }, { init: ->(models : Models) { BlogHandler.new(models) as HTTP::Handler }, }, { init: ->(models : Models) { HTTP::StaticFileHandler.new(models.config["public"]) as HTTP::Handler }, }, { init: ->(models : Models) { NotFoundHandler.new(models) as HTTP::Handler }, }] def self.get(models : Models) : Array(HTTP::Handler) HANDLERS.select { |h| if h.has_key?(:env) (h[:env] as Array(String)).includes?(models.config["environment"]) else true end }.map { |h| init = (h[:init] as Proc(Models, HTTP::Handler)) init.call(models) } end end end