File size: 4,577 Bytes
2d86077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash

# Deployment Script for ScoreAIManage on Render
# This script prepares the application for deployment

set -e

echo "πŸš€ Starting deployment preparation for ScoreAIManage..."

# Check if we're in the correct directory
if [ ! -f "package.json" ]; then
    echo "❌ Error: package.json not found. Please run this script from the project root."
    exit 1
fi

# Install dependencies
echo "πŸ“¦ Installing dependencies..."
npm ci --production=false

# Run tests
echo "πŸ§ͺ Running tests..."
npm test

# Build the application
echo "πŸ”¨ Building application..."
npm run build || echo "⚠️  Build script not found, continuing..."

# Create logs directory
echo "πŸ“ Creating logs directory..."
mkdir -p logs

# Set production environment
echo "🌍 Setting production environment..."
export NODE_ENV=production

# Check environment variables
echo "πŸ” Checking environment variables..."
if [ -f ".env.production" ]; then
    echo "βœ… Production environment file found"
    # Copy production env to .env for deployment
    cp .env.production .env
else
    echo "⚠️  Warning: .env.production not found"
fi

# Create database initialization script for production
echo "πŸ—„οΈ  Preparing database initialization..."
cat > scripts/init-production-db.sql << 'EOF'
-- Production Database Initialization for ScoreAIManage
-- This script initializes the database with basic structure

-- Run the main schema
\i database_schema_postgresql.sql

-- Create production user with limited privileges
DO $$
BEGIN
    IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'scoremanage_user') THEN
        CREATE ROLE scoremanage_user WITH LOGIN PASSWORD 'change_this_password';
    END IF;
END
$$;

-- Grant necessary permissions
GRANT CONNECT ON DATABASE scoremanage TO scoremanage_user;
GRANT USAGE ON SCHEMA public TO scoremanage_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO scoremanage_user;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO scoremanage_user;

-- Set default privileges for future tables
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO scoremanage_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT USAGE, SELECT ON SEQUENCES TO scoremanage_user;

EOF

echo "βœ… Database initialization script created"

# Create health check endpoint
echo "πŸ₯ Creating health check endpoint..."
cat > health-check.js << 'EOF'
// Health check script for Render
const http = require('http');

const options = {
  hostname: 'localhost',
  port: process.env.PORT || 10000,
  path: '/api/health',
  method: 'GET',
  timeout: 5000
};

const req = http.request(options, (res) => {
  if (res.statusCode === 200) {
    console.log('βœ… Health check passed');
    process.exit(0);
  } else {
    console.log(`❌ Health check failed with status: ${res.statusCode}`);
    process.exit(1);
  }
});

req.on('error', (err) => {
  console.log(`❌ Health check error: ${err.message}`);
  process.exit(1);
});

req.on('timeout', () => {
  console.log('❌ Health check timeout');
  req.destroy();
  process.exit(1);
});

req.end();
EOF

echo "βœ… Health check script created"

# Create deployment info file
echo "πŸ“‹ Creating deployment info..."
cat > deployment-info.json << EOF
{
  "deployed_at": "$(date -u +%Y-%m-%dT%H:%M:%S.%3NZ)",
  "version": "$(node -p "require('./package.json').version")",
  "environment": "production",
  "platform": "Render",
  "database": "PostgreSQL",
  "node_version": "$(node --version)",
  "npm_version": "$(npm --version)"
}
EOF

echo "βœ… Deployment info created"

# Final checks
echo "πŸ” Final deployment checks..."

# Check if main entry point exists
if [ -f "server.js" ] || [ -f "app.js" ] || [ -f "index.js" ]; then
    echo "βœ… Main entry point found"
else
    echo "⚠️  Warning: No main entry point (server.js, app.js, or index.js) found"
fi

# Check if package.json has start script
if npm run | grep -q "start"; then
    echo "βœ… Start script found"
else
    echo "⚠️  Warning: No start script found in package.json"
fi

echo ""
echo "πŸŽ‰ Deployment preparation completed!"
echo ""
echo "πŸ“‹ Next steps:"
echo "1. Push your code to GitHub"
echo "2. Connect your repository to Render"
echo "3. Configure environment variables in Render dashboard"
echo "4. Deploy!"
echo ""
echo "🌐 Your application will be available at: https://scoreaimanage.onrender.com"
echo ""
echo "πŸ“Š Health check endpoint: https://scoreaimanage.onrender.com/api/health"
echo "πŸ“š API documentation: https://scoreaimanage.onrender.com/api/docs"
echo ""