Skip to content

Commit

Permalink
fix: move server setup to knative-microservice package
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickleet committed Nov 12, 2022
1 parent e1294bb commit 60cc215
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 179 deletions.
81 changes: 0 additions & 81 deletions __tests__/unit/bin/start.ts

This file was deleted.

17 changes: 0 additions & 17 deletions __tests__/unit/lib/healthcheck.ts

This file was deleted.

24 changes: 21 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
"axios": "1.1.3",
"axios-retry": "3.3.1",
"debug": "4.3.4",
"express": "4.18.2",
"express-pino-logger": "7.0.0",
"knative-microservice": "1.0.2",
"knativebus": "2.3.17",
"pino": "8.7.0",
"register-server-handlers": "4.2.4",
"sourced": "4.0.6",
"sourced-queued-repo-promise": "1.1.0",
"sourced-repo-typeorm": "3.2.7",
Expand Down
87 changes: 15 additions & 72 deletions src/bin/start.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
import path from "path";
import express, { Router } from "express";
import pino from "pino";
import pinoLoggerMiddleware from "express-pino-logger";
import { persistenceLayer } from "sourced-repo-typeorm";
import { registerHandlers } from "register-server-handlers";
import { config } from "../config.js";
import { healthcheck } from "../lib/healthcheck.js";
import { microservice } from "knative-microservice";

const { port, sourced, handlerBasePath } = config;
const handlersPath = path.resolve(process.cwd(), handlerBasePath, "handlers");

const logger = pino();
const pinoLogger = pinoLoggerMiddleware({ logger });
const server = express();
export const start = async () => {
const logger = pino();

const healthRouter = Router();
const appRouter = Router();

let listeningServer;

// parse application/x-www-form-urlencoded
server.use(express.urlencoded({ extended: false }));
// parse application/json
server.use(express.json({ limit: "50mb" }));

// enable request logging
appRouter.use(pinoLogger);

export const start = async (server, handlersPath: string) => {
const connectionOptions = {
type: "postgres" as const,
url: sourced.psql.url,
Expand All @@ -39,6 +21,7 @@ export const start = async (server, handlersPath: string) => {
},
},
};

logger.info({
msg: "⏳ connecting to psql",
sync: connectionOptions.synchronize,
Expand All @@ -52,60 +35,20 @@ export const start = async (server, handlersPath: string) => {
}
logger.info("✅ connected to psql");

healthRouter.get("/", healthcheck);
server.use("/health", healthRouter);

logger.info(`⏳ registering server handlers from ${handlersPath}`);
// register handlers as HTTP Post handlers
await registerHandlers({
server: appRouter,
path: handlersPath,
handlerOptions: {
sync: config.enableSyncSendToDenormalizer,
enableEventPublishing: config.enableEventPublishing,
},
});

logger.info(
`⏳ registering server cloud event handlers from ${handlersPath}`
);
// register handlers as KNative Cloud Event Handlers
await registerHandlers({
server: appRouter,
path: handlersPath,
cloudevents: true,
serverPath: "/cloudevent/",
handlerOptions: {
sync: false,
enableEventPublishing: config.enableEventPublishing,
const { server, shutdown, onListen } = await microservice({
handlers: {
path: handlersPath,
options: {
enableSyncSendToDenormalizer: config.enableSyncSendToDenormalizer,
enableEventPublishing: config.enableEventPublishing,
},
},
logger,
});

server.use("/", appRouter);

logger.info(`✅ handlers registered`);
server.listen(port, onListen(port));

listeningServer = server.listen(port, onListen(port));
process.on("SIGTERM", shutdown(persistenceLayer));
};

const onListen = (port) => {
logger.info(`🚀 Server listening on port ${port}`);
};

export const shutdown = (server) => async () => {
logger.info("🛑 Received SIGTERM, shutting down...");
if (server && server.close) {
await server.close();
logger.info("🛑 Server closed");
}
if (persistenceLayer && persistenceLayer.disconnect) {
await persistenceLayer.disconnect();
logger.info("🛑 Disconnected from PSQL");
}
logger.info("🛑 Exiting...");
return process.exit(0);
};

process.on("SIGTERM", shutdown(listeningServer));

start(server, handlersPath);
await start();
3 changes: 0 additions & 3 deletions src/lib/healthcheck.ts

This file was deleted.

0 comments on commit 60cc215

Please sign in to comment.