# --------------------------------------------------------------------
# 5b) Deployment: affine-server (serves HTTP on port 3010)
# --------------------------------------------------------------------
apiVersion: apps/v1
kind: Deployment
metadata:
  name: affine-server
  namespace: affine
  labels:
    app: affine-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: affine-server
  template:
    metadata:
      labels:
        app: affine-server
    spec:
      initContainers:
        - name: affine-migrate
          image: ghcr.io/toeverything/affine-graphql:stable-9e7280c
          command: ["sh", "-c", "node ./scripts/self-host-predeploy.js"]
          env:
            - name: REDIS_SERVER_HOST
              value: "redis-lb.redis.svc.cluster.local"
            - name: REDIS_SERVER_PORT
              value: "6379"
            - name: DATABASE_URL
              value: >
                postgresql://$(DB_USERNAME):$(DB_PASSWORD)@postgres-base-rw.postgres.svc.cluster.local:5432/$(DB_DATABASE)
            - name: AFFINE_SERVER_PORT
              value: "3010"
          envFrom:
            - secretRef:
                name: affine-db-secret
          volumeMounts:
            - name: affine-storage
              mountPath: /root/.affine/storage
            - name: affine-config
              mountPath: /root/.affine/config
      containers:
        - name: affine
          image: ghcr.io/toeverything/affine-graphql:stable-9e7280c
          ports:
            - containerPort: 3010
              name: http
          env:
            - name: NODE_TLS_REJECT_UNAUTHORIZED
              value: "0"
            - name: AFFINE_SERVER_HTTPS
              value: "true"
            - name: AFFINE_SERVER_HOST
              value: "affine.prod.panic.haus"
            - name: REDIS_SERVER_HOST
              value: "redis-lb.redis.svc.cluster.local"
            - name: REDIS_SERVER_PORT
              value: "6379"
            - name: DATABASE_URL
              value: >-
                postgresql://$(DB_USERNAME):$(DB_PASSWORD)@postgres-base-rw.postgres.svc.cluster.local:5432/$(DB_DATABASE)
            - name: AFFINE_SERVER_EXTERNAL_URL
              value: "https://affine.prod.panic.haus"
            - name: AFFINE_SERVER_PORT
              value: "3010"
          envFrom:
            - secretRef:
                name: affine-db-secret
          readinessProbe:
            httpGet:
              path: /health
              port: 3010
            initialDelaySeconds: 10
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /health
              port: 3010
            initialDelaySeconds: 30
            periodSeconds: 20
          volumeMounts:
            - name: affine-storage
              mountPath: /root/.affine/storage
            - name: affine-config
              mountPath: /root/.affine/config
      volumes:
        - name: affine-storage
          persistentVolumeClaim:
            claimName: affine-storage-pvc
        - name: affine-config
          persistentVolumeClaim:
            claimName: affine-config-pvc