Skip to content

Commit bdac8dc

Browse files
committed
eliminating trys and add tests
1 parent 0c9c4f1 commit bdac8dc

8 files changed

Lines changed: 99 additions & 96 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ Below are some of the features that are planned to be added in future versions o
4444
Installing
4545
----------
4646
```julia
47-
Pkg.add("Merly") #Release
48-
Pkg.clone("git://github.com/codeneomatrix/Merly.jl.git") #Development
47+
```julia
48+
Pkg> add Diana #Release
49+
pkg> add Diana#master #Development
4950
```
5051

5152
## Example

src/Merly.jl

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ mutable struct myresponse
1919
body::String
2020
end
2121

22+
my_headers= HTTP.mkheaders(["Content-Type" => "text/plain"])
23+
2224
cors=false::Bool
2325
root=pwd()
2426
if root[end]=='/'
@@ -50,12 +52,8 @@ function _body(data::Array{UInt8,1},format::SubString{String})
5052
end
5153

5254
function File(file::String)
53-
try
5455
path = normpath(root, file)
5556
return String(read(path))
56-
catch
57-
return file
58-
end
5957
end
6058

6159
function resolveroute(ruta::String)
@@ -82,36 +80,32 @@ function handler(request::HTTP.Messages.Request)
8280
data = split(request.target,"?")
8381
url=data[1]
8482
searchroute = request.method*url
85-
try
86-
q.query= HTTP.queryparams(data[2]);
87-
catch
88-
end
83+
84+
if (length(data)>1) q.query= HTTP.queryparams(data[2]) end
8985

9086
response = myresponse(200,Dict(),"")
9187

92-
header_content_type = HTTP.header(request, "Content-Type")
93-
if(length(header_content_type)>0)
94-
q.body= _body(request.body,header_content_type)
95-
else
96-
q.body = _body(request.body,SubString("*/*"))
88+
if ((request.method=="POST" )||(request.method=="PUT" )||(request.method=="PATCH"))
89+
header_content_type = HTTP.header(request, "Content-Type")
90+
if(length(header_content_type)>0)
91+
q.body= getindex(formats, header_content_type)(String(request.body))
92+
else
93+
q.body = getindex(formats, "*/*")(String(request.body))
94+
end
9795
end
9896

99-
if (haskey(routes, searchroute))
97+
if (searchroute in routes_array)
10098
response.body = getindex(routes, searchroute)(q,request,response)
10199
else
102-
try
103-
response.body = processroute_pattern(searchroute,request,response)
104-
catch
100+
#try
101+
# response.body = processroute_pattern(searchroute,request,response)
102+
#catch
105103
response.body = getindex(routes, "notfound")(q,request,response)
106-
end
104+
#end
107105
end
108106

109-
responsefinal = HTTP.Response(response.status,response.body)
110-
if cors
111-
HTTP.setheader(responsefinal,"Access-Control-Allow-Origin" => "*")
112-
HTTP.setheader(responsefinal,"Access-Control-Allow-Methods" => "POST,GET,OPTIONS")
113-
end
114-
HTTP.setheader(responsefinal,"Content-Type" => "text/plain" )
107+
responsefinal = HTTP.Response(response.status,my_headers, body=response.body)
108+
115109
for (key, value) in response.headers
116110
HTTP.setheader(responsefinal,key => value )
117111
end
@@ -126,11 +120,17 @@ function app()
126120
global cors
127121

128122
function useCORS(activate::Bool)
129-
cors=activate
123+
HTTP.setheader(my_headers,"Access-Control-Allow-Origin" => "*")
124+
HTTP.setheader(my_headers,"Access-Control-Allow-Methods" => "POST,GET,OPTIONS")
125+
return true
130126
end
131127

132-
function notfound(text::AbstractString)
128+
function notfound(text::String)
129+
if occursin(".html", text)
133130
notfound_message= File(text)
131+
else
132+
notfound_message= text
133+
end
134134
end
135135

136136
function webserverfiles(load::AbstractString)
@@ -148,38 +148,18 @@ function app()
148148

149149
function start(config=Dict("host" => "127.0.0.1","port" => 8000)::Dict)
150150
host= Sockets.IPv4("127.0.0.1")
151-
port= 8000
152-
153-
try
154-
host=Sockets.IPv4(get(config, "host", "127.0.0.1")::AbstractString)
155-
catch
156-
try
157-
host=Sockets.IPv6(get(config, "host", "127.0.0.1")::AbstractString)
158-
catch
159-
end
160-
end
151+
port=get(config, "port", 8000)::Int
161152

162-
try
163-
port=get(config, "port", 8000)::Int
164-
catch
165-
@info("Port 8000 ")
166-
end
153+
my_host = get(config, "host", "127.0.0.1")::String
154+
if ('.' in my_host) host=Sockets.IPv4(my_host) end
155+
if (':' in my_host) host=Sockets.IPv6(my_host) end
167156

168157
http = (req)-> handler(req)
169-
170158
myserver= HTTP.Servers.Server(http, stdout)
171-
172-
try
173-
#@async run(server, host=IPv4(host), port=port)
174-
return HTTP.Servers.serve(myserver, host, port)
175-
catch
176-
try
177-
return HTTP.Servers.serve(myserver, host, port)
178-
catch
179-
@warn("Address not valid, check it")
180-
end
181-
end
159+
@info("Listening on: $(host) : $(port)")
160+
return HTTP.Servers.serve(myserver, host, port)
182161
end
162+
183163
@info("App created")
184164
return Fram(notfound,start,useCORS,webserverfiles,webserverpath)
185165
end

src/allformats.jl

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,17 @@ function toplanetext(data::String)
44
end
55

66
function tojson(data::String)
7-
try
87
return JSON.parse(data)
9-
catch
10-
@warn("The format JSON does not match the data received")
11-
return data
12-
end
138
end
149

1510
function toxml(data::String)
16-
try
1711
return parse_xml(content)
18-
catch
19-
@warn("The format XML does not match the data received")
20-
return data
21-
end
2212
end
2313

2414

2515
formats = Dict(
2616
"application/json" => tojson
27-
, "application/xml" => toxml
17+
#, "application/xml" => toxml
2818
, "*/*" => toplanetext
2919
, "text/plain" => toplanetext
3020
)

src/routes.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,20 @@ function NotFound(q,req,res)
1515
end
1616

1717
routes=Dict()
18+
routes_array=[]
1819
routes_patterns=Dict()
1920
routes["notfound"] = NotFound
2021

2122
function createurl(url::String,funtion::Function)
2223
if occursin(":",url)||occursin("(",url)
23-
try
2424
url_ = "^"*url*"\$"
2525
url_ = replace(url_,":" => "(?<")
2626
url_ = replace(url_,">" => ">[a-z]+)")
2727
routes_patterns[Regex(url_)] = funtion
2828
@info("Url added",Regex(url_))
29-
catch
30-
@warn("Error in the format of the route $url, verify it\n \"VERB/get/:data>\" \n \"VERB/get/([0-9])\"")
31-
end
3229
else
3330
routes[url] = funtion
31+
push!(routes_array,url)
3432
@info("Url added",url)
3533
end
3634
end

src/webserver.jl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ function files(arch::Array{Any,1})
44
for i=1:length(arch)
55
roop=replace(replace(arch[i],root => ""),"\\" => "/")
66
extension="text/plain"
7-
try
8-
extension=mimetypes[split(roop,".")[end]]
9-
catch
10-
end
7+
ext= split(roop,".")
8+
if(length(ext)>1) extension=mimetypes[ext[2]] end
119
data = File(roop[2:end])
12-
#try
13-
createurl("GET"*roop,(q,req,res)->(begin
14-
HTTP.setheader(res,"Content-Type" => extension)
15-
res.status = 200
16-
res.body= data
17-
end))
18-
#end
10+
createurl("GET"*roop,(q,req,res)->(begin
11+
res.headers["Content-Type"]= extension
12+
res.status = 200
13+
res.body= data
14+
end))
1915
end
2016
end
2117

test/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Document</title>
6+
</head>
7+
<body>
8+
<h1>hola</h1>
9+
</body>
10+
</html>

test/prueba.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
probando webserver

test/runtests.jl

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ using Merly
22
using Test
33
using HTTP
44
using JSON
5+
#using Pkg
6+
#Pkg.add("BenchmarkTools")
7+
#using BenchmarkTools
8+
9+
ip = "127.0.0.1" #127.0.0.1
510
# write your own tests here
611
server = Merly.app()
712

@@ -17,7 +22,6 @@ server = Merly.app()
1722
<body><h1>404, Not found</h1></body>
1823
</html>"
1924

20-
2125
u="hello"
2226

2327
@page "/" "Hello World!"
@@ -43,10 +47,11 @@ end
4347

4448
Get("/data", (q,req,res)->(begin
4549
res.headers["Content-Type"]= "text/plain"
50+
println("params: ",q.params)
51+
println("query: ",q.query)
4652
u*"data"
4753
end))
4854

49-
5055
Post("/data", (q,req,res)->(begin
5156
println("params: ",q.params)
5257
println("query: ",q.query)
@@ -56,49 +61,71 @@ Post("/data", (q,req,res)->(begin
5661
"I did something!"
5762
end))
5863

59-
t = @async server.start(Dict("host" => "127.0.0.1","port" => 8000))
64+
server.webserverfiles("jl")
65+
66+
@async server.start(Dict("host" => "$(ip)","port" => 8000))
6067

6168

62-
r = HTTP.get("http://127.0.0.1:8000/")
69+
r = HTTP.get("http://$(ip):8000/")
6370
@test r.status == 200
6471
@test String(r.body) == "Hello World!"
6572

66-
r = HTTP.get("http://127.0.0.1:8000/hola/usuario")
73+
#=r = HTTP.get("http://$(ip):8000/hola/usuario")
6774
@test r.status == 200
6875
@test String(r.body) == "<b>Hello usuario!</b>"
6976
70-
r = HTTP.get("http://127.0.0.1:8000/get/testdata")
77+
r = HTTP.get("http://$(ip):8000/get/testdata")
7178
@test r.status == 200
7279
@test String(r.body) == "get this back: testdata"
73-
74-
r = HTTP.get("http://127.0.0.1:8000/data")
80+
=#
81+
r = HTTP.get("http://$(ip):8000/data?hola=1")
7582
@test r.status == 200
7683
@test String(r.body) == "hellodata"
7784

7885
myjson = Dict("query"=>"data")
7986
my_headers = HTTP.mkheaders(["Accept" => "application/json","Content-Type" => "application/json"])
80-
r = HTTP.post("http://127.0.0.1:8000/data",my_headers,JSON.json(myjson))
87+
r = HTTP.post("http://$(ip):8000/data",my_headers,JSON.json(myjson))
8188
@test r.status == 200
8289
@test String(r.body) == "I did something!"
8390

84-
r = HTTP.get("http://127.0.0.1:8000/data")
91+
r = HTTP.get("http://$(ip):8000/data")
8592
@test r.status == 200
8693
@test String(r.body) == "byedata"
8794

8895

8996

90-
r = HTTP.post("http://127.0.0.1:8000/post",my_headers,JSON.json(myjson))
97+
r = HTTP.post("http://$(ip):8000/post",my_headers,JSON.json(myjson))
9198
@test r.status == 200
9299
@test String(r.body) == "I did something!"
93100

94-
r = HTTP.post("http://127.0.0.1:8000/",my_headers,JSON.json(myjson))
101+
r = HTTP.post("http://$(ip):8000/",my_headers,JSON.json(myjson))
95102
@test r.status == 200
96103
@test String(r.body) == "I did something!"
97104

98-
r = HTTP.put("http://127.0.0.1:8000/",my_headers,JSON.json(myjson))
105+
r = HTTP.put("http://$(ip):8000/",my_headers,JSON.json(myjson))
99106
@test r.status == 200
100107
@test String(r.body) == "I did something!"
101108

102-
r = HTTP.delete("http://127.0.0.1:8000/")
109+
r = HTTP.delete("http://$(ip):8000/")
103110
@test r.status == 200
104-
@test String(r.body) == "I did something!"
111+
@test String(r.body) == "I did something!"
112+
@test r.headers == Pair{SubString{String},SubString{String}}["Content-Type"=>"text/plain", "Access-Control-Allow-Origin"=>"*", "Access-Control-Allow-Methods"=>"POST,GET,OPTIONS", "Transfer-Encoding"=>"chunked"]
113+
114+
try
115+
r = HTTP.get("http://$(ip):8000/nada")
116+
catch e
117+
@test e.status == 404
118+
@test String(e.response.body) == "NotFound"
119+
end
120+
121+
r = HTTP.get("http://$(ip):8000/prueba.txt")
122+
@test r.status == 200
123+
@test String(r.body) == "probando webserver"
124+
@test r.headers == Pair{SubString{String},SubString{String}}["Content-Type"=>"text/plain", "Access-Control-Allow-Origin"=>"*", "Access-Control-Allow-Methods"=>"POST,GET,OPTIONS", "Transfer-Encoding"=>"chunked"]
125+
126+
r = HTTP.get("http://$(ip):8000/index.html")
127+
@test r.status == 200
128+
@test String(r.body) == "<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n\t<meta charset=\"UTF-8\">\r\n\t<title>Document</title>\r\n</head>\r\n<body>\r\n<h1>hola</h1>\r\n</body>\r\n</html>"
129+
@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"]
130+
131+
#@btime HTTP.get("http://$(ip):8000/?hola=5")

0 commit comments

Comments
 (0)