Skip to content

Instantly share code, notes, and snippets.

@pydanny
Created November 12, 2023 08:29
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 pydanny/bf25df3142c6454f5af2afe3f05c1f1a to your computer and use it in GitHub Desktop.
Save pydanny/bf25df3142c6454f5af2afe3f05c1f1a to your computer and use it in GitHub Desktop.
@app.command()
def serve(site: Path = Path("site")):
    """Serve the site"""
    check_call(["python", "-m", "http.server", "8000", "-d", site])
@app.command()
def serve2(site: Path = Path("site"), port: int = 8000):
    """Serve the site"""
    from .server import server

    server(site=site, port=port)
import functools
import http.server
import os
import socketserver
from pathlib import Path

from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer


def build_handler(directory: Path):
    return functools.partial(http.server.SimpleHTTPRequestHandler, directory=directory)


class MyHandler(FileSystemEventHandler):
    def on_any_event(self, event):
        if event.is_directory:
            return
        elif event.event_type in ["created", "modified"]:
            print(f"Reloading server due to file change: {event.src_path}")
            os._exit(0)


def run_server(site: Path, port: int = 8000):
    with socketserver.TCPServer(("", port), build_handler(site)) as httpd:
        print(f"Serving on port {port}")
        httpd.serve_forever()


def server(site: Path, port: int = 8000):
    # Set up the file system event handler
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, site, recursive=True)
    observer.start()

    try:
        # Run the HTTP server
        run_server(site=site, port=port)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment