all repos — nimhttpd @ 2c8a9a0b7dd08c731e280314b0b2a38978789512

A useful static file web server.

Improved look and feel; added --title.
h3rald h3rald@h3rald.com
Wed, 21 Apr 2021 20:22:51 +0000
commit

2c8a9a0b7dd08c731e280314b0b2a38978789512

parent

1b57f412f55df29cdfd39023dfd134d2f9d35b97

3 files changed, 21 insertions(+), 11 deletions(-)

jump to
M src/nimhttpd.nimsrc/nimhttpd.nim

@@ -25,7 +25,7 @@ addressDefault = "0.0.0.0"

portDefault = 1337 let usage = """ $1 v$2 - $3 - (c) 2014-2020 $4 + (c) 2014-2021 $4 Usage: nimhttpd [-p:port] [directory]

@@ -34,6 +34,7 @@ Arguments:

directory The directory to serve (default: current directory). Options: + -t, --title The title to use in index pages (default: Index) -p, --port The port to listen to (default: $5). -a, --address The address to listen to (default: $6). If the specified port is unavailable, the number will be incremented until an available port is found.

@@ -50,8 +51,9 @@ logging*: bool

directory*: string mimes*: MimeDb port*: Port + title*: string address*: string - name: string + name*: string version*: string proc bindAvailablePort*(handle: SocketHandle, p: int): Port =

@@ -67,8 +69,9 @@ check = bindAddr(handle, cast[ptr SockAddr](addr(name)), sizeof(name).Socklen)

result = getLocalAddr(handle, AF_INET)[1] -proc h_page(settings:NimHttpSettings, content: string, title=""): string = +proc h_page(settings:NimHttpSettings, content, title, subtitle: string): string = var footer = """<div id="footer">$1 v$2</div>""" % [settings.name, settings.version] + var titles = "" result = """ <!DOCTYPE html> <html>

@@ -76,14 +79,16 @@ <head>

<title>$1</title> <style type="text/css">$2</style> <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <h1>$1</h1> - $3 + <h2>$3</h2> $4 + $5 </body> </html> - """ % [title, style, content, footer] + """ % [title, style, subtitle, content, footer] proc relativePath(path, cwd: string): string = var path2 = path

@@ -107,11 +112,11 @@ return relparent

proc sendNotFound(settings: NimHttpSettings, path: string): NimHttpResponse = var content = "<p>The page you requested cannot be found.<p>" - return (code: Http404, content: h_page(settings, content, $Http404), headers: newHttpHeaders()) + return (code: Http404, content: h_page(settings, content, $Http404, "Page Not Found"), headers: newHttpHeaders()) proc sendNotImplemented(settings: NimHttpSettings, path: string): NimHttpResponse = var content = "<p>This server does not support the functionality required to fulfill the request.</p>" - return (code: Http501, content: h_page(settings, content, $Http501), headers: newHttpHeaders()) + return (code: Http501, content: h_page(settings, content, $Http501, "Not Implemented"), headers: newHttpHeaders()) proc sendStaticFile(settings: NimHttpSettings, path: string): NimHttpResponse = let mimes = settings.mimes

@@ -129,7 +134,8 @@ var res: NimHttpResponse

var files = newSeq[string](0) if path != cwd and path != cwd&"/" and path != cwd&"\\": files.add """<li class="i-back entypo"><a href="$1">..</a></li>""" % [path.relativeParent(cwd)] - var title = "Index of " & path.relativePath(cwd) + var title = settings.title + let subtitle = path.relativePath(cwd) for i in walkDir(path): let name = i.path.extractFilename let relpath = i.path.relativePath(cwd)

@@ -144,7 +150,7 @@ <ul>

$1 </ul> """ % [files.join("\n")] - res = (code: Http200, content: h_page(settings, ul, title), headers: newHttpHeaders()) + res = (code: Http200, content: h_page(settings, ul, title, subtitle), headers: newHttpHeaders()) return res proc printReqInfo(settings: NimHttpSettings, req: Request) =

@@ -192,6 +198,7 @@ var port = portDefault

var address = addressDefault var logging = false var www = getCurrentDir() + var title = "Index" for kind, key, val in getopt(): case kind

@@ -207,6 +214,8 @@ echo version

quit(0) of "address", "a": address = val + of "title", "t": + title = val of "port", "p": try: port = val.parseInt

@@ -244,6 +253,7 @@ settings.mimes = newMimeTypes()

settings.mimes.register("htm", "text/html") settings.address = address settings.name = name + settings.title = title settings.version = version settings.port = availablePort
M src/nimhttpdpkg/config.nimsrc/nimhttpdpkg/config.nim

@@ -1,5 +1,5 @@

const pkgTitle* = "NimHTTPd" - pkgVersion* = "1.1.1" + pkgVersion* = "1.2.0" pkgAuthor* = "Fabio Cevasco" pkgDescription* = "A tiny static file web server."
M src/style.csssrc/style.css

@@ -148,10 +148,10 @@ h4,

h5, h6 { color: #111111; - border-bottom: 1px solid #dddddd; } h1 { text-align: center; + border-bottom: 1px solid #dddddd; } pre { -moz-background-clip: padding;