File size: 2,745 Bytes
ef85da9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5e5eac
 
 
8db1a4b
b5e5eac
65b0171
 
 
 
 
ef85da9
 
 
 
 
 
 
 
 
b5e5eac
 
6bd7697
b5e5eac
6bd7697
 
 
 
 
 
 
ef85da9
6bd7697
 
b5e5eac
8db1a4b
 
 
65b0171
b5e5eac
65b0171
 
 
b5e5eac
 
 
 
65b0171
b5e5eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const isHuggingFaceSpace = !!process.env.HF_SPACE_ID || !!process.env.SPACE_ID;
const isProduction = process.env.NODE_ENV === 'production';

if (!isHuggingFaceSpace && !isProduction) {
  const dotenv = require('dotenv');
  const path = require('path');
  const fs = require('fs');
  
  const envPath = path.join(__dirname, '../.env');
  if (fs.existsSync(envPath)) {
    dotenv.config({ path: envPath });
    console.log(`πŸ“„ Development: Loaded .env from: ${envPath}`);
  } else {
    dotenv.config();
    console.log('πŸ“„ Development: Using default .env location');
  }
} else {
  console.log('πŸ“„ Production: Using environment variables from platform secrets');
}

import 'reflect-metadata';
import { AppDataSource } from './config/database';
import app from './app';
import { initializeAdmin } from './scripts/createAdmin';

const PORT = parseInt(process.env.PORT || process.env.SPACE_PORT || '7860', 10);

console.log('πŸ”§ Starting server...');
console.log(`πŸ“‘ Environment: ${process.env.NODE_ENV || 'development'}`);
console.log(`🌐 Port: ${PORT}`);
if (process.env.DATABASE_URL) {
  const dbUrl = process.env.DATABASE_URL;
  const masked = dbUrl.length > 60 
    ? `${dbUrl.substring(0, 30)}...${dbUrl.substring(dbUrl.length - 10)}`
    : dbUrl;
  console.log(`πŸ“Š Database URL: Set (${masked})`);
} else {
  console.log(`πŸ“Š Database URL: ❌ NOT SET`);
}

AppDataSource.initialize()
  .then(async () => {
    console.log('βœ… Database connected');
    
    if (process.env.DB_SYNCHRONIZE !== 'true' && process.env.NODE_ENV === 'production') {
      try {
        console.log('πŸ”„ Running migrations...');
        await AppDataSource.runMigrations();
        console.log('βœ… Migrations completed');
      } catch (migrationError) {
        console.warn('⚠️  Migration warning:', migrationError);
      }
    }

    // Initialize permanent admin user from environment variables
    await initializeAdmin();

    app.listen(PORT, '0.0.0.0', () => {
      console.log(`πŸš€ Server running on port ${PORT}`);
      console.log(`🌍 Server accessible at http://0.0.0.0:${PORT}`);
      console.log(`πŸ“š Swagger docs available at http://0.0.0.0:${PORT}/docs`);
      console.log(`❀️  Health check at http://0.0.0.0:${PORT}/health`);
    });
  })
  .catch((error) => {
    console.error('❌ Database connection failed:', error);
    console.error('Error details:', error.message);
    process.exit(1);
  });

// Graceful shutdown
process.on('SIGTERM', async () => {
  console.log('SIGTERM received, closing server...');
  await AppDataSource.destroy();
  process.exit(0);
});

process.on('SIGINT', async () => {
  console.log('SIGINT received, closing server...');
  await AppDataSource.destroy();
  process.exit(0);
});