# Build stage FROM golang:alpine AS builder WORKDIR /app # Install git for go mod download RUN apk add --cache git # Clone the repository RUN git clone https://github.com/router-for-me/CLIProxyAPI.git . ARG VERSION=dev ARG COMMIT=none ARG BUILD_DATE=unknown RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w -X 'main.Version=${VERSION}' -X 'main.Commit=${COMMIT}' -X 'main.BuildDate=${BUILD_DATE}'" -o ./chat-server ./cmd/server/ # Runtime stage FROM alpine:3.22.0 RUN apk add --no-cache tzdata curl nginx # Create non-root user for HF Spaces RUN adduser -D -u 1000 user RUN mkdir -p /app /var/cache/nginx /var/run/nginx /var/log/nginx && \ chown -R user:user /app /var/cache/nginx /var/run/nginx /var/log/nginx /etc/nginx # Download management panel to static directory RUN mkdir -p /app/static && \ curl -L -o /app/static/management.html \ "https://github.com/router-for-me/Cli-Proxy-API-Management-Center/releases/download/v1.2.6/management.html" && \ chown -R user:user /app/static COPY --from=builder /app/chat-server /app/chat-server COPY --from=builder /app/config.example.yaml /app/config.example.yaml # Copy our fake UI COPY index.html /app/static/index.html RUN chown -R user:user /app/static WORKDIR /app # Create auth directory for user RUN mkdir -p /home/user/.cli-proxy-api && chown -R user:user /home/user/.cli-proxy-api # Configure Go Backend RUN echo 'host: "127.0.0.1"' > /app/config.yaml && \ echo 'port: 8080' >> /app/config.yaml && \ echo 'debug: false' >> /app/config.yaml && \ echo 'auth-dir: "/home/user/.cli-proxy-api"' >> /app/config.yaml && \ echo 'remote-management:' >> /app/config.yaml && \ echo ' secret-key: "admin123"' >> /app/config.yaml && \ echo ' allow-remote: true' >> /app/config.yaml && \ echo ' disable-control-panel: false' >> /app/config.yaml && \ echo ' panel-github-repository: "https://github.com/router-for-me/Cli-Proxy-API-Management-Center"' >> /app/config.yaml && \ echo 'api-keys:' >> /app/config.yaml && \ echo ' - "default-key"' >> /app/config.yaml && \ chown user:user /app/config.yaml # Configure Nginx Reverse Proxy RUN echo 'pid /var/run/nginx/nginx.pid;' > /etc/nginx/nginx.conf && \ echo 'error_log stderr;' >> /etc/nginx/nginx.conf && \ echo 'events {}' >> /etc/nginx/nginx.conf && \ echo 'http {' >> /etc/nginx/nginx.conf && \ echo ' access_log /dev/stdout;' >> /etc/nginx/nginx.conf && \ echo ' client_body_temp_path /var/cache/nginx/client_temp;' >> /etc/nginx/nginx.conf && \ echo ' proxy_temp_path /var/cache/nginx/proxy_temp;' >> /etc/nginx/nginx.conf && \ echo ' fastcgi_temp_path /var/cache/nginx/fastcgi_temp;' >> /etc/nginx/nginx.conf && \ echo ' uwsgi_temp_path /var/cache/nginx/uwsgi_temp;' >> /etc/nginx/nginx.conf && \ echo ' scgi_temp_path /var/cache/nginx/scgi_temp;' >> /etc/nginx/nginx.conf && \ echo ' server {' >> /etc/nginx/nginx.conf && \ echo ' listen 7860;' >> /etc/nginx/nginx.conf && \ echo ' location = / {' >> /etc/nginx/nginx.conf && \ echo ' root /app/static;' >> /etc/nginx/nginx.conf && \ echo ' try_files /index.html =404;' >> /etc/nginx/nginx.conf && \ echo ' }' >> /etc/nginx/nginx.conf && \ echo ' location / {' >> /etc/nginx/nginx.conf && \ echo ' proxy_pass http://127.0.0.1:8080;' >> /etc/nginx/nginx.conf && \ echo ' proxy_set_header Host $host;' >> /etc/nginx/nginx.conf && \ echo ' proxy_buffering off;' >> /etc/nginx/nginx.conf && \ echo ' proxy_read_timeout 300s;' >> /etc/nginx/nginx.conf && \ echo ' }' >> /etc/nginx/nginx.conf && \ echo ' }' >> /etc/nginx/nginx.conf && \ echo '}' >> /etc/nginx/nginx.conf # HF Spaces requires port 7860 EXPOSE 7860 ENV TZ=Asia/Shanghai ENV MANAGEMENT_STATIC_PATH=/app/static/management.html RUN cp /usr/share/zoneinfo/${TZ} /etc/localtime && echo "${TZ}" > /etc/timezone # Switch to non-root user USER user # Create an entrypoint script to keep the space active RUN echo '#!/bin/sh' > /app/start.sh && \ echo 'nginx' >> /app/start.sh && \ echo './chat-server -config /app/config.yaml &' >> /app/start.sh && \ echo 'while true; do curl -s http://localhost:7860/ > /dev/null; sleep 300; done' >> /app/start.sh && \ chmod +x /app/start.sh # Start with the wrapper script CMD ["/app/start.sh"]