Skip to content

Commit

Permalink
set default value for routePrefix, add some debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed May 7, 2024
1 parent 1f44f3d commit 3e93665
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions bin/counterfact.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ program
.option(
"--prefix <string>",
"base path from which routes will be served (e.g. /api/v1)",
"",
)
.action(main)
// eslint-disable-next-line sonar/process-argv
Expand Down
6 changes: 6 additions & 0 deletions src/server/koa-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { IncomingHttpHeaders } from "node:http";

import createDebug from "debug";
import type Koa from "koa";
import koaProxy from "koa-proxy";

import type { Config } from "./config.js";
import type { Dispatcher } from "./dispatcher.js";
import type { HttpMethods } from "./registry.js";

const debug = createDebug("counterfact:server:create-koa-app");

const HTTP_STATUS_CODE_OK = 200;

function addCors(ctx: Koa.ExtendableContext, headers?: IncomingHttpHeaders) {
Expand Down Expand Up @@ -58,6 +61,9 @@ export function koaMiddleware(
return async function middleware(ctx, next) {
const { proxyEnabled, proxyUrl, routePrefix } = config;

debug("middleware running for path: %s", ctx.request.path);
debug("routePrefix: %s", routePrefix);

if (!ctx.request.path.startsWith(routePrefix)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return await next();
Expand Down
5 changes: 3 additions & 2 deletions src/server/page-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export function pageMiddleware(
.join(__dirname, `../client/${templateName}.html.hbs`)
.replaceAll("\\", "/");

debug("pathToHandlebarsTemplate: %s", pathToHandlebarsTemplate);

const render = Handlebars.compile(await readFile(pathToHandlebarsTemplate));

if (ctx.URL.pathname === pathname) {
debug("rendering page: %s", pathname);
debug("locals: %o", locals);

ctx.body = render(locals);

return;
Expand Down

0 comments on commit 3e93665

Please sign in to comment.