File size: 3,658 Bytes
b5e5eac
 
8058f3a
2fe81ea
 
b5e5eac
 
 
2fe81ea
b5e5eac
 
 
 
2fe81ea
 
 
b5e5eac
 
 
d931d25
aeffa8d
c1670a4
d931d25
 
b5e5eac
9c67ac5
b5e5eac
 
9c67ac5
4a285d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5e5eac
 
 
 
65b0171
 
 
 
 
ef85da9
 
 
65b0171
2fe81ea
b5e5eac
2fe81ea
 
 
 
b5e5eac
 
 
 
 
8058f3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5e5eac
b2cd2a3
 
8058f3a
b2cd2a3
 
b5e5eac
 
8058f3a
b5e5eac
b2cd2a3
 
48f4e54
 
b2cd2a3
 
 
 
 
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
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
import express, { Application } from 'express';
import cors from 'cors';
import path from 'path';
import swaggerUi from 'swagger-ui-express';
import { swaggerSpec } from './docs/swagger';

// Routes
import authRoutes from './routes/authRoutes';
import creatorAuthRoutes from './routes/creatorAuthRoutes';
import agentRoutes from './routes/agentRoutes';
import chatRoutes from './routes/chatRoutes';
import subscriptionRoutes from './routes/subscriptionRoutes';
import walletRoutes from './routes/walletRoutes';
import userRoutes from './routes/userRoutes';
import creatorRoutes from './routes/creatorRoutes';
import adminRoutes from './routes/adminRoutes';

const app: Application = express();

// Trust proxy for Hugging Face Spaces
app.set('trust proxy', 1);

// Simple CORS - allow all
app.use(cors());

// Body parsing
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));

app.get('/', (req, res) => {
  res.json({
    name: 'Zurri API',
    version: '1.0.0',
    description: 'Zurri Agents Marketplace API with Wallet Point System',
    status: 'running',
    timestamp: new Date().toISOString(),
    endpoints: {
      docs: '/docs',
      health: '/health',
      api: '/api',
    },
    note: 'Frontend will be built in a separate milestone',
  });
});

app.get('/health', (req, res) => {
  res.json({ status: 'ok', timestamp: new Date().toISOString() });
});

app.use('/docs', swaggerUi.serve);
app.get('/docs', swaggerUi.setup(swaggerSpec, {
  explorer: true,
  customCss: '.swagger-ui .topbar { display: none }',
  customSiteTitle: 'Zurri API Documentation',
  swaggerOptions: {
    persistAuthorization: true,
  },
}));

app.use('/api/auth', authRoutes);
app.use('/api/creator-auth', creatorAuthRoutes);
app.use('/api/users', userRoutes);
app.use('/api/creators', creatorRoutes);
app.use('/api/admin', adminRoutes);
app.use('/api/agents', agentRoutes);
app.use('/api/chat', chatRoutes);
app.use('/api/subscriptions', subscriptionRoutes);
app.use('/api/wallet', walletRoutes);

// Serve frontend static files if they exist
const frontendPath = path.join(__dirname, '../../zurri-mock-frontend/dist');
let frontendExists = false;
try {
  const fs = require('fs');
  if (fs.existsSync(frontendPath) && fs.existsSync(path.join(frontendPath, 'index.html'))) {
    app.use(express.static(frontendPath));
    frontendExists = true;
  }
} catch (error) {
  // Frontend not available, continue without it
}

// Serve React app for all non-API routes (SPA routing)
// This must come after all API routes but before 404 handler
if (frontendExists) {
  app.get('*', (req, res, next) => {
    // Skip if it's an API, docs, health, or root route
    if (req.path.startsWith('/api') || req.path.startsWith('/docs') || req.path.startsWith('/health') || req.path === '/') {
      return next();
    }
    // Serve React app for all other routes (dashboard, wallet, etc.)
    res.sendFile(path.join(frontendPath, 'index.html'));
  });
}

// 404 handler - only for API routes or if frontend doesn't exist
app.use((req, res) => {
  if (req.path.startsWith('/api')) {
    res.status(404).json({ error: 'Route not found' });
  } else if (!frontendExists) {
    res.status(404).send('Not found');
  }
});

// Simplified error handler (must be last)
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
  if (process.env.NODE_ENV === 'development') {
    console.error('Error:', err.message);
  }
  
  if (req.path.startsWith('/api')) {
    res.status(500).json({ error: 'Server error' });
  } else {
    res.status(500).send('Server error');
  }
});

export default app;