anything-llm/server/index.js

113 lines
3.2 KiB
JavaScript
Raw Normal View History

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 serveIndex = require("serve-index");
2023-06-08 06:31:35 +02:00
const cors = require("cors");
const path = require("path");
2023-06-08 06:31:35 +02:00
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");
const { adminEndpoints } = require("./endpoints/admin");
const { inviteEndpoints } = require("./endpoints/invite");
2023-08-10 22:23:23 +02:00
const { utilEndpoints } = require("./endpoints/utils");
const { Telemetry } = require("./models/telemetry");
const { developerEndpoints } = require("./endpoints/api");
Replace custom sqlite dbms with prisma (#239) * WIP converted all sqlite models into prisma calls * modify db setup and fix ApiKey model calls in admin.js * renaming function params to be consistent * converted adminEndpoints to utilize prisma orm * converted chatEndpoints to utilize prisma orm * converted inviteEndpoints to utilize prisma orm * converted systemEndpoints to utilize prisma orm * converted workspaceEndpoints to utilize prisma orm * converting sql queries to prisma calls * fixed default param bug for orderBy and limit * fixed typo for workspace chats * fixed order of deletion to account for sql relations * fix invite CRUD and workspace management CRUD * fixed CRUD for api keys * created prisma setup scripts/docs for understanding how to use prisma * prisma dependency change * removing unneeded console.logs * removing unneeded sql escape function * linting and creating migration script * migration from depreciated sqlite script update * removing unneeded migrations in prisma folder * create backup of old sqlite db and use transactions to ensure all operations complete successfully * adding migrations to gitignore * updated PRISMA.md docs for info on how to use sqlite migration script * comment changes * adding back migrations folder to repo * Reviewing SQL and prisma integraiton on fresh repo * update inline key replacement * ensure migration script executes and maps foreign_keys regardless of db ordering * run migration endpoint * support new prisma backend * bump version * change migration call --------- Co-authored-by: timothycarambat <rambat1010@gmail.com>
2023-09-28 23:00:03 +02:00
const setupTelemetry = require("./utils/telemetry");
2023-06-04 04:28:07 +02:00
const app = express();
const apiRouter = express.Router();
const FILE_LIMIT = "3GB";
2023-06-04 04:28:07 +02:00
app.use(cors({ origin: true }));
app.use(bodyParser.text({ limit: FILE_LIMIT }));
app.use(bodyParser.json({ limit: FILE_LIMIT }));
2023-06-08 06:31:35 +02:00
app.use(
bodyParser.urlencoded({
limit: FILE_LIMIT,
2023-06-08 06:31:35 +02:00
extended: true,
})
);
2023-06-04 04:28:07 +02:00
app.use("/api", apiRouter);
systemEndpoints(apiRouter);
workspaceEndpoints(apiRouter);
chatEndpoints(apiRouter);
adminEndpoints(apiRouter);
inviteEndpoints(apiRouter);
2023-08-10 22:23:23 +02:00
utilEndpoints(apiRouter);
developerEndpoints(app, apiRouter);
2023-06-04 04:28:07 +02:00
apiRouter.post("/v/:command", async (request, response) => {
2023-06-04 04:28:07 +02:00
try {
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) {
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
if (process.env.NODE_ENV !== "development") {
app.use(
express.static(path.resolve(__dirname, "public"), { extensions: ["js"] })
);
app.use("/", function (_, response) {
response.sendFile(path.join(__dirname, "public", "index.html"));
});
app.get("/robots.txt", function (_, response) {
response.type("text/plain");
response.send("User-agent: *\nDisallow: /").end();
});
}
app.use(
"/system/data-exports",
serveIndex(__dirname + "/storage/exports", { icons: true })
);
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
.listen(process.env.SERVER_PORT || 3001, async () => {
await setupTelemetry();
2023-06-08 06:31:35 +02:00
console.log(
2023-12-14 22:52:11 +01:00
`Primary server 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 () {
Telemetry.flush();
2023-06-04 04:28:07 +02:00
process.kill(process.pid, "SIGUSR2");
});
process.on("SIGINT", function () {
Telemetry.flush();
2023-06-04 04:28:07 +02:00
process.kill(process.pid, "SIGINT");
});
2023-06-08 06:31:35 +02:00
});