Skip to content

Commit 19713c8

Browse files
authored
Also apply dark theme on 404 pages (#162)
* Also apply dark theme on 404 pages * follow indentation convention
1 parent 08bf742 commit 19713c8

File tree

1 file changed

+81
-64
lines changed

1 file changed

+81
-64
lines changed

src/server.jl

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function get_fs_path(
122122
silent::Bool=false,
123123
onlyfs::Bool=false
124124
)
125-
125+
126126
uri = HTTP.URI(req_path)
127127
r_parts = HTTP.URIs.unescapeuri.(split(lstrip(uri.path, '/'), '/'))
128128
fs_path = joinpath(CONTENT_DIR[], r_parts...)
@@ -208,70 +208,86 @@ Generate a page which lists content at path `dir`.
208208
"""
209209
function get_dir_list(dir::AbstractString)::String
210210
list = readdir(dir; join=true, sort=true)
211-
io = IOBuffer()
212211
sdir = dir
213212
cdir = CONTENT_DIR[]
214213
if !isempty(cdir)
215214
sdir = join([cdir, lstrip_cdir(dir)], "/")
216215
end
216+
pagehtml(title="Directory listing") do io
217+
write(io, """
218+
<h1 style='margin-top: 1em;'>
219+
Directory listing
220+
</h1>
221+
<h3>
222+
<a href="/" alt="root">🏠</a>
223+
<a href="/$(dirname(dir))" alt="parent dir">⬆️</a>
224+
&nbsp; path: <code style='color:gray;'>$(sdir)</code>
225+
</h3>
217226
227+
<hr>
228+
<ul>
229+
"""
230+
)
231+
232+
list_files = [f for f in list if isfile(f)]
233+
list_dirs = [d for d in list if d list_files]
234+
235+
for fname in list_files
236+
link = lstrip_cdir(fname)
237+
name = splitdir(fname)[end]
238+
post = ifelse(islink(fname), " @", "")
239+
write(io, """
240+
<li><a href="/$(link)">$(name)$(post)</a></li>
241+
"""
242+
)
243+
end
244+
for fdir in list_dirs
245+
link = lstrip_cdir(fdir)
246+
# ensure ends with slash, see #135
247+
link *= ifelse(endswith(link, "/"), "", "/")
248+
name = splitdir(fdir)[end]
249+
pre = "📂 "
250+
post = ifelse(islink(fdir), " @", "")
251+
write(io, """
252+
<li><a href="/$(link)">$(pre)$(name)$(post)</a></li>
253+
"""
254+
)
255+
end
256+
write(io, """
257+
</ul>
258+
<hr>
259+
<a href="https://github.com/tlienart/LiveServer.jl">
260+
💻 LiveServer.jl
261+
</a>
262+
</body>
263+
</html>
264+
"""
265+
)
266+
end
267+
end
268+
269+
function pagehtml(f::Base.Callable; title::AbstractString)
270+
io = IOBuffer()
271+
# Construct the shared head part of the HTML
218272
write(io, """
219273
<!DOCTYPE HTML>
220274
<html>
221275
<head>
222276
<meta charset="utf-8">
223277
<meta name="viewport" content="width=device-width, initial-scale=1">
224278
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/spcss">
225-
<title>Directory listing</title>
279+
<title>$(title)</title>
226280
<style>
227281
a {text-decoration: none;}
228282
</style>
229283
</head>
230-
<body>
231-
<h1 style='margin-top: 1em;'>
232-
Directory listing
233-
</h1>
234-
<h3>
235-
<a href="/" alt="root">🏠</a>
236-
<a href="/$(dirname(dir))" alt="parent dir">⬆️</a>
237-
&nbsp; path: <code style='color:gray;'>$(sdir)</code>
238-
</h3>
239-
240-
<hr>
241-
<ul>
284+
<body>
242285
"""
243286
)
244-
245-
list_files = [f for f in list if isfile(f)]
246-
list_dirs = [d for d in list if d list_files]
247-
248-
for fname in list_files
249-
link = lstrip_cdir(fname)
250-
name = splitdir(fname)[end]
251-
post = ifelse(islink(fname), " @", "")
252-
write(io, """
253-
<li><a href="/$(link)">$(name)$(post)</a></li>
254-
"""
255-
)
256-
end
257-
for fdir in list_dirs
258-
link = lstrip_cdir(fdir)
259-
# ensure ends with slash, see #135
260-
link *= ifelse(endswith(link, "/"), "", "/")
261-
name = splitdir(fdir)[end]
262-
pre = "📂 "
263-
post = ifelse(islink(fdir), " @", "")
264-
write(io, """
265-
<li><a href="/$(link)">$(pre)$(name)$(post)</a></li>
266-
"""
267-
)
268-
end
287+
# Write the page-specific HTML (should only write the _contents_ of <body>...</body> tag)
288+
f(io)
289+
# Write the shared footer
269290
write(io, """
270-
</ul>
271-
<hr>
272-
<a href="https://github.com/tlienart/LiveServer.jl">
273-
💻 LiveServer.jl
274-
</a>
275291
</body>
276292
</html>
277293
"""
@@ -334,24 +350,25 @@ function serve_file(
334350
fs_path, case = get_fs_path(req.target)
335351

336352
if case == :not_found_without_404
337-
return HTTP.Response(404,
338-
"""
339-
<div style="width: 100%; max-width: 500px; margin: auto">
340-
<h1 style="margin-top: 2em">404 Not Found</h1>
341-
<p>
342-
The requested URL [<code>$(req.target)</code>] does not correspond to a resource on the server.
343-
</p>
344-
<p>
345-
Perhaps you made a typo in the URL, or the URL corresponds to a file that has been
346-
deleted or renamed.
347-
</p>
348-
<p>
349-
<a href="/">Home</a>
350-
</p>
351-
</div>
352-
"""
353-
)
354-
ret_code = 404
353+
html_404 = pagehtml(title = "404 Not Found") do io
354+
write(io, """
355+
<div style="width: 100%; max-width: 500px; margin: auto">
356+
<h1 style="margin-top: 2em">404 Not Found</h1>
357+
<p>
358+
The requested URL [<code>$(req.target)</code>] does not correspond to a resource on the server.
359+
</p>
360+
<p>
361+
Perhaps you made a typo in the URL, or the URL corresponds to a file that has been
362+
deleted or renamed.
363+
</p>
364+
<p>
365+
<a href="/">Home</a>
366+
</p>
367+
</div>
368+
"""
369+
)
370+
end
371+
return HTTP.Response(404, html_404)
355372
elseif case == :not_found_with_404
356373
ret_code = 404
357374
elseif case == :dir_without_index
@@ -419,7 +436,7 @@ function serve_file(
419436
content = take!(io)
420437
end
421438
end
422-
439+
423440
range_match = match(r"bytes=(\d+)-(\d+)" , HTTP.header(req, "Range", ""))
424441
is_ranged = !isnothing(range_match)
425442

0 commit comments

Comments
 (0)