Fixed address conflict problem; improved help messages.
h3rald h3rald@h3rald.com
Wed, 22 Feb 2023 06:16:04 +0000
1 files changed,
16 insertions(+),
13 deletions(-)
jump to
M
src/nimhttpd.nim
→
src/nimhttpd.nim
@@ -20,7 +20,8 @@ version = pkgVersion
style = "style.css".slurp description = pkgDescription author = pkgAuthor - addressDefault = "localhost" + addressDefault4 = "127.0.0.1" + addressDefault6 = "0:0:0:0:0:0:0:1" portDefault = 1337 let usage = """ $1 v$2 - $3@@ -34,12 +35,12 @@ 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 IPv4 address to listen to (default: $6). If the specified port is + -p, --port The port to listen to (default: $5). If the specified port is unavailable, the number will be incremented until an available port is found. - -6, --ipv6 The IPv6 address to listen to (default: $6). - -H --header Add a custom header. Multiple headers can be added -""" % [name, version, description, author, $portDefault, $addressDefault] + -a, --address The IPv4 address to listen to (default: $6). + -6, --ipv6 The IPv6 address to listen to (default: $7). + -H --header Add a custom header. Multiple headers can be added. +""" % [name, version, description, author, $portDefault, $addressDefault4, $addressDefault6] type@@ -160,11 +161,11 @@ proc genMsg(settings: NimHttpSettings): string =
let t = now() let pid = getCurrentProcessId() result = """$1 v$2 -Address4: $3 -Address6: $4 -Directory: $5 -Current Time: $6 -PID: $7""" % [settings.name, settings.version, settings.address4, settings.address6, settings.directory.quoteShell, $t, $pid] +Address (IPv4): http://$3:$5 +Address (IPv6): http://$4:$5 +Directory: $6 +Current Time: $7 +PID: $8""" % [settings.name, settings.version, settings.address4, settings.address6, $settings.port, settings.directory.quoteShell, $t, $pid] proc serve*(settings: NimHttpSettings) = var server = newAsyncHttpServer()@@ -191,8 +192,8 @@
when isMainModule: var port = portDefault - var address4 = addressDefault - var address6 = addressDefault + var address4 = addressDefault4 + var address6 = addressDefault6 var logging = false var www = getCurrentDir() var title = "Index"@@ -250,6 +251,8 @@ var addrInfo6 = getAddrInfo(address6, Port(port), AF_INET6)
if (addrInfo4 == nil) and (addrInfo6 == nil): echo "Error: Could not resolve given IPv4 or IPv6 addresses." quit(1) + freeAddrInfo(addrInfo4) + freeAddrInfo(addrInfo6) var settings: NimHttpSettings settings.directory = www