Last active
July 23, 2020 21:58
-
-
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`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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