File size: 3,452 Bytes
338ac2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8291c25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
set -e

echo "Starting PDF TEI Editor application..."

# Set default port if not provided
PORT=${PORT:-8000}

# Change to app directory
cd /app

# Initialize database directory if it doesn't exist
if [ ! -d "/app/data/db" ]; then
    echo "Initializing database directory..."
    mkdir -p /app/data/db
fi

# Copy default config files if they don't exist
for config_file in /app/config/*.json; do
    if [ -f "$config_file" ]; then
        filename=$(basename "$config_file")
        if [ ! -f "/app/data/db/$filename" ]; then
            echo "Copying default $filename..."
            cp "$config_file" "/app/data/db/$filename"
        fi
    fi
done


# Create default accounts if no environment variables are set
if [ -z "$APP_ADMIN_PASSWORD" ] && [ -z "$APP_DEMO_PASSWORD" ]; then
    echo "No custom passwords provided, setting up default demo accounts..."

    # Set default login message with security warning
    export APP_LOGIN_MESSAGE="<h2>⚠️ Demo Installation</h2><p>Default accounts: <code>admin/admin</code> and <code>demo/demo</code>. For testing purposes only. <a href='https://github.com/mpilhlt/pdf-tei-editor/blob/main/docs/testdrive-docker.md' target='_blank'>Configure real passwords in production!</a></p>"
    # Use Python to properly escape the message for JSON
    ESCAPED_DEFAULT_MESSAGE=$(.venv/bin/python -c "import json, os; print(json.dumps(os.environ.get('APP_LOGIN_MESSAGE', '')))")
    .venv/bin/python bin/manage.py config set application.login-message "$ESCAPED_DEFAULT_MESSAGE" 2>/dev/null || echo "Warning: Failed to set default login message"

    # Create default admin and demo user
    export APP_ADMIN_PASSWORD="admin"
    export APP_DEMO_PASSWORD="demo"
fi

# Set login message if APP_LOGIN_MESSAGE is provided
if [ -n "$APP_LOGIN_MESSAGE" ]; then
    echo "Setting login message from environment variable..."
    # Use Python to properly escape the message for JSON
    ESCAPED_MESSAGE=$(.venv/bin/python -c "import json, os; print(json.dumps(os.environ.get('APP_LOGIN_MESSAGE', '')))")
    .venv/bin/python bin/manage.py config set application.login-message "$ESCAPED_MESSAGE" 2>/dev/null || echo "Warning: Failed to set login message"
fi


# Create or update admin user if APP_ADMIN_PASSWORD is set
if [ -n "$APP_ADMIN_PASSWORD" ]; then
    echo "Setting up admin user from environment variable..."
    if .venv/bin/python bin/manage.py user add admin \
            --password "$APP_ADMIN_PASSWORD" \
            --fullname "Administrator" \
            --roles "admin" \
            --email "admin@localhost";
    then
        echo "Admin user created successfully"
    else
        echo "Warning: Failed to create admin user"
    fi
fi

# Create or update demo user if APP_DEMO_PASSWORD is set
if [ -n "$APP_DEMO_PASSWORD" ]; then
    echo "Creating new demo user..."
    if .venv/bin/python bin/manage.py user add demo \
            --password "$APP_DEMO_PASSWORD" \
            --fullname "Demo User" \
            --roles "user,annotator,reviewer" \
            --email "demo@localhost";
    then
        echo "Demo user created successfully"
    else
        echo "Warning: Failed to create demo user"
    fi
fi

# Import demo data if present
if [ -f /app/docker/import-demo-data.sh ]; then
    echo "Importing demo data..."
    /app/docker/import-demo-data.sh
fi

# Start the PDF TEI Editor application bound to all interfaces for Docker
.venv/bin/python bin/start-prod 0.0.0.0 $PORT