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