#!/usr/bin/env python3
"""
Signal Viewer 로컬 서버
포트 1031에서 HTML 파일 서빙 (프로젝트 루트 기준)
"""

import http.server
import os
import socketserver
from pathlib import Path

PORT = 1031
# 프로젝트 루트 (share의 부모 디렉토리)
PROJECT_ROOT = Path(__file__).parent.parent


class Handler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, directory=str(PROJECT_ROOT), **kwargs)


class Server(socketserver.TCPServer):
    allow_reuse_address = True


def main():
    os.chdir(PROJECT_ROOT)
    with Server(("", PORT), Handler) as httpd:
        print(f"Signal Viewer serving at http://localhost:{PORT}")
        print(f"Open: http://localhost:{PORT}/share/signal_viewer.html")
        print("Press Ctrl+C to stop")
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            print("\nServer stopped.")


if __name__ == "__main__":
    main()
