mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 17:12:43 +00:00
fix: wait for config file validation
This commit is contained in:
committed by
Rainer Killinger
parent
90822f5888
commit
30082f8726
224
src/app.ts
224
src/app.ts
@@ -36,123 +36,131 @@ import {Elasticsearch} from './storage/elasticsearch/Elasticsearch';
|
|||||||
|
|
||||||
export const app = express();
|
export const app = express();
|
||||||
|
|
||||||
// only accept json as content type for all requests
|
async function configureApp() {
|
||||||
app.use(bodyParser.json({
|
// only accept json as content type for all requests
|
||||||
limit: '500kb',
|
app.use(bodyParser.json({
|
||||||
type: (req) => {
|
limit: '500kb',
|
||||||
const contentType = typeof req.headers['Content-Type'] === 'string' ?
|
type: (req) => {
|
||||||
req.headers['Content-Type'] : req.headers['content-type'];
|
const contentType = typeof req.headers['Content-Type'] === 'string' ?
|
||||||
if (typeof contentType === 'string' && contentType.match(/^application\/json/)) {
|
req.headers['Content-Type'] : req.headers['content-type'];
|
||||||
return true;
|
if (typeof contentType === 'string' && contentType.match(/^application\/json/)) {
|
||||||
} else {
|
return true;
|
||||||
throw new SCUnsupportedMediaTypeErrorResponse(process.env.NODE_ENV !== 'production');
|
} else {
|
||||||
}
|
throw new SCUnsupportedMediaTypeErrorResponse(process.env.NODE_ENV !== 'production');
|
||||||
},
|
}
|
||||||
})); // 500kb should be reasonably large
|
},
|
||||||
|
})); // 500kb should be reasonably large
|
||||||
|
|
||||||
// use morgan as a request logger
|
// use morgan as a request logger
|
||||||
// request loggers have to be the first middleware to be set in express
|
// request loggers have to be the first middleware to be set in express
|
||||||
app.use(morgan('dev'));
|
app.use(morgan('dev'));
|
||||||
|
|
||||||
const databases: {[name: string]: DatabaseConstructor} = {
|
const databases: {[name: string]: DatabaseConstructor} = {
|
||||||
elasticsearch: Elasticsearch,
|
elasticsearch: Elasticsearch,
|
||||||
};
|
};
|
||||||
|
|
||||||
// validate config file
|
// validate config file
|
||||||
export const scValidator = new Validator();
|
const scValidator = new Validator();
|
||||||
scValidator.addSchemas(join('node_modules', '@openstapps', 'core', 'lib', 'schema'));
|
await scValidator.addSchemas(join('node_modules', '@openstapps', 'core', 'lib', 'schema'));
|
||||||
|
|
||||||
// validate the config file
|
// validate the config file
|
||||||
const configValidation = scValidator.validate(config.util.toObject(), 'SCConfigFile');
|
const configValidation = scValidator.validate(config.util.toObject(), 'SCConfigFile');
|
||||||
|
|
||||||
// validation failed
|
// validation failed
|
||||||
if (configValidation.errors.length > 0) {
|
if (configValidation.errors.length > 0) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Validation of config file failed. Errors were: ' +
|
'Validation of config file failed. Errors were: ' +
|
||||||
JSON.stringify(configValidation.errors),
|
JSON.stringify(configValidation.errors),
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
// check if a database name was given
|
|
||||||
if (!config.has('internal.database.name')) {
|
|
||||||
throw new Error('You have to configure a database');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof mailer !== 'undefined') {
|
|
||||||
// set a mailQueue to use the backend mailer
|
|
||||||
if (config.has('internal.monitoring')) {
|
|
||||||
app.set('mailQueue', new MailQueue(mailer));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const database =
|
// check if a database name was given
|
||||||
new databases[config.get<string>('internal.database.name')](
|
if (!config.has('internal.database.name')) {
|
||||||
config.util.toObject(),
|
throw new Error('You have to configure a database');
|
||||||
app.get('mailQueue'),
|
}
|
||||||
|
|
||||||
|
if (typeof mailer !== 'undefined') {
|
||||||
|
// set a mailQueue to use the backend mailer
|
||||||
|
if (config.has('internal.monitoring')) {
|
||||||
|
app.set('mailQueue', new MailQueue(mailer));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const database =
|
||||||
|
new databases[config.get<string>('internal.database.name')](
|
||||||
|
config.util.toObject(),
|
||||||
|
app.get('mailQueue'),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (typeof database === 'undefined') {
|
||||||
|
throw new Error('No implementation for configured database found. Please check your configuration.');
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.ok('Validated config file sucessfully');
|
||||||
|
|
||||||
|
// make the validator available on the app
|
||||||
|
app.set('validator', scValidator);
|
||||||
|
|
||||||
|
// treats /foo and /foo/ as two different routes
|
||||||
|
// see http://expressjs.com/en/api.html#app.set
|
||||||
|
app.set('strict routing', true);
|
||||||
|
|
||||||
|
// make the bulk storage available to all http middlewares/routes
|
||||||
|
app.set(
|
||||||
|
'bulk',
|
||||||
|
new BulkStorage(database),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (typeof database === 'undefined') {
|
const corsOptions = {
|
||||||
throw new Error('No implementation for configured database found. Please check your configuration.');
|
allowedHeaders: [
|
||||||
|
'DNT',
|
||||||
|
'Keep-Alive',
|
||||||
|
'User-Agent',
|
||||||
|
'X-Requested-With',
|
||||||
|
'If-Modified-Since',
|
||||||
|
'Cache-Control',
|
||||||
|
'Content-Type',
|
||||||
|
'X-StApps-Version',
|
||||||
|
],
|
||||||
|
credentials: true,
|
||||||
|
maxAge: 1728000,
|
||||||
|
methods: ['GET', 'POST', 'PUT', 'OPTIONS'],
|
||||||
|
optionsSuccessStatus: 204,
|
||||||
|
};
|
||||||
|
|
||||||
|
// allow all origins on all routes
|
||||||
|
app.use(cors(corsOptions));
|
||||||
|
// TODO: See if it can handle options request with no content-type
|
||||||
|
|
||||||
|
// allow cors preflight requests on every route
|
||||||
|
app.options('*', cors(corsOptions));
|
||||||
|
|
||||||
|
app.set('isProductiveEnvironment', process.env.NODE_ENV !== 'production');
|
||||||
|
|
||||||
|
// load routes before plugins
|
||||||
|
// they now can be used or overwritten by any plugin
|
||||||
|
app.use(
|
||||||
|
bulkAddRouter,
|
||||||
|
bulkDoneRouter,
|
||||||
|
bulkRouter,
|
||||||
|
indexRouter,
|
||||||
|
multiSearchRouter,
|
||||||
|
searchRouter,
|
||||||
|
thingUpdateRouter,
|
||||||
|
);
|
||||||
|
|
||||||
|
// add a route for a missing resource (404)
|
||||||
|
app.use((_req, res) => {
|
||||||
|
const errorResponse = new SCNotFoundErrorResponse(process.env.NODE_ENV !== 'production');
|
||||||
|
res.status(errorResponse.statusCode);
|
||||||
|
res.json(errorResponse);
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: implement a route to register plugins
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.ok('Validated config file sucessfully');
|
configureApp().then(() => {
|
||||||
|
logger.ok('Sucessfully configured express server');
|
||||||
// make the validator available on the app
|
}).catch((err) => {
|
||||||
app.set('validator', scValidator);
|
throw err;
|
||||||
|
|
||||||
// treats /foo and /foo/ as two different routes
|
|
||||||
// see http://expressjs.com/en/api.html#app.set
|
|
||||||
app.set('strict routing', true);
|
|
||||||
|
|
||||||
// make the bulk storage available to all http middlewares/routes
|
|
||||||
app.set(
|
|
||||||
'bulk',
|
|
||||||
new BulkStorage(database),
|
|
||||||
);
|
|
||||||
|
|
||||||
const corsOptions = {
|
|
||||||
allowedHeaders: [
|
|
||||||
'DNT',
|
|
||||||
'Keep-Alive',
|
|
||||||
'User-Agent',
|
|
||||||
'X-Requested-With',
|
|
||||||
'If-Modified-Since',
|
|
||||||
'Cache-Control',
|
|
||||||
'Content-Type',
|
|
||||||
'X-StApps-Version',
|
|
||||||
],
|
|
||||||
credentials: true,
|
|
||||||
maxAge: 1728000,
|
|
||||||
methods: ['GET', 'POST', 'PUT', 'OPTIONS'],
|
|
||||||
optionsSuccessStatus: 204,
|
|
||||||
};
|
|
||||||
|
|
||||||
// allow all origins on all routes
|
|
||||||
app.use(cors(corsOptions));
|
|
||||||
// TODO: See if it can handle options request with no content-type
|
|
||||||
|
|
||||||
// allow cors preflight requests on every route
|
|
||||||
app.options('*', cors(corsOptions));
|
|
||||||
|
|
||||||
app.set('isProductiveEnvironment', process.env.NODE_ENV !== 'production');
|
|
||||||
|
|
||||||
// load routes before plugins
|
|
||||||
// they now can be used or overwritten by any plugin
|
|
||||||
app.use(
|
|
||||||
bulkAddRouter,
|
|
||||||
bulkDoneRouter,
|
|
||||||
bulkRouter,
|
|
||||||
indexRouter,
|
|
||||||
multiSearchRouter,
|
|
||||||
searchRouter,
|
|
||||||
thingUpdateRouter,
|
|
||||||
);
|
|
||||||
|
|
||||||
// add a route for a missing resource (404)
|
|
||||||
app.use((_req, res) => {
|
|
||||||
const errorResponse = new SCNotFoundErrorResponse(process.env.NODE_ENV !== 'production');
|
|
||||||
res.status(errorResponse.statusCode);
|
|
||||||
res.json(errorResponse);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: implement a route to register plugins
|
|
||||||
|
|||||||
Reference in New Issue
Block a user