Skip to content

Commit 9ea33b5

Browse files
committed
CORS
1 parent 25ba387 commit 9ea33b5

File tree

3 files changed

+79
-19
lines changed

3 files changed

+79
-19
lines changed

README.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,6 @@ server.start("localhost", 8080)
6767

6868
```
6969

70-
Features available in the development version
71-
------------------
72-
* Place a message in case of not finding a resource:
73-
```julia
74-
server.notfound("<!DOCTYPE html>
75-
<html>
76-
<head><title>Not found</title></head>
77-
<body><h1>404, Not found</h1></body>
78-
</html>")
79-
```
80-
```julia
81-
server.notfound("notfound.html")
82-
```
83-
84-
8570
Features available in the current release
8671
------------------
8772
###Parameters dictionary
@@ -178,10 +163,24 @@ Possible values of load
178163
"" Any file, Default
179164
```
180165

166+
### Not found message
167+
```julia
168+
server.notfound("<!DOCTYPE html>
169+
<html>
170+
<head><title>Not found</title></head>
171+
<body><h1>404, Not found</h1></body>
172+
</html>")
173+
```
174+
```julia
175+
server.notfound("notfound.html")
176+
```
177+
###CORS
178+
```julia
179+
server.use("CORS")
180+
```
181+
181182
### Bonus
182183
If you forgot the MIME type of a file you can use the next instruction
183184
```julia
184185
r.headers["Content-Type"]=mimetypes["file extension"]
185186
```
186-
187-

examples/CORS.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Merly
2+
3+
global u
4+
u="hello"
5+
6+
server = Merly.app()
7+
server.use("CORS")
8+
9+
@page "/" "Hello World!"
10+
@page "/hola/:usr" "<b>Hello {{usr}}!</b>"
11+
12+
@route GET "/get/:data" begin
13+
"get this back: {{data}}"
14+
end
15+
16+
@route POST "/post" begin
17+
"I did something!"
18+
end
19+
20+
@route POST|PUT|DELETE "/" begin
21+
println("params: ",q.params)
22+
println("query: ",q.query)
23+
println("body: ",q.body)
24+
25+
r.headers["Content-Type"]="text/plain"
26+
27+
"I did something!"
28+
end
29+
30+
Get("/data", (q,r)->(begin
31+
r.headers["Content-Type"]="text/plain"
32+
"$u data"
33+
end))
34+
35+
36+
Post("/data", (q,r)->(begin
37+
println("params: ",q.params)
38+
println("query: ",q.query)
39+
println("body: ",q.body)
40+
r.headers["Content-Type"]="text/plain"
41+
global u="bye"
42+
"I did something!"
43+
end))
44+
45+
46+
server.start("localhost", 8080)

src/Merly.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ end
3838
type Fram
3939
notfound::Function
4040
start::Function
41+
use::Function
4142
end
4243

4344
function pag(url::ASCIIString,code::Function)
@@ -215,15 +216,19 @@ function process(element::Merly.Pag,q,res::HttpCommon.Response,req::HttpCommon.R
215216

216217
if !(ismatch(Regex("GET"),req.method))
217218
#println("interpetando los Bytes de req.data como caracteres: ")
219+
global cors
218220
try
219221
body= _body(req.data,req.headers["Accept"])
220222
catch
221223
body= _body(req.data,"")
222224
end
223225
end
224226
#h["Content-Type"]="text/html"
227+
if cors
228+
res.headers["Access-Control-Allow-Origin"]="*"
229+
res.headers["Access-Control-Allow-Methods"]="POST,GET,OPTIONS"
230+
end
225231
res.status = 200
226-
println(element)
227232
#----------aqui escribe el programador-----------
228233
respond = element.code(q,res)
229234
#----------------------------------------------
@@ -269,6 +274,8 @@ function app(r=pwd()::AbstractString,load=""::AbstractString)
269274
global root
270275
global exten
271276
global q
277+
global cors
278+
cors=false
272279
root=r
273280
q=Q("","","")
274281
if root[end]=='/'
@@ -290,6 +297,14 @@ if length(load)>0
290297
end
291298
cd(root)
292299

300+
function use(y::AbstractString)
301+
if(y=="CORS")
302+
cors=true
303+
else
304+
cors=false
305+
end
306+
end
307+
293308
function notfound(x::AbstractString)
294309
global nfmessage
295310
nfmessage=x
@@ -320,6 +335,6 @@ cd(root)
320335
"only IPv4 addresses"
321336
end
322337
end
323-
return Fram(notfound,start)
338+
return Fram(notfound,start,use)
324339
end
325340
end # module

0 commit comments

Comments
 (0)