Skip to content

Commit

Permalink
feat(indiekit): gracefully shutdown server
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Jan 6, 2024
1 parent 530f25a commit bbf2f8b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
42 changes: 25 additions & 17 deletions packages/indiekit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const Indiekit = class {
this.application = this.config.application;
this.plugins = this.config.plugins;
this.publication = this.config.publication;
this.client = getMongodbClient(this.application.mongodbUrl);
}

static async initialize(options = {}) {
Expand Down Expand Up @@ -53,11 +54,16 @@ export const Indiekit = class {
}

async bootstrap() {
const client = await getMongodbClient(this.application.mongodbUrl);
// Check for required configuration options
if (!this.publication.me) {
console.error("No publication URL in configuration");
console.info("https://getindiekit.com/configuration/#publication-me-url");
process.exit();
}

// Setup database
if (client) {
const database = client.db("indiekit");
if (this.client) {
const database = this.client.db("indiekit");

this.application.hasDatabase = true;
this.application.cache = new Keyv({
Expand All @@ -84,25 +90,27 @@ export const Indiekit = class {
return this;
}

stop(server, name) {
server.close(() => {
console.info(`Stopping ${name}`);
this.client.close();
process.exit(0);
});
}

async server(options = {}) {
const config = await this.bootstrap();
const { application, publication } = config;

// Check for required configuration options
if (!publication.me) {
console.error("No publication URL in configuration");
console.info(
"See https://getindiekit.com/configuration/#publication-me-url",
);
process.exit();
}

const { name, version } = application;
const port = options.port || application.port;
const { name, version } = config.application;
const port = options.port || config.application.port;
const app = expressConfig(config);

return app.listen(port, () => {
const server = app.listen(port, () => {
console.info(`Starting ${name} (v${version}) on port ${port}`);
});

process.on("SIGINT", () => this.stop(server, name));
process.on("SIGTERM", () => this.stop(server, name));

return server;
}
};
5 changes: 2 additions & 3 deletions packages/indiekit/lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { MongoClient } from "mongodb";
/**
* Connect to MongoDB client
* @param {string} mongodbUrl - MongoDB URL
* @returns {Promise<import('mongodb').MongoClient>} MongoDB client
* @returns {import('mongodb').MongoClient|undefined} MongoDB client
*/
export const getMongodbClient = async (mongodbUrl) => {
export const getMongodbClient = (mongodbUrl) => {
if (mongodbUrl) {
try {
const client = new MongoClient(mongodbUrl, {
connectTimeoutMS: 5000,
});
await client.connect();

return client;
} catch (error) {
Expand Down

0 comments on commit bbf2f8b

Please sign in to comment.