/
dsl.cr
171 lines (145 loc) · 4.41 KB
/
dsl.cr
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
module Artanis
# TODO: error(code, &block) macro to install handlers for returned statuses
module DSL
FIND_PARAM_NAME = /:([\w\d_]+)/
{% for method in %w(head options get post put patch delete) %}
macro {{ method.id }}(path, &block)
match {{ method }}, \{\{ path }} do \{\{ (block.args.empty? ? "" : "|#{block.args.argify}|").id }}
\{\{ yield }}
end
end
{% end %}
macro match(method, path, &block)
gen_match "match", {{ method }}, {{ path }}, {{ block }}
end
macro before(path = "*", &block)
gen_match "before", nil, {{ path }}, {{ block }}
end
macro after(path = "*", &block)
gen_match "after", nil, {{ path }}, {{ block }}
end
# TODO: use __match_000000 routes to more easily support whatever in routes (?)
# TODO: match regexp routes
# TODO: add conditions on routes
#
# OPTIMIZE: split the route in segments (?) would help to avoid double gsub (?)
macro gen_match(type, method, path, &block)
{%
prepared = path
.gsub(/\*[^\/.()]+/, "_SPLAT_")
.gsub(/\*/, "_ANY_")
.gsub(/:[^\/.()]+/, "_PARAM_")
.gsub(/\./, "_DOT_")
.gsub(/\//, "_SLASH_")
.gsub(/\(/, "_LPAREN_")
.gsub(/\)/, "_RPAREN_")
matcher = prepared
.gsub(/_SPLAT_/, "(.*?)")
.gsub(/_ANY_/, ".*?")
.gsub(/_PARAM_/, "([^\\/]+)")
.gsub(/_DOT_/, "\\.")
.gsub(/_SLASH_/, "\\/")
#.gsub(/_LPAREN_(.+?)_RPAREN_/, "(?:\1)")
.gsub(/_LPAREN_/, "(?:")
.gsub(/_RPAREN_/, ")?")
method_name = prepared
.gsub(/-/, "_MINUS_")
method_name = "#{method.upcase.id}_#{method_name.id}" if method
%}
{{ type.upcase.id }}_{{ method_name.upcase.id }} = /\A{{ matcher.id }}\Z/
def {{ type.id }}_{{ method_name.id }}(matchdata)
{{ path }}
.scan(FIND_PARAM_NAME)
.each_with_index do |m, i|
if value = matchdata[i + 1]?
@params[m[1]] = value
end
end
{% for arg, index in block.args %}
{{ arg.id }} = matchdata[{{ index + 1 }}]?
{% end %}
res = {{ yield }}
{% if type == "match" %}
if res.is_a?(Int)
status res
else
body res
end
{% end %}
# see https://github.com/manastech/crystal/issues/821
:ok
end
end
macro call_action(method_name)
if %m = URI.unescape(request.path || "/").match({{ method_name.upcase.id }})
%ret = {{ method_name.id }}(%m)
{% if method_name.starts_with?("match_") %}
break unless %ret == :pass
{% end %}
end
end
macro call_method(method)
{% method_names = @type.methods.map(&.name.stringify) %}
{{
method_names
.select(&.starts_with?("before_"))
.map { |method_name| "call_action #{ method_name }" }
.join("\n ")
.id
}}
while 1
{{
method_names
.select(&.starts_with?("match_#{method.id}"))
.map { |method_name| "call_action #{ method_name }" }
.join("\n ")
.id
}}
no_such_route
break
end
{{
method_names
.select(&.starts_with?("after_"))
.map { |method_name| "call_action #{ method_name }" }
.join("\n ")
.id
}}
end
# OPTIMIZE: build a tree from path segments (?)
macro def call : HTTP::Response
case request.method.upcase
{{
@type.methods
.map(&.name.stringify)
.select(&.starts_with?("match_"))
.map { |method_name| method_name.split("_")[1] }
.uniq
.map { |method| "when #{method}\n call_method(#{method})" }
.join("\n ")
.id
}}
else
no_such_route
end
response
end
ALWAYS_TRUE = true
macro halt(code_or_message = 200)
{% if code_or_message.is_a?(NumberLiteral) %}
status {{ code_or_message }}.to_i
{% else %}
body {{ code_or_message }}.to_s
{% end %}
return :halt if ALWAYS_TRUE
end
macro halt(code, message)
status {{ code }}.to_i
body {{ message }}.to_s
return :halt if ALWAYS_TRUE
end
macro pass
return :pass if ALWAYS_TRUE
end
end
end