@@ -19,6 +19,8 @@ mutable struct myresponse
1919 body:: String
2020end
2121
22+ my_headers= HTTP. mkheaders ([" Content-Type" => " text/plain" ])
23+
2224cors= false :: Bool
2325root= pwd ()
2426if root[end ]== ' /'
@@ -50,12 +52,8 @@ function _body(data::Array{UInt8,1},format::SubString{String})
5052end
5153
5254function File (file:: String )
53- try
5455 path = normpath (root, file)
5556 return String (read (path))
56- catch
57- return file
58- end
5957end
6058
6159function 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)
185165end
0 commit comments