aboutsummaryrefslogtreecommitdiff
path: root/src/guff/handlers.cr
blob: e57367318d248f015ddc5e45159a75f77b0e3968 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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) {
        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