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
|
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
|