Socialify

Folder ..

Viewing app.ts
85 lines (72 loc) • 2.1 KB

 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
import express, { Request, Response, NextFunction } from 'express';
import { initializeDatabase } from './database/data-source';
import Logger from './utils/logger';
import routes from './routes';
import { taskManager } from './tasks/TaskManager';
import { fribbSyncTask } from './tasks/FribbSyncTask';

const app = express();
const port = process.env.PORT || 3000;

app.use(express.json());

// Request logging middleware
app.use((req: Request, _res: Response, next: NextFunction) => {
    Logger.info(`${req.method} ${req.url}`, {
        timestamp: true,
        prefix: 'Request'
    });
    next();
});

// Mount routes
app.use('/', routes);

// Error handling middleware
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
    Logger.error(err, {
        timestamp: true,
        prefix: `Request ${req.method} ${req.url}`
    });

    if (res.headersSent) {
        return next(err);
    }

    res.status(500).json({
        error: process.env.NODE_ENV === 'production'
            ? 'Internal Server Error'
            : err.message
    });
});

// 404 handler
app.use((_req: Request, res: Response) => {
    res.status(404).json({
        error: 'Not Found',
        message: 'The requested resource was not found'
    });
});

// Initialize tasks and start server
const startServer = async () => {
    try {
        // Initialize database
        await initializeDatabase();

        // Register and start tasks
        taskManager.registerTask(fribbSyncTask);
        await taskManager.startAllTasks();

        // Start server
        app.listen(port, () => {
            Logger.success(`Server running at http://localhost:${port}`, {
                timestamp: true,
                prefix: 'Server'
            });
        });
    } catch (error) {
        Logger.error('Failed to start server', {
            timestamp: true,
            prefix: 'Server'
        });
        Logger.debug(error as string);
        process.exit(1);
    }
};

// Handle graceful shutdown
process.on('SIGTERM', () => {
    taskManager.stopAllTasks();
    process.exit(0);
});

startServer();

export default app;