11__precompile__()
22module Merly
3- import HttpServer. mimetypes
43import Base.|
54
6- using HttpServer ,
5+ using Sockets ,
76 HttpCommon,
87 JSON,
8+ HTTP,
99 XMLDict
1010
11+ include(" mimetypes.jl" )
1112include(" routes.jl" )
1213include(" allformats.jl" )
1314include(" webserver.jl" )
1415
1516export app, @page, @route, GET,POST,PUT,DELETE,HEAD,OPTIONS,PATCH,Get,Post,Put,Delete
1617
1718cors= false :: Bool
18- debug= true :: Bool
1919root= pwd()
2020if root[end ]== ' /'
2121 root= root[1 : end - 1 ]
22- elseif is_windows () && root[end ]== ' \\ '
22+ elseif Sys . iswindows () && root[end ]== ' \\ '
2323 root= root[1 : end - 1 ]
2424end
2525
2626exten= " \"\" " :: AbstractString
2727
28- type Q
28+ mutable struct Data
2929 query:: Dict
3030 params:: Any
31- body:: AbstractString
32- notfound_message :: AbstractString
31+ body:: Any
32+ headers :: Dict
3333end
34+ global notfound_message = " NotFound" :: AbstractString
35+ global q= Data(Dict()," " ," " ,Dict())
3436
35- global q= Q(Dict(),Dict()," " ," NotFound" )
36-
37- type Fram
37+ mutable struct Fram
3838 notfound:: Function
3939 start:: Function
4040 useCORS:: Function
4141 webserverfiles:: Function
4242 webserverpath:: Function
4343end
4444
45- function _body(data:: Array{UInt8,1} ,format:: String )
46- content= " "
47- for i= 1 : length(data)
48- content*= " $(Char(data[i])) "
49- end
50- return getindex(formats, format)(content)
45+ function _body(data:: Array{UInt8,1} ,format:: SubString{String} )
46+ return getindex(formats, format)(String(data))
5147end
5248
5349function File(file:: String )
5450 try
5551 path = normpath(root, file)
56- return readstring( path)
52+ return String(read( path) )
5753 catch
5854 return file
5955 end
@@ -68,51 +64,56 @@ function resolveroute(ruta::String)
6864 end
6965end
7066
71- function processroute_pattern(searchroute:: String ,request:: HttpCommon.Request ,response:: HttpCommon.Response )
67+ function processroute_pattern(searchroute:: String ,request,response)
7268 q. params, _function = resolveroute(searchroute)
7369 respond = _function(q,request,response)
74- sal = matchall( Regex(" {{([a-z])+}}" ),respond)
70+ sal = collect((m . match for m = eachmatch( Regex(" {{([a-z])+}}" ), respond)) )
7571 for i in sal
76- respond = replace(respond,Regex(i), q. params[" $(i[3 : end - 2 ]) " ])
72+ respond = replace(respond,Regex(i) => q. params[" $(i[3 : end - 2 ]) " ])
7773 end
74+ response. status = 200
7875 return respond
7976end
8077
81- function handler(request:: HttpCommon.Request ,response:: HttpCommon.Response )
82-
83- data = split(request. resource," ?" )
78+ function handler(request:: HTTP.Messages.Request )
79+ data = split(request. target," ?" )
8480 url= data[1 ]
85-
8681 searchroute = request. method* url
87-
8882 try
89- q. query= parsequerystring(data[2 ]);
83+ q. query= HTTP. queryparams(data[2 ]);
84+ catch
9085 end
86+ response = HTTP. Response()
9187
9288 try
93- q. body = _body(request. data,request . headers[ " Content-Type" ] )
89+ q. body= _body(request. body,HTTP . header(request, " Content-Type" ) )
9490 catch
95- q. body = _body(request. data, " */*" )
91+ q. body = _body(request. body,SubString( " */*" ) )
9692 end
9793
9894 if cors
99- response . headers[ " Access-Control-Allow-Origin" ] = " *"
100- response . headers[ " Access-Control-Allow-Methods" ] = " POST,GET,OPTIONS"
95+ HTTP . setheader(response, " Access-Control-Allow-Origin" => " *" )
96+ HTTP . setheader(response, " Access-Control-Allow-Methods" => " POST,GET,OPTIONS" )
10197 end
10298
103- if debug
104- info( " METODO : " ,request . method, " URL : " ,url)
105- end
106- try
107- response. data = getindex(routes, searchroute)(q,request,response)
108- catch
99+ HTTP . setheader(response, " Content-Type " => " text/plain " )
100+
101+ # try
102+ response . status = 200
103+ response. body = getindex(routes, searchroute)(q,request,response)
104+ #= catch
109105 try
110- response. data = processroute_pattern(searchroute,request,response)
106+ response.body = processroute_pattern(searchroute,request,response)
111107 catch
112- response. data = getindex(routes, " notfound" )(q,request,response)
108+ response.body = getindex(routes, "notfound")(q,request,response)
113109 end
110+ end=#
111+
112+ for (key, value) in q. headers
113+ HTTP. setheader(response,key => value )
114114 end
115115
116+
116117 return response
117118end
118119
@@ -121,14 +122,13 @@ function app()
121122global root
122123global exten
123124global cors
124- global debug
125125
126126 function useCORS(activate:: Bool )
127127 cors= activate
128128 end
129129
130130 function notfound(text:: AbstractString )
131- q . notfound_message= File(text)
131+ notfound_message= File(text)
132132 end
133133
134134 function webserverfiles(load:: AbstractString )
@@ -144,48 +144,40 @@ global debug
144144 root= path
145145 end
146146
147- function start(config= Dict(" host" => " 127.0.0.1" ," port" => 8000 , " debug " => true ):: Dict )
148- host= " 127.0.0.1"
147+ function start(config= Dict(" host" => " 127.0.0.1" ," port" => 8000 ):: Dict )
148+ host= Sockets . IPv4( " 127.0.0.1" )
149149 port= 8000
150150
151151 try
152- host= get(config, " host" , " 127.0.0.1" ):: AbstractString
152+ host= Sockets . IPv4( get(config, " host" , " 127.0.0.1" ):: AbstractString )
153153 catch
154- error(" Verify the format of the ip address \n AbstractString \" 127.0.0.1\" " )
154+ try
155+ host= Sockets. IPv6(get(config, " host" , " 127.0.0.1" ):: AbstractString )
156+ catch
157+ end
155158 end
156159
157160 try
158161 port= get(config, " port" , 8000 ):: Int
159162 catch
160- error( " Verify the port format \n Int 8000 " )
163+ @info( " Port 8000 " )
161164 end
162165
163- try
164- debug= get(config, " debug" , true ):: Bool
165- catch
166- error(" Verify the debug format \n Bool true " )
167- end
166+ http = (req)-> handler(req)
168167
169- http = HttpHandler((req, res)-> handler(req,res))
170- http. events[" error" ] = (client, error) -> println(error)
171- http. events[" listen" ] = (port) -> println(" Listening on $port ..." )
172- server = Server(http)
168+ myserver= HTTP. Servers. Server(http, stdout )
173169
174- if host== " localhost"
175- host= " 127.0.0.1"
176- end
177170 try
178171 # @async run(server, host=IPv4(host), port=port)
179- run(server , host= IPv4(host), port = port)
172+ HTTP . Servers . serve(myserver , host, port)
180173 catch
181174 try
182- run(server , host= IPv6(host), port = port)
175+ HTTP . Servers . serve(myserver , host, port)
183176 catch
184- warn(" Address not valid, check it" )
177+ @ warn(" Address not valid, check it" )
185178 end
186179 end
187180 end
188-
189181 return Fram(notfound,start,useCORS,webserverfiles,webserverpath)
190182end
191183end # module
0 commit comments