@@ -18,29 +18,7 @@ mutable struct myrequest
1818 end
1919end
2020
21- struct App
22- notfound:: Function
23- start:: Function
24- useCORS:: Function
25- webserverfiles:: Function
26- webserverpath:: Function
27- end
28-
2921my_headers= HTTP. mkheaders ([" Content-Type" => " text/plain" ])
30- root= pwd ()
31- if root[end ]== ' /'
32- root= root[1 : end - 1 ]
33- elseif Sys. iswindows () && root[end ]== ' \\ '
34- root= root[1 : end - 1 ]
35- end
36- exten= " \"\" " :: AbstractString
37- notfound_message = " NotFound" :: String
38-
39-
40- function File (file:: String )
41- path = normpath (root, file)
42- return String (read (path))
43- end
4422
4523function searchroute_regex (ruta:: String )
4624 for i in 1 : length (routes_patterns_array)
@@ -96,50 +74,108 @@ function handler(request::HTTP.Messages.Request)
9674end
9775
9876
99- function app ()
100- global exten
77+ struct app
78+ notfound:: Function
79+ start:: Function
80+ useCORS:: Function
81+ webserverfiles:: Function
82+ webserverpath:: Function
83+ headersalways:: Function
10184
102- function useCORS (activate:: Bool )
103- HTTP. setheader (my_headers," Access-Control-Allow-Origin" => " *" )
104- HTTP. setheader (my_headers," Access-Control-Allow-Methods" => " POST,GET,OPTIONS" )
105- return true
106- end
85+ function app ()
86+ root= pwd ()
10787
108- function notfound (text:: String )
109- if occursin (" .html" , text)
110- notfound_message= File (text)
111- else
112- notfound_message= text
88+ function File (file:: String )
89+ path = normpath (root, file)
90+ return String (read (path))
11391 end
114- end
11592
116- function webserverfiles (load:: AbstractString )
117- if load== " *"
118- WebServer (root)
119- else
120- exten= load:: AbstractString
121- WebServer (root)
93+ function files (arch:: Array{Any,1} )
94+ roop= " "
95+ for i= 1 : length (arch)
96+ roop= replace (replace (arch[i],root => " " )," \\ " => " /" )
97+ extension= " text/plain"
98+ ext= split (roop," ." )
99+ if (length (ext)> 1 )
100+ my_extension = ext[2 ]
101+ if (haskey (mimetypes,my_extension))
102+ extension= mimetypes[my_extension]
103+ end
104+ end
105+ data = File (roop[2 : end ])
106+ createurl (" GET" * roop,(req,res)-> (begin
107+ res. headers[" Content-Type" ]= extension
108+ res. status = 200
109+ res. body= data
110+ end ))
111+ end
122112 end
123- end
124113
125- function webserverpath (path:: AbstractString )
126- root= path
127- end
114+ function WebServer (rootte:: String ,exten:: String )
115+ cd (rootte)
116+ ls= readdir ()
117+ arrfile= []
118+ arrdir= []
119+ for i= 1 : length (ls)
120+ if isfile (ls[i])
121+ if ( (occursin (Regex (" ((.)*\\ .(?!($exten )))" ),ls[i])) && (! occursin (r" ^(\. )" ,ls[i])))
122+ push! (arrfile,normpath (rootte,ls[i]))
123+ end
124+ end
125+ if isdir (ls[i])
126+ push! (arrdir,normpath (rootte,ls[i]))
127+ end
128+ end
129+ files (arrfile)
130+ for i= 1 : length (arrdir)
131+ WebServer (arrdir[i],exten)
132+ end
133+ end
128134
129- function start (config= Dict (" host" => " 127.0.0.1" ," port" => 8000 ):: Dict )
130- host= Sockets. IPv4 (" 127.0.0.1" )
131- port= get (config, " port" , 8000 ):: Int
132- my_host = get (config, " host" , " 127.0.0.1" ):: String
133- if (' .' in my_host) host= Sockets. IPv4 (my_host) end
134- if (' :' in my_host) host= Sockets. IPv6 (my_host) end
135- http = (req)-> handler (req)
136- myserver= HTTP. Servers. Server (http, stdout )
137- @info (" Listening on: $(host) : $(port) " )
138- return HTTP. Servers. serve (myserver, host, port)
139- end
135+ function useCORS (activate:: Bool )
136+ HTTP. setheader (my_headers," Access-Control-Allow-Origin" => " *" )
137+ HTTP. setheader (my_headers," Access-Control-Allow-Methods" => " POST,GET,OPTIONS" )
138+ return true
139+ end
140140
141- @info (" App created" )
142- return App (notfound,start,useCORS,webserverfiles,webserverpath)
143- end
141+ function headersalways (head:: AbstractString ,value:: AbstractString )
142+ HTTP. setheader (my_headers,head => value)
143+ end
144+
145+ function notfound (text:: String )
146+ if occursin (" .html" , text)
147+ notfound_message= File (text)
148+ addnotfound (notfound_message)
149+ else
150+ addnotfound (text)
151+ end
152+ end
153+
154+ function webserverfiles (load:: AbstractString )
155+ if load== " *"
156+ WebServer (root," " )
157+ else
158+ WebServer (root,load)
159+ end
160+ end
144161
145- # HSTS HTTP.setheader(response,"Strict-Transport-Security" => "max-age=10886400; includeSubDomains; preload"
162+ function webserverpath (path:: AbstractString )
163+ root = path
164+ end
165+
166+ function start (config= Dict (" host" => " 127.0.0.1" ," port" => 8000 ):: Dict )
167+ host= Sockets. IPv4 (" 127.0.0.1" )
168+ port= get (config, " port" , 8000 ):: Int
169+ my_host = get (config, " host" , " 127.0.0.1" ):: String
170+ if (' .' in my_host) host= Sockets. IPv4 (my_host) end
171+ if (' :' in my_host) host= Sockets. IPv6 (my_host) end
172+ http = (req)-> handler (req)
173+ myserver= HTTP. Servers. Server (http, stdout )
174+ @info (" Listening on: $(host) : $(port) " )
175+ return HTTP. Servers. serve (myserver, host, port)
176+ end
177+
178+ @info (" App created" )
179+ return new (notfound,start,useCORS,webserverfiles,webserverpath,headersalways)
180+ end
181+ end
0 commit comments