"""
啟動 MOPS 公司資料查詢介面
"""
import sys
sys.stdout.reconfigure(encoding='utf-8')

import http.server
import socketserver
import webbrowser
import os
from pathlib import Path

PORT = 8080

def main():
    # 切換到專案目錄
    os.chdir(Path(__file__).parent)

    print(f"\n=== MOPS 公司資料查詢介面 ===")
    print(f"啟動伺服器於 http://localhost:{PORT}")
    print(f"按 Ctrl+C 停止伺服器\n")

    # 自動開啟瀏覽器
    webbrowser.open(f'http://localhost:{PORT}/viewer.html')

    # 啟動伺服器
    handler = http.server.SimpleHTTPRequestHandler
    with socketserver.TCPServer(("", PORT), handler) as httpd:
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            print("\n伺服器已停止")

if __name__ == '__main__':
    main()
