feat: implement HTTP server
Anirudh icyph0x@pm.me
Sun, 15 Apr 2018 17:06:29 +0530
1 files changed,
24 insertions(+),
0 deletions(-)
jump to
M
make.py
→
make.py
@@ -5,6 +5,8 @@ import sys
import jinja2 import time import argparse +import http.server +import socketserver from markdown2 import markdown_path from huepy import *@@ -15,6 +17,12 @@ help_txt = 'Serve pages from the ' + italic('build') + ' directory.'
parser = argparse.ArgumentParser(description=desc, usage=usage) parser.add_argument('serve', nargs='*', help=help_txt) +try: + args = parser.parse_args() +except IndexError: + parser.print_help() + sys.exit(1) + # import config file try: sys.path.append(os.getcwd())@@ -30,6 +38,7 @@ PAGES_PATH = 'pages/'
BUILD_PATH = 'build/' TEMPL_PATH = 'templates/' TEMPL_FILE = TEMPL_PATH + config.template +PORT = 1911 # jinja2@@ -61,8 +70,23 @@ with open(os.path.join(html_file, 'index.html'), 'w') as f:
f.write(output) print(run('Rendered %s.' % (page))) +def server(): + handler = http.server.SimpleHTTPRequestHandler + try: + with socketserver.TCPServer(('', PORT), handler) as httpd: + print(run(f'Serving pages at http://localhost:{PORT}')) + print(white('Ctrl+C') + ' to stop.') + httpd.serve_forever() + except KeyboardInterrupt: + print(info('Stopping server.')) + sys.exit(1) def main(): + if args.serve: + server() + else: + parser.print_help() + start = time.process_time() TEMPL_FILE = TEMPL_PATH + config.template if not os.listdir(PAGES_PATH):