Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP Server improvements / testing #663

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/create/templates/hello-v2/src/operations.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DBOS } from "@dbos-inc/dbos-sdk";
import { Hello, dbos_hello } from "./operations";
//import request from "supertest";
import request from "supertest";

describe("operations-test", () => {
beforeAll(async () => {
Expand All @@ -22,4 +22,15 @@ describe("operations-test", () => {
const rows = await DBOS.executor.queryUserDB("SELECT * FROM dbos_hello WHERE name=$1", ["dbos"]) as dbos_hello[];
expect(rows[0].greet_count).toBe(1);
});

/**
* Test the HTTP endpoint.
*/
test("test-endpoint", async () => {
const res = await request(DBOS.getHTTPHandlersCallback()).get(
"/greeting/dbos"
);
expect(res.statusCode).toBe(200);
expect(res.text).toMatch("Hello, dbos! You have been greeted");
});
});
24 changes: 24 additions & 0 deletions src/dbos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export class DBOS {
// Lifecycle
///////
static adminServer: Server | undefined = undefined;
static appServer: Server | undefined = undefined;

static setConfig(config: DBOSConfig, runtimeConfig?: DBOSRuntimeConfig) {
DBOS.dbosConfig = config;
Expand Down Expand Up @@ -166,6 +167,14 @@ export class DBOS {
});
}

// Create the DBOS HTTP server
// This may be a no-op if there are no registered endpoints
const server = new DBOSHttpServer(DBOSExecutor.globalInstance);
if (DBOS.runtimeConfig) {
// This will not listen if there's no decorated endpoint
DBOS.appServer = await server.appListen(DBOS.runtimeConfig.port);
}

if (httpApps) {
if (httpApps.koaApp) {
DBOS.logger.info("Setting up Koa tracing middleware");
Expand All @@ -191,6 +200,12 @@ export class DBOS {
}

static async shutdown() {
// Stop the app server
if (DBOS.appServer) {
DBOS.appServer.close();
DBOS.appServer = undefined;
}

// Stop the admin server
if (DBOS.adminServer) {
DBOS.adminServer.close();
Expand All @@ -212,6 +227,15 @@ export class DBOS {
return DBOSExecutor.globalInstance as DBOSExecutorContext;
}

// This retrieves the HTTP handlers callback for DBOS HTTP.
// (This is the one that handles the @DBOS.getApi, etc., methods.)
// Useful for testing purposes, or to combine the DBOS service with routes.
// If you are using your own HTTP server, this won't return anything.
static getHTTPHandlersCallback() {
if (!DBOSHttpServer.instance) return undefined;
return DBOSHttpServer.instance.app.callback();
}

//////
// Globals
//////
Expand Down
19 changes: 14 additions & 5 deletions src/httpServer/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class DBOSHttpServer {
readonly applicationRouter: Router;
readonly logger: Logger;
static nRegisteredEndpoints: number = 0;
static instance?: DBOSHttpServer = undefined;

/**
* Create a Koa app.
Expand All @@ -49,6 +50,8 @@ export class DBOSHttpServer {

DBOSHttpServer.registerDecoratedEndpoints(this.dbosExec, this.applicationRouter, this.app);
this.app.use(this.applicationRouter.routes()).use(this.applicationRouter.allowedMethods());

DBOSHttpServer.instance = this;
}

static setupAdminApp(dbosExec: DBOSExecutor): Koa {
Expand All @@ -71,17 +74,23 @@ export class DBOSHttpServer {
* @param port
*/
async listen(port: number, adminPort: number) {
const appServer = await this.appListen(port);

// TODO we should check adminPort as well. This is done elsewhere though...
const adminServer = this.adminApp.listen(adminPort, () => {
this.logger.info(`DBOS Admin Server is running at http://localhost:${adminPort}`);
});
return { appServer: appServer, adminServer: adminServer };
}

async appListen(port: number) {
await DBOSHttpServer.checkPortAvailabilityIPv4Ipv6(port, this.logger);
// TODO we should check adminPort as well.

const appServer = DBOSHttpServer.nRegisteredEndpoints === 0 ? undefined : this.app.listen(port, () => {
this.logger.info(`DBOS Server is running at http://localhost:${port}`);
});

const adminServer = this.adminApp.listen(adminPort, () => {
this.logger.info(`DBOS Admin Server is running at http://localhost:${adminPort}`);
});
return { appServer: appServer, adminServer: adminServer };
return appServer;
}

static async checkPortAvailabilityIPv4Ipv6(port: number, logger: Logger) {
Expand Down
Loading