File size: 2,801 Bytes
eda9b8d
 
 
 
 
 
 
 
 
7853dfb
eda9b8d
 
 
 
e23c8e9
 
 
 
eda9b8d
e23c8e9
 
 
 
 
 
 
eda9b8d
 
 
 
 
 
59c9279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eda9b8d
59c9279
eda9b8d
 
 
 
 
 
 
 
7853dfb
eda9b8d
3d52d95
 
 
7bd8a28
 
 
 
 
eda9b8d
 
 
 
 
 
 
 
 
 
 
ece569a
 
 
e3eaa0d
 
ece569a
 
 
 
 
5134e36
 
 
5ec1688
eda9b8d
 
 
 
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
require('dotenv').config();
const express = require('express');
const cors = require('cors');

const authRoutes = require('./routes/auth');
const workspaceRoutes = require('./routes/workspaces');
const threadRoutes = require('./routes/threads');
const messageRoutes = require('./routes/messages');
const linkRoutes = require('./routes/links');
const notificationRoutes = require('./routes/notifications');

const app = express();

// Middleware
const allowedOrigins = process.env.FRONTEND_URL
  ? process.env.FRONTEND_URL.split(',').map(url => url.trim())
  : ['http://localhost:3000'];

app.use(cors({
  origin: (origin, callback) => {
    if (!origin || allowedOrigins.includes(origin)) {
      callback(null, true);
    } else {
      callback(new Error('Not allowed by CORS'));
    }
  },
  credentials: true,
}));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Health check
app.get('/health', async (req, res) => {
  const health = {
    status: 'ok',
    timestamp: new Date().toISOString(),
    uptime: process.uptime(),
    services: {
      database: 'unknown'
    }
  };

  try {
    const prisma = require('./lib/prisma');
    await prisma.$queryRaw`SELECT 1`;
    health.services.database = 'healthy';
  } catch (error) {
    health.status = 'error';
    health.services.database = 'unhealthy';
    health.error = error.message;
  }

  const httpStatus = health.status === 'ok' ? 200 : 503;
  res.status(httpStatus).json(health);
});

app.get('/api/health', (req, res) => {
  res.redirect('/health');
});

// Routes
app.use('/api/auth', authRoutes);
app.use('/api/workspaces', workspaceRoutes);
app.use('/api/threads', threadRoutes);
app.use('/api/messages', messageRoutes);
app.use('/api/links', linkRoutes);
app.use('/api/notifications', notificationRoutes);

const adminRoutes = require('./routes/admin');
app.use('/api/admin', adminRoutes);

// Root health check for platforms that ping /
app.get('/', (req, res) => {
  res.status(200).send('BackLink Exchange API is running');
});

// 404 handler
app.use((req, res) => {
  res.status(404).json({ error: 'Route not found' });
});

// Error handler
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({ error: 'Internal server error' });
});

const http = require('http');
const wsManager = require('./lib/ws');

// Force 7860 on Hugging Face Spaces (which sets SPACE_ID), otherwise use .env PORT or default to 4000
const PORT = process.env.SPACE_ID ? 7860 : (process.env.PORT || 4000);
const server = http.createServer(app);

// Initialize WebSockets
wsManager.init(server);

// Initialize Matcher Cron Job
require('./jobs/matcher');

server.listen(PORT, '0.0.0.0', () => {
  console.log(`🚀 BackLink Exchange API running on port ${PORT}`);
});

module.exports = app;