gravadigital / generator-grava-node-api

MIT License
1 stars 0 forks source link

Add beforeInit and afterInit in app flow #25

Open lautaroalvarez opened 2 years ago

lautaroalvarez commented 2 years ago

In app.js add two new functions: beforeInit and afterInit

They should be async functions because we want to wait it before continue.

Then, in the bin/init.js add this two functions in the "wake-up flow":

Should be something like this:

return app.connectMongoose()
    .then(() => {
        application = app.initialize();
        return app.beforeInit();
    })
    .then(() => {
        application.listen(process.env.SERVER_PORT);
        logger.info(`Your server is listening on port ${process.env.SERVER_PORT}`);
        app.afterInit();
    })
    .catch((error) => {
        logger.error('APP STOPPED');
        logger.error(error.stack);
        return process.exit(1);
    });

The scheduleRunner(); and createDefaultAdmin(); should be inside the afterInit function.

Tests

In tests we have to call this new functions. Its something like this:

function start() {
    return connectMongoose()
        .then(() => {
            return app.beforeInit();
        })
        .then(() => {
            return app.initialize();
        });
}