Skip to content

Commit a0a6e24

Browse files
committed
refactor urls regex and add test regular expression
1 parent e87ee0d commit a0a6e24

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,19 @@ Features available in the current release
102102
------------------
103103
### Parameters dictionary
104104
```julia
105+
# :data> does not accept special symbols (!,#,$,etc)
105106
@route GET "/get/:data>" begin
106107
# matches "GET /get/foo" and "GET /get/bar"
107108
# q.params["data"] is 'foo' or 'bar'
108109
"get this back: "*q.params["data"]
109110
end
111+
112+
# it is possible to use regular expressions, enclosing them always between '(' ')'
113+
@route GET "/regex/(\\w+\\d+)" begin
114+
# matches "GET /regex/test1" and "GET /regex/test125"
115+
# q.params[1] is 'test1' or 'test125'
116+
"datos $(q.params[1])"
117+
end
110118
```
111119
### url query dictionary
112120

src/routes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function createurl(url::String,funtion::Function)
2424
if occursin(":",url)||occursin("(",url)
2525
url_ = "^"*url*"\$"
2626
url_ = replace(url_,":" => "(?<")
27-
url_ = replace(url_,">" => ">[a-z]+)")
27+
url_ = replace(url_,">" => ">\\w+)")
2828
routes_patterns[Regex(url_)] = funtion
2929
push!(routes_patterns_array,Regex(url_))
3030
@info("Url added",Regex(url_))

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ u="hello"
3232
"get this back: {{data}}"
3333
end
3434

35+
@route GET "/regex/(\\w+\\d+)" begin
36+
"datos $(q.params[1])"
37+
end
38+
3539
@route POST "/post" begin
3640
res.body = "I did something!"
3741
end
@@ -135,6 +139,9 @@ end
135139

136140
@test r.headers == Pair{SubString{String},SubString{String}}["Content-Type"=>"text/html", "Access-Control-Allow-Origin"=>"*", "Access-Control-Allow-Methods"=>"POST,GET,OPTIONS", "Transfer-Encoding"=>"chunked"]
137141

142+
143+
r= HTTP.get("http://$(ip):$(port)/regex/test1")
144+
@test String(r.body) == "datos test1"
138145
#@btime HTTP.get("http://$(ip):$(port)/?hola=5") # 3.864 ms (8304 allocations: 381.20 KiB)
139146
#@btime HTTP.get("http://$(ip):$(port)/hola/usuario") # 4.211 ms (7685 allocations: 353.44 KiB)
140147
#@btime r= HTTP.get("http://$(ip):$(port)/get/testdata") # 3.906 ms (7693 allocations: 353.78 KiB)

0 commit comments

Comments
 (0)