Files
amc/amc_server/server.py
teernisse a7b2b3b902 refactor(server): extract amc_server package from monolithic script
Split the 860+ line bin/amc-server into a modular Python package:

  amc_server/
    __init__.py         - Package marker
    context.py          - Shared constants (DATA_DIR, PORT, CLAUDE_PROJECTS_DIR, etc.)
    handler.py          - AMCHandler class using mixin composition
    logging_utils.py    - Structured logging setup with signal handlers
    server.py           - Main entry point (ThreadingHTTPServer)
    mixins/
      __init__.py       - Mixin package marker
      control.py        - Session control (dismiss, respond via Zellij)
      conversation.py   - Conversation history parsing (Claude JSONL format)
      discovery.py      - Session discovery (Codex pane inspection, Zellij cache)
      http.py           - HTTP response helpers (CORS, JSON, static files)
      parsing.py        - Session state parsing and aggregation
      state.py          - Session state endpoint logic

The monolithic bin/amc-server becomes a thin launcher that just imports
and calls main(). This separation enables:

- Easier testing of individual components
- Better IDE support (proper Python package structure)
- Cleaner separation of concerns (discovery vs parsing vs control)
- ThreadingHTTPServer instead of single-threaded (handles concurrent requests)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-25 15:01:26 -05:00

32 lines
971 B
Python

import os
from http.server import ThreadingHTTPServer
from amc_server.context import DATA_DIR, PORT
from amc_server.handler import AMCHandler
from amc_server.logging_utils import LOGGER, configure_logging, install_signal_handlers
def main():
configure_logging()
DATA_DIR.mkdir(parents=True, exist_ok=True)
LOGGER.info("Starting AMC server")
server = ThreadingHTTPServer(("127.0.0.1", PORT), AMCHandler)
install_signal_handlers(server)
LOGGER.info("AMC server listening on http://127.0.0.1:%s", PORT)
# Write PID file
pid_file = DATA_DIR / "server.pid"
pid_file.write_text(str(os.getpid()))
try:
server.serve_forever()
except KeyboardInterrupt:
LOGGER.info("AMC server interrupted; shutting down")
except Exception:
LOGGER.exception("AMC server crashed")
raise
finally:
pid_file.unlink(missing_ok=True)
server.server_close()
LOGGER.info("AMC server stopped")