2023-06-08 22:13:48 +02:00
|
|
|
process.env.NODE_ENV === "development"
|
|
|
|
? require("dotenv").config({ path: `.env.${process.env.NODE_ENV}` })
|
|
|
|
: require("dotenv").config();
|
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
const express = require("express");
|
|
|
|
const bodyParser = require("body-parser");
|
|
|
|
const cors = require("cors");
|
2023-06-13 20:26:11 +02:00
|
|
|
const path = require("path");
|
2023-06-08 06:31:35 +02:00
|
|
|
const { validatedRequest } = require("./utils/middleware/validatedRequest");
|
|
|
|
const { reqBody } = require("./utils/http");
|
|
|
|
const { systemEndpoints } = require("./endpoints/system");
|
|
|
|
const { workspaceEndpoints } = require("./endpoints/workspaces");
|
|
|
|
const { chatEndpoints } = require("./endpoints/chat");
|
|
|
|
const { getVectorDbClass } = require("./utils/helpers");
|
2023-06-15 08:12:59 +02:00
|
|
|
const { validateTablePragmas } = require("./utils/database");
|
2023-06-04 04:28:07 +02:00
|
|
|
const app = express();
|
2023-06-13 20:26:11 +02:00
|
|
|
const apiRouter = express.Router();
|
2023-06-04 04:28:07 +02:00
|
|
|
|
|
|
|
app.use(cors({ origin: true }));
|
|
|
|
app.use(bodyParser.text());
|
|
|
|
app.use(bodyParser.json());
|
2023-06-08 06:31:35 +02:00
|
|
|
app.use(
|
|
|
|
bodyParser.urlencoded({
|
|
|
|
extended: true,
|
|
|
|
})
|
|
|
|
);
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-06-13 20:26:11 +02:00
|
|
|
apiRouter.use("/system/*", validatedRequest);
|
|
|
|
systemEndpoints(apiRouter);
|
2023-06-15 08:12:59 +02:00
|
|
|
|
|
|
|
apiRouter.use("/workspace/*", validatedRequest);
|
2023-06-13 20:26:11 +02:00
|
|
|
workspaceEndpoints(apiRouter);
|
|
|
|
chatEndpoints(apiRouter);
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-06-13 20:26:11 +02:00
|
|
|
apiRouter.post("/v/:command", async (request, response) => {
|
2023-06-04 04:28:07 +02:00
|
|
|
try {
|
2023-06-08 22:13:48 +02:00
|
|
|
const VectorDb = getVectorDbClass();
|
|
|
|
const { command } = request.params;
|
|
|
|
if (!Object.getOwnPropertyNames(VectorDb).includes(command)) {
|
|
|
|
response.status(500).json({
|
|
|
|
message: "invalid interface command",
|
|
|
|
commands: Object.getOwnPropertyNames(VectorDb),
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const body = reqBody(request);
|
|
|
|
const resBody = await VectorDb[command](body);
|
|
|
|
response.status(200).json({ ...resBody });
|
|
|
|
} catch (e) {
|
|
|
|
// console.error(e)
|
|
|
|
console.error(JSON.stringify(e));
|
|
|
|
response.status(500).json({ error: e.message });
|
|
|
|
}
|
|
|
|
return;
|
2023-06-04 04:28:07 +02:00
|
|
|
} catch (e) {
|
2023-06-08 22:13:48 +02:00
|
|
|
console.log(e.message, e);
|
|
|
|
response.sendStatus(500).end();
|
2023-06-04 04:28:07 +02:00
|
|
|
}
|
2023-06-08 06:31:35 +02:00
|
|
|
});
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-06-13 20:26:11 +02:00
|
|
|
app.use("/api", apiRouter);
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== "development") {
|
2023-06-14 22:35:55 +02:00
|
|
|
app.use(
|
|
|
|
express.static(path.resolve(__dirname, "public"), { extensions: ["js"] })
|
|
|
|
);
|
2023-06-13 20:26:11 +02:00
|
|
|
|
|
|
|
app.use("/", function (_, response) {
|
|
|
|
response.sendFile(path.join(__dirname, "public", "index.html"));
|
2023-06-14 22:35:55 +02:00
|
|
|
});
|
2023-06-13 20:26:11 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
app.all("*", function (_, response) {
|
2023-06-04 04:28:07 +02:00
|
|
|
response.sendStatus(404);
|
|
|
|
});
|
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
app
|
2023-06-15 08:12:59 +02:00
|
|
|
.listen(process.env.SERVER_PORT || 3001, async () => {
|
|
|
|
await validateTablePragmas();
|
2023-06-08 06:31:35 +02:00
|
|
|
console.log(
|
2023-06-13 20:26:11 +02:00
|
|
|
`Example app listening on port ${process.env.SERVER_PORT || 3001}`
|
2023-06-08 06:31:35 +02:00
|
|
|
);
|
|
|
|
})
|
2023-06-04 04:28:07 +02:00
|
|
|
.on("error", function (err) {
|
|
|
|
process.once("SIGUSR2", function () {
|
|
|
|
process.kill(process.pid, "SIGUSR2");
|
|
|
|
});
|
|
|
|
process.on("SIGINT", function () {
|
|
|
|
process.kill(process.pid, "SIGINT");
|
|
|
|
});
|
2023-06-08 06:31:35 +02:00
|
|
|
});
|