# Memory service: one container, one vault on a mounted volume.
# Build: docker build -t obsidian-wiki .
# Run:   docker run -p 127.0.0.1:8080:8080 -e WIKI_API_KEY=... -v wiki-data:/vault obsidian-wiki
# Or pull the released image: ghcr.io/ar9av/obsidian-wiki:latest
FROM python:3.12-slim

# Ownership proof for the MCP Registry (server.json); must match its "name".
LABEL io.modelcontextprotocol.server.name="io.github.Ar9av/obsidian-wiki"

# git: obsidian_wiki/sync.py shells out to it for vault backup and two-way sync
# (WIKI_GIT_REMOTE). openssh-client: for git@ remotes with a mounted deploy key.
RUN apt-get update && apt-get install -y --no-install-recommends git openssh-client \
    && rm -rf /var/lib/apt/lists/* \
    # A bind-mounted vault is owned by the host's uid; without this git refuses it.
    && git config --system safe.directory '*'

WORKDIR /src
COPY . .
# .git is excluded from the build context, so hatch-vcs cannot read the tag.
# CI passes the real one: docker build --build-arg VERSION=2026.8.1
ARG VERSION=0.0.0
ENV HATCH_VCS_PRETEND_VERSION=$VERSION SETUPTOOLS_SCM_PRETEND_VERSION=$VERSION
RUN pip install --no-cache-dir '.[server]'

RUN useradd --create-home wiki && mkdir -p /vault && chown wiki:wiki /vault
USER wiki

# WIKI_HOST is loopback by default; a container must listen on every interface.
# The port is WIKI_PORT, else the PaaS-assigned PORT (Render, Railway), else 8080.
ENV OBSIDIAN_VAULT_PATH=/vault WIKI_HOST=0.0.0.0
# Author for the server's sync commits; override to attribute them to yourself.
ENV GIT_AUTHOR_NAME="obsidian-wiki server" GIT_AUTHOR_EMAIL=obsidian-wiki@localhost \
    GIT_COMMITTER_NAME="obsidian-wiki server" GIT_COMMITTER_EMAIL=obsidian-wiki@localhost
VOLUME /vault
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s \
    CMD python -c "import os,sys,urllib.request; p=os.environ.get('WIKI_PORT') or os.environ.get('PORT') or '8080'; sys.exit(0 if urllib.request.urlopen(f'http://127.0.0.1:{p}/health').status==200 else 1)"

CMD ["python", "-m", "obsidian_wiki.server"]
