diff options
Diffstat (limited to 'src/guff/handlers.cr')
-rw-r--r-- | src/guff/handlers.cr | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/guff/handlers.cr b/src/guff/handlers.cr new file mode 100644 index 0000000..4ebeae7 --- /dev/null +++ b/src/guff/handlers.cr @@ -0,0 +1,62 @@ +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 |