Skip to content

Commit e87ee0d

Browse files
committed
performance routes regex
1 parent 158cb30 commit e87ee0d

4 files changed

Lines changed: 28 additions & 29 deletions

File tree

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,20 @@ Below are some of the features that are planned to be added in future versions o
3030
#### Version 0.1.0
3131
- [x] Julia version 0.7 syntax update
3232

33-
#### Version 0.1.1
33+
#### Version 0.2.0
3434
- [x] Julia version 1.0 syntax update
3535
- [x] Update and refactor
3636

37-
#### Version 0.1.2
37+
#### Version 0.2.2
3838
- [ ] Implementation of a websocket module
3939

40-
#### Version 0.1.3
40+
#### Version 0.2.3
4141
- [ ] Performance improvement
4242

4343

4444
Installing
4545
----------
4646
```julia
47-
```julia
4847
Pkg> add Diana #Release
4948
pkg> add Diana#master #Development
5049
```

src/base.jl

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,13 @@ function File(file::String)
3434
return String(read(path))
3535
end
3636

37-
function resolveroute(ruta::String)
38-
for key in keys(routes_patterns)
39-
params= match(key,ruta)
40-
if params!= nothing
41-
return params, getindex(routes_patterns,key)
37+
function searchroute_regex(ruta::String)
38+
for i in 1:length(routes_patterns_array)
39+
if(occursin(routes_patterns_array[i],ruta))
40+
return routes_patterns_array[i]
4241
end
43-
end
44-
end
45-
46-
function processroute_pattern(searchroute::String,request,response)
47-
q.params, _function = resolveroute(searchroute)
48-
respond = _function(q,request,response)
49-
sal = collect((m.match for m = eachmatch(Regex("{{([a-z])+}}"), respond)))
50-
for i in sal
51-
respond = replace(respond,Regex(i) => q.params["$(i[3:end-2])"])
52-
end
53-
response.status = 200
54-
return respond
42+
end
43+
return ""
5544
end
5645

5746
function handler(request::HTTP.Messages.Request)
@@ -73,11 +62,18 @@ function handler(request::HTTP.Messages.Request)
7362
if (searchroute in routes_array)
7463
response.body = getindex(routes, searchroute)(q,request,response)
7564
else
76-
#try
77-
# response.body = processroute_pattern(searchroute,request,response)
78-
#catch
79-
response.body = getindex(routes, "notfound")(q,request,response)
80-
#end
65+
pattern = searchroute_regex(searchroute)
66+
if (typeof(pattern) <:Regex)
67+
q.params = match(pattern,searchroute)
68+
_function = getindex(routes_patterns,pattern)
69+
response.body = _function(q,request,response)
70+
sal = collect((m.match for m = eachmatch(Regex("{{([a-z])+}}"), response.body)))
71+
for i in sal
72+
response.body = replace(response.body,Regex(i) => q.params["$(i[3:end-2])"])
73+
end
74+
else
75+
response.body = getindex(routes, "notfound")(q,request,response)
76+
end
8177
end
8278
responsefinal = HTTP.Response(response.status,my_headers, body=response.body)
8379
for (key, value) in response.headers

src/routes.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ end
1717
routes=Dict()
1818
routes_array=[]
1919
routes_patterns=Dict()
20+
routes_patterns_array=[]
2021
routes["notfound"] = NotFound
2122

2223
function createurl(url::String,funtion::Function)
@@ -25,6 +26,7 @@ function createurl(url::String,funtion::Function)
2526
url_ = replace(url_,":" => "(?<")
2627
url_ = replace(url_,">" => ">[a-z]+)")
2728
routes_patterns[Regex(url_)] = funtion
29+
push!(routes_patterns_array,Regex(url_))
2830
@info("Url added",Regex(url_))
2931
else
3032
routes[url] = funtion

test/runtests.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ r = HTTP.get("http://$(ip):$(port)/")
7272
@test r.status == 200
7373
@test String(r.body) == "Hello World!"
7474

75-
#=r = HTTP.get("http://$(ip):$(port)/hola/usuario")
75+
r = HTTP.get("http://$(ip):$(port)/hola/usuario")
7676
@test r.status == 200
7777
@test String(r.body) == "<b>Hello usuario!</b>"
7878

7979
r = HTTP.get("http://$(ip):$(port)/get/testdata")
8080
@test r.status == 200
8181
@test String(r.body) == "get this back: testdata"
82-
=#
82+
8383
r = HTTP.get("http://$(ip):$(port)/data?hola=1")
8484
@test r.status == 200
8585
@test String(r.body) == "hellodata"
@@ -135,4 +135,6 @@ end
135135

136136
@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"]
137137

138-
#@btime HTTP.get("http://$(ip):$(port)/?hola=5")
138+
#@btime HTTP.get("http://$(ip):$(port)/?hola=5") # 3.864 ms (8304 allocations: 381.20 KiB)
139+
#@btime HTTP.get("http://$(ip):$(port)/hola/usuario") # 4.211 ms (7685 allocations: 353.44 KiB)
140+
#@btime r= HTTP.get("http://$(ip):$(port)/get/testdata") # 3.906 ms (7693 allocations: 353.78 KiB)

0 commit comments

Comments
 (0)