-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathserver.ts
117 lines (100 loc) · 3 KB
/
server.ts
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
import cors from 'cors';
import express, { json, urlencoded, Express } from 'express';
import 'express-async-errors';
import helmet from 'helmet';
import logger from 'jet-logger';
import morgan from 'morgan';
import fileUpload from 'express-fileupload';
import { ErrorHandler } from '@/error/ErrorHandler';
import { seedData, seedAdminUsers } from '@/database/seeder/app';
import { setupMigrator } from './database/migration';
import { connectDatabase } from './database/connection';
import { baseRouter } from './routes';
import { envCheck } from './pre-start/envCheck';
import { requiredEnvVariables } from './pre-start/env-required';
/**
* Connect to database, run necessary migrations, and seed fake data,
* depending on NODE_ENV
*
* @param {string} [NODE_ENV='development']
* @returns {Promise<void>}
*/
const setupDatabase = async (NODE_ENV = 'development'): Promise<void> => {
let db;
// Check for required ENV variables
envCheck(requiredEnvVariables);
// Connect to database
if (NODE_ENV === 'testing') {
logger.info('Connecting to test database…');
db = await connectDatabase({
MONGO_DB: 'praise_db_testing_tmp',
});
logger.info('Connected to test database.');
} else {
logger.info('Connecting to database…');
db = await connectDatabase();
logger.info('Connected to database.');
}
// Checks database migrations and run them if they are not already applied
logger.info('Checking for pending migrations…');
const umzug = setupMigrator(db.connection);
const migrations = await umzug.pending();
logger.info(`Found ${migrations.length} pending migrations`);
await umzug.up();
if (migrations.length > 0) {
logger.info('Migrations complete.');
}
// Seed database with fake data in 'development' environment
if (NODE_ENV === 'development') {
await seedData();
}
};
/**
* Prepare and initialize express server
*
* @param {string} [NODE_ENV='development']
* @returns {Promise<Express>}
*/
const setupApiServer = async (NODE_ENV = 'development'): Promise<Express> => {
const app = express();
//app.use((req, res, next) => setTimeout(next, 1000)); // Delay response during testing
app.use(
fileUpload({
createParentPath: true,
})
);
app.use(
cors({
origin: '*',
})
);
app.use(json());
app.use(urlencoded({ extended: true }));
// Serve static files
app.use('/uploads', express.static('uploads'));
// API routes
app.use('/api', baseRouter);
// Error handling
app.use(ErrorHandler);
if (NODE_ENV === 'development') {
await seedAdminUsers();
app.use(morgan('dev'));
} else if (NODE_ENV === 'production') {
await seedAdminUsers();
app.use(helmet());
} else if (NODE_ENV === 'testing') {
await seedAdminUsers();
app.use(morgan('dev'));
}
return app;
};
/**
* Run application
*
* @returns {Promise<Express>}
*/
export const setup = async (): Promise<Express> => {
await setupDatabase(process.env.NODE_ENV);
const app = setupApiServer(process.env.NODE_ENV);
return app;
};