all repos — py-vite @ b27d9c45872655e19ad5f84cfd99fc1a7420336c

the original vite, written in python

feat: implement HTTP server
Anirudh icyph0x@pm.me
Sun, 15 Apr 2018 17:06:29 +0530
commit

b27d9c45872655e19ad5f84cfd99fc1a7420336c

parent

a91eeb72d8f2249e47e54046be2df2ada20802f1

1 files changed, 24 insertions(+), 0 deletions(-)

jump to
M make.pymake.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):