Skip to content

Commit 9f76f13

Browse files
committed
succeed backend local
1 parent 2650a62 commit 9f76f13

File tree

7 files changed

+67
-22
lines changed

7 files changed

+67
-22
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ jobs:
5252

5353
- name: Build and push Docker images
5454
run: |
55-
docker compose build
56-
docker compose push
55+
docker compose -f docker-compose.yml build
56+
docker compose -f docker-compose.yml push

config/nginx/default.conf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ server {
6363

6464
# Backend API
6565
location /api/ {
66-
proxy_pass http://localhost:8000/;
66+
proxy_pass http://localhost:8000;
6767
proxy_set_header Host $host;
6868
proxy_set_header X-Real-IP $remote_addr;
69-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
70-
proxy_set_header X-Forwarded-Proto $forwarded_scheme;
69+
access_log /var/log/nginx/api_access.log;
70+
error_log /var/log/nginx/api_error.log debug;
7171
}
7272

7373
# GitHub raw content proxy

docker-compose.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ services:
1616
# Local schema mount (only used when SCHEMA_SOURCE=local)
1717
- ${SCHEMA_PATH:-./schemas}:/schemas:ro
1818
# Data storage volume
19-
- ${DATA_PATH:-./data}:/data
19+
- ${DATA_PATH:-./data}:/data:rw
2020
environment:
2121
# Core settings
2222
- NODE_ENV=${NODE_ENV:-production}
@@ -31,8 +31,11 @@ services:
3131

3232
# Backend configuration
3333
- REPROSCHEMA_BACKEND_BASEDIR=/data
34-
- INITIAL_TOKEN=${INITIAL_TOKEN:-}
34+
# For development mode
35+
- DEV8dac6d02a913=${DEV_MODE:-1}
3536
- BACKEND_URL=${BACKEND_URL:-/api}
37+
# Project name for data organization
38+
- PROJECT_NAME=${PROJECT_NAME:-demo}
3639

3740
# Development settings
3841
- DEV_MODE=${DEV_MODE:-0}

docker/Dockerfile

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ RUN pip install --no-cache-dir sanic sanic-ext requests jsonschema uuid
66

77
# Start backend temporarily to get token
88
RUN mkdir -p /data/logs && \
9-
python app.py & \
9+
python app.py > /data/logs/startup.log 2>&1 & \
1010
sleep 2 && \
11-
export INITIAL_TOKEN=$(grep "TOKEN:" /data/logs/* | cut -d':' -f2) && \
12-
echo $INITIAL_TOKEN > /tmp/token
11+
grep "Warning: INITIAL_TOKEN not set, generated:" /data/logs/startup.log | head -n1 | cut -d':' -f3 | tr -d ' ' > /tmp/token && \
12+
echo "Saved token: $(cat /tmp/token)" && \
13+
cat /data/logs/startup.log # Debug output
1314

1415
# 2. Frontend stage
1516
FROM node:18-alpine as frontend-builder
@@ -32,7 +33,9 @@ RUN chmod +x setup-config.sh
3233

3334
# 3. Set up config with everything we need
3435
COPY --from=backend-builder /tmp/token /tmp/token
35-
ENV INITIAL_TOKEN="$(cat /tmp/token)"
36+
RUN INITIAL_TOKEN=$(cat /tmp/token) && \
37+
echo "Got token: $INITIAL_TOKEN" && \
38+
echo "INITIAL_TOKEN=$INITIAL_TOKEN" >> /tmp/env_vars
3639
ENV BACKEND_URL="/api"
3740

3841
# 4. Run setup-config.sh to prepare config
@@ -54,9 +57,11 @@ RUN echo "=== Dist config.js ===" && cat dist/config.js || echo "No config.js in
5457
FROM python:3.11-slim
5558
WORKDIR /app
5659

57-
# Get assets path from frontend stage
60+
# Get assets path and token from frontend stage
5861
COPY --from=frontend-builder /tmp/assets_path /tmp/assets_path
62+
COPY --from=frontend-builder /tmp/token /tmp/token
5963
ENV ASSETS_PUBLIC_PATH="$(cat /tmp/assets_path)"
64+
ENV INITIAL_TOKEN="$(cat /tmp/token)"
6065

6166
# Install nginx and other required packages
6267
RUN apt-get update && \
@@ -84,8 +89,10 @@ RUN chmod +x /app/scripts/setup-ssl.sh
8489
# Create SSL directory
8590
RUN mkdir -p /etc/nginx/ssl
8691

87-
# Create necessary directories
88-
RUN mkdir -p /schemas /data
92+
# Create necessary directories with proper permissions
93+
RUN mkdir -p /schemas /data && \
94+
mkdir -p /data/responses && \
95+
chmod -R 777 /data # Ensure backend can write to data directory
8996

9097
# Environment variables
9198
ENV REPROSCHEMA_BACKEND_BASEDIR=/data

docker/backend/app.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
app = Sanic("reproschema_backend")
1919
Extend(app)
2020

21-
@app.get("/health")
21+
@app.get("/api/health")
2222
async def health_check(request):
2323
print("Health check requested")
2424
return json({"status": "healthy"})
@@ -35,7 +35,7 @@ async def health_check(request):
3535
else:
3636
print(f"Using INITIAL_TOKEN from environment: {INITIAL_TOKEN}")
3737

38-
@app.get("/token")
38+
@app.get("/api/token")
3939
async def get_token(request):
4040
token = request.args.get('token')
4141
project = request.args.get('project', os.getenv('STUDY_PREFIX', 'study'))
@@ -58,7 +58,7 @@ async def get_token(request):
5858
})
5959

6060
# Add register endpoint for local development
61-
@app.get("/register")
61+
@app.get("/api/register")
6262
async def register(request):
6363
if not os.getenv('DEV_MODE'):
6464
return json({"error": "Registration only available in dev mode"}, status=403)
@@ -75,6 +75,10 @@ async def register(request):
7575
async def submit_data(request):
7676
auth_token = request.headers.get('Authorization')
7777

78+
# Strip "Bearer " prefix if present
79+
if auth_token and auth_token.startswith('Bearer '):
80+
auth_token = auth_token.split(' ')[1]
81+
7882
if not auth_token or auth_token not in auth_tokens:
7983
return json({"error": "Invalid auth token"}, status=401)
8084

@@ -126,8 +130,8 @@ async def root(request):
126130
return json({
127131
"status": "running",
128132
"endpoints": [
129-
"/health",
130-
"/token",
133+
"/api/health",
134+
"/api/token",
131135
"/api/responses",
132136
"/api/schema/<url>"
133137
]

scripts/setup-config.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ setup_config() {
5656
log "Environment variables:"
5757
log " SCHEMA_SOURCE: ${SCHEMA_SOURCE}"
5858
log " BACKEND_URL: ${BACKEND_URL}"
59+
# Get token from file
60+
INITIAL_TOKEN=$(cat /tmp/token)
5961
log " INITIAL_TOKEN: ${INITIAL_TOKEN}"
6062
log "Schema values:"
6163
log " banner: ${banner}"

scripts/start.sh

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ envsubst '${ASSETS_PUBLIC_PATH}' < /etc/nginx/conf.d/default.conf.template > /et
2121
log "Nginx config:"
2222
cat /etc/nginx/conf.d/default.conf | grep -A 5 "location.*${ASSETS_PUBLIC_PATH}"
2323

24+
# Log token early, before SSL noise
25+
if [ "${DEV_MODE}" = "1" ]; then
26+
echo "================================================================"
27+
log "BACKEND TOKEN: ${INITIAL_TOKEN:-$(cat /tmp/token)}"
28+
echo "================================================================"
29+
fi
30+
2431
# Setup SSL certificates
2532
/app/scripts/setup-ssl.sh
2633

@@ -30,8 +37,21 @@ nginx
3037

3138
# Start backend
3239
log "Starting Python backend..."
33-
python app.py &
40+
# Ensure backend has correct token
41+
export INITIAL_TOKEN="$(cat /tmp/token)"
42+
# Ensure data directory exists and is writable
43+
mkdir -p /data/responses
44+
mkdir -p /data/logs
45+
python app.py > /data/logs/backend.log 2>&1 &
3446
BACKEND_PID=$!
47+
log "Backend started with PID: ${BACKEND_PID}"
48+
49+
# Verify backend process is running
50+
if ! kill -0 $BACKEND_PID 2>/dev/null; then
51+
log "Backend failed to start! Logs:"
52+
cat /data/logs/backend.log
53+
exit 1
54+
fi
3555

3656
# Wait for backend to be ready
3757
log "Waiting for backend to be ready..."
@@ -41,12 +61,21 @@ until curl -s http://localhost:8000/health > /dev/null; do
4161
ATTEMPTS=$((ATTEMPTS + 1))
4262
if [ $ATTEMPTS -ge $MAX_ATTEMPTS ]; then
4363
log "Backend failed to start after $MAX_ATTEMPTS attempts"
64+
log "Backend logs:"
65+
cat /data/logs/backend.log
4466
exit 1
4567
fi
4668
log "Attempt $ATTEMPTS: Backend not ready yet..."
4769
sleep 1
4870
done
4971
log "Backend is ready"
5072

51-
# Wait for processes
52-
wait $BACKEND_PID
73+
# Keep container running
74+
while kill -0 $BACKEND_PID 2>/dev/null; do
75+
tail -f /data/logs/backend.log &
76+
wait $BACKEND_PID
77+
done
78+
79+
log "Backend exited unexpectedly! Logs:"
80+
cat /data/logs/backend.log
81+
exit 1

0 commit comments

Comments
 (0)