Skip to content

Commit 905c23b

Browse files
authored
(EAI-49): Add static site endpoint to mongodb-chatbot-server + include in implementations (#255)
* add static site to server package * make packages use the updated version * update lock file * lock fix again * dep fix * trigger staging build * Apply suggestions from code review * Delete chat-server/trigger.me
1 parent 6ef3833 commit 905c23b

File tree

17 files changed

+5911
-9008
lines changed

17 files changed

+5911
-9008
lines changed

chat-server/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mongodb-chatbot-server",
3-
"version": "0.2.0",
3+
"version": "0.2.1-0",
44
"license": "Apache-2.0",
55
"description": "A chatbot server for retrieval augmented generation (RAG).",
66
"author": "MongoDB, Inc.",
@@ -11,7 +11,8 @@
1111
},
1212
"files": [
1313
"dist",
14-
"README.md"
14+
"README.md",
15+
"static"
1516
],
1617
"main": "./dist/index.js",
1718
"scripts": {

chat-server/src/app.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ describe("App", () => {
1515
},
1616
});
1717
});
18+
test("should server static files", async () => {
19+
const { appConfig } = makeTestAppConfig();
20+
const app = await makeApp({ ...appConfig, serveStaticSite: true });
21+
const response = await request(app).get("/index.html");
22+
expect(response.status).toBe(200);
23+
expect(response.text).toContain("<!DOCTYPE html>");
24+
});
1825

1926
describe("Error handling", () => {
2027
test("Should return 404 if path is not found", async () => {

chat-server/src/app.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { getRequestId, logRequest, sendErrorResponse } from "./utils";
1717
import { CorsOptions } from "cors";
1818
import { logger } from "mongodb-rag-core";
1919
import cloneDeep from "lodash.clonedeep";
20+
import path from "path";
2021

2122
/**
2223
Configuration for the server Express.js app.
@@ -42,6 +43,17 @@ export interface AppConfig {
4243
Prefix for all API routes. Defaults to `/api/v1`.
4344
*/
4445
apiPrefix?: string;
46+
47+
/**
48+
Whether to serve a static site from the root path (`GET https://my-site.com/`).
49+
Defaults to false.
50+
This is useful for demo and testing purposes.
51+
52+
You should probably not include this in your production server.
53+
You can control including this in dev/test/staging but not production
54+
with an environment variable.
55+
*/
56+
serveStaticSite?: boolean;
4557
}
4658

4759
/**
@@ -106,6 +118,7 @@ export const makeApp = async (config: AppConfig): Promise<Express> => {
106118
conversationsRouterConfig,
107119
corsOptions,
108120
apiPrefix = DEFAULT_API_PREFIX,
121+
serveStaticSite,
109122
} = config;
110123
logger.info("Server has the following configuration:");
111124
logger.info(
@@ -117,13 +130,8 @@ export const makeApp = async (config: AppConfig): Promise<Express> => {
117130
app.use(cors(corsOptions));
118131
app.use(express.json());
119132
app.use(reqHandler);
120-
const { NODE_ENV } = process.env;
121-
if (
122-
NODE_ENV === "development" ||
123-
NODE_ENV === "staging" ||
124-
NODE_ENV === "qa"
125-
) {
126-
app.use(express.static("static"));
133+
if (serveStaticSite) {
134+
app.use("/", express.static(path.join(__dirname, "..", "static")));
127135
}
128136
app.use(
129137
`${apiPrefix}/conversations`,

chat-server/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
@fileoverview This file contains the exported modules and types
3-
for using the chat server as a library.
3+
for using the chat server library.
44
*/
55
export * from "./app";
6-
export * from "./routes/conversations";
6+
export * from "./routes";
77
export * from "./services";
88
export * from "./processors";
99
export * from "./middleware";

chat-server/src/routes/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./conversations";
2+
export * from "./staticSite";
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import request from "supertest";
2+
import express from "express";
3+
import { makeStaticSite } from "./staticSite";
4+
5+
test("should server static files", async () => {
6+
const app = express();
7+
makeStaticSite(app);
8+
const response = await request(app).get("/index.html");
9+
expect(response.status).toBe(200);
10+
expect(response.text).toContain("<!DOCTYPE html>");
11+
});

chat-server/src/routes/staticSite.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import express, { Express } from "express";
2+
import path from "path";
3+
4+
/**
5+
Middleware that serves the static site from the root path
6+
(`GET https://my-site.com/`).
7+
*/
8+
export function makeStaticSite(app: Express) {
9+
return app.use(
10+
"/",
11+
// servers from <package root>/static
12+
express.static(path.join(__dirname, "..", "..", "static"))
13+
);
14+
}

chat-ui/vite.config.static-site.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ export default defineConfig({
2020
],
2121
root: ".",
2222
build: {
23-
outDir: "../chatbot-server-mongodb-public/static",
23+
outDir: "../chat-server/static",
2424
},
2525
});

chatbot-server-mongodb-public/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"typechat": "^0.0.10",
4444
"winston": "^3.9.0",
4545
"zod-error": "^1.5.0",
46-
"mongodb-chatbot-server": "^0.2.0"
46+
"mongodb-chatbot-server": "*"
4747
},
4848
"devDependencies": {
4949
"@babel/core": "^7.22.5",

chatbot-server-mongodb-public/src/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,5 @@ export const config: AppConfig = {
203203
corsOptions: {
204204
origin: allowedOrigins,
205205
},
206+
serveStaticSite: process.env.NODE_ENV !== "production",
206207
};

chatbot-server-mongodb-public/static/.gitinclude

Whitespace-only changes.

0 commit comments

Comments
 (0)