Skip to content

Instantly share code, notes, and snippets.

@stisa
Last active July 23, 2020 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stisa/2dcfb26853439be474a627e5286758b1 to your computer and use it in GitHub Desktop.
Save stisa/2dcfb26853439be474a627e5286758b1 to your computer and use it in GitHub Desktop.
Static file server using jester, should be roughly equivalent to `python -m http.server`
import jester, asyncdispatch
from parseopt import getopt,CmdLineKind
from strutils import parseint,isDigit
from os import dirExists
proc usage():string = r"""
Usage: jest [<dir>][<port>][--host:<addr>]
addr : the address to serve to, default: 'localhost'
port : the port to serve to, default: 8000
dir : the directory to serve, default: local dir ( '.' ). Needs to exist.
"""
var
prt :int = 8000
dir :string = "."
adr :string = "localhost"
for kind,key,val in getopt():
case kind:
of cmdArgument:
if key.string[0].isDigit: prt = key.string.parseint
elif dirExists(key): dir = key
else:
echo usage()
quit()
of cmdShortOption:
echo usage()
quit()
of cmdLongOption:
case key:
of "host": adr = val
else:
echo usage()
quit()
of cmdEnd: quit()
settings:
staticDir = dir
port = Port(prt)
bindAddr = adr
routes:
get "/":
redirect(uri("index.html"))
runForever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment