Last active
February 19, 2019 16:04
-
-
Save ozanmuyes/bfa1b6adc535df06bce4ad84642be909 to your computer and use it in GitHub Desktop.
A proper application code to start the server and a service to connect to the database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// index.js | |
const http = require('http'); | |
const kernel = require('@rezeus/kernel'); | |
require('./services_database'); | |
kernel.on('error', (err) => { | |
// TODO Handle error | |
}); | |
kernel.once('booted', () => { | |
// Kernel was booted. Here you can start the server. | |
// Code excerpted from https://nodejs.org/en/docs/guides/getting-started-guide/ | |
// and slightly modified | |
const server = http.createServer(server.callback()); | |
const server = http.createServer((req, res) => { | |
// Now you can use Sequelize to query some data; | |
// kernel.sequelize.query('SELECT * FROM my_table').then((result) => { ... }).catch(...); | |
res.statusCode = 200; | |
res.setHeader('Content-Type', 'text/plain'); | |
res.end('Hello World\n'); | |
}); | |
server.listen('127.0.0.1', 3000, () => { | |
console.log('Server running on port 3000'); | |
}); | |
kernel.once('shutdown_server', () => new Promise((resolve) => { | |
server.close(() => { resolve(); }); | |
})); | |
}); | |
// First register the listener against the kernel... | |
kernel.on('shutted_down', () => { | |
process.exit(); | |
}); | |
process.on('SIGTERM', () => { | |
// ...and then start shutting down (i.e. graceful shutdown) | |
kernel.shutdown(); | |
// NOTE Beware that the `SIGTERM` signal may be signalled multiple times (hance `.on()` not `.once()`) | |
}); | |
kernel.boot(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// services/database.js | |
const Sequelize = require('sequelize'); | |
const kernel = require('@rezeus/kernel'); | |
kernel.on('booting_backing', () => new Promise((resolve, reject) => { | |
// Code excerpted from http://docs.sequelizejs.com/manual/installation/getting-started | |
// and slightly modified | |
// Setting up a connection | |
const sequelize = new Sequelize('database', 'username', 'password', { | |
host: 'localhost', | |
dialect: 'mysql', | |
operatorsAliases: false, | |
pool: { | |
max: 5, | |
min: 0, | |
acquire: 30000, | |
idle: 10000 | |
}, | |
}); | |
// Test the connection | |
sequelize | |
.authenticate() | |
.then(() => { | |
console.log('Connection has been established successfully.'); | |
kernel.sequelize = sequelize; | |
resolve(); | |
}) | |
.catch((err) => { | |
console.error('Unable to connect to the database:', err); | |
reject(err); | |
}); | |
// NOTE We are just returning the result of the `.close()` method since the return value is Promise | |
kernel.on('shutdown_service', () => sequelize.close()); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment