Skip to content

Commit

Permalink
feat(indiekit): add debug logs to troubleshoot Indiekit/Express serve…
Browse files Browse the repository at this point in the history
…r configuration
  • Loading branch information
jackdbd authored and paulrobertlloyd committed Aug 24, 2024
1 parent 1996a3d commit d02b5b8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
8 changes: 8 additions & 0 deletions packages/indiekit/config/express.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import compression from "compression";
import makeDebug from "debug";
import express from "express";
import fileUpload from "express-fileupload";
import { templates } from "@indiekit/frontend";
Expand All @@ -10,12 +11,15 @@ import { logging } from "../lib/middleware/logging.js";
import { routes } from "../lib/routes.js";
import { views } from "../lib/views.js";

const debug = makeDebug(`indiekit:express`);

/**
* @typedef {import("express").Application} Application
* @param {object} indiekitConfig - Indiekit configuration
* @returns {Application} Express application
*/
export const expressConfig = (indiekitConfig) => {
debug(`create Express app`);
const app = express();

// Enable reversed proxy connections
Expand All @@ -24,6 +28,8 @@ export const expressConfig = (indiekitConfig) => {
// Don’t advertise server details
app.set("x-powered-by", false);

debug(`add middlewares`);

// Body parsers
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
Expand All @@ -48,6 +54,7 @@ export const expressConfig = (indiekitConfig) => {
app.use(logging);

// Views
debug(`add view engine and templates`);
app.set("views", views(indiekitConfig));
app.engine("njk", templates(app).render);
app.set("view engine", "njk");
Expand All @@ -56,6 +63,7 @@ export const expressConfig = (indiekitConfig) => {
app.use(routes(indiekitConfig));

// Handle errors
debug(`add error handling middlewares`);
app.use(error.notFound, error.internalServer);

return app;
Expand Down
32 changes: 25 additions & 7 deletions packages/indiekit/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
import process from "node:process";
import makeDebug from "debug";
import Keyv from "keyv";
import KeyvMongo from "@keyv/mongo";
import { expressConfig } from "./config/express.js";
Expand All @@ -12,6 +13,8 @@ import { getPostTemplate } from "./lib/post-template.js";
import { getPostTypes } from "./lib/post-types.js";
import { getMediaStore, getStore } from "./lib/store.js";

const debug = makeDebug(`indiekit:index`);

export const Indiekit = class {
/**
* @private
Expand Down Expand Up @@ -55,10 +58,12 @@ export const Indiekit = class {

addPreset(preset) {
this.publication.preset = preset;
debug(`added publication preset: ${preset.name}`);
}

addStore(store) {
this.application.stores.push(store);
debug(`added content store: ${store.name}`);
}

addSyndicator(syndicator) {
Expand All @@ -67,41 +72,51 @@ export const Indiekit = class {
...this.publication.syndicationTargets,
...syndicator,
];
const names = this.publication.syndicationTargets.map(
(target) => target.name,
);
debug(`added ${names.length} syndication target/s: ${names.join(", ")}`);
}

async bootstrap() {
debug(`bootstrap - check for required configuration options`);
// Check for required configuration options
if (!this.publication.me) {
console.error("No publication URL in configuration");
console.info("https://getindiekit.com/configuration/publication#me");
process.exit();
}

// Connect to database client
const mongodbClient = await getMongodbClient(this.application.mongodbUrl);
const mongodbClientOrError = await getMongodbClient(
this.application.mongodbUrl,
);

if (mongodbClient?.client) {
this.client = mongodbClient.client;
if (mongodbClientOrError?.client) {
this.client = mongodbClientOrError.client;

// Get database name from connection string
let { databaseName } = this.client.db();

// If no database given, use ‘indiekit’ as default database, not ‘test’
databaseName = databaseName === "test" ? "indiekit" : databaseName;

// Set database
debug(`bootstrap - connect to MongoDB database ${databaseName}`);
const database = this.client.db(databaseName);

this.application.hasDatabase = true;
this.application.cache = new Keyv(
new KeyvMongo(this.application.mongodbUrl),
);

debug(`bootstrap - add database collection posts`);
this.application.posts = database.collection("posts");

debug(`bootstrap - add database collection media`);
this.application.media = database.collection("media");
}

if (mongodbClient?.error) {
this.application._mongodbClientError = mongodbClient.error;
if (mongodbClientOrError?.error) {
this.application._mongodbClientError = mongodbClientOrError.error;
}

// Update application configuration
Expand Down Expand Up @@ -137,10 +152,13 @@ export const Indiekit = class {
const app = expressConfig(config);

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

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

return server;
Expand Down

0 comments on commit d02b5b8

Please sign in to comment.