Skip to content
Snippets Groups Projects
index.js 2.82 KiB
Newer Older
  • Learn to ignore specific revisions
  • process.env.NODE_ENV === "development"
      ? require("dotenv").config({ path: `.env.${process.env.NODE_ENV}` })
      : require("dotenv").config();
    
    
    const express = require("express");
    const bodyParser = require("body-parser");
    const cors = require("cors");
    
    frasergr's avatar
    frasergr committed
    const path = require("path");
    
    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");
    
    timothycarambat's avatar
    timothycarambat committed
    const { utilEndpoints } = require("./endpoints/utils");
    
    const { developerEndpoints } = require("./endpoints/api");
    
    const { extensionEndpoints } = require("./endpoints/extensions");
    
    const { bootHTTP, bootSSL } = require("./utils/boot");
    
    timothycarambat's avatar
    timothycarambat committed
    const app = express();
    
    frasergr's avatar
    frasergr committed
    const apiRouter = express.Router();
    
    const FILE_LIMIT = "3GB";
    
    timothycarambat's avatar
    timothycarambat committed
    
    app.use(cors({ origin: true }));
    
    app.use(bodyParser.text({ limit: FILE_LIMIT }));
    app.use(bodyParser.json({ limit: FILE_LIMIT }));
    
    app.use(
      bodyParser.urlencoded({
    
        limit: FILE_LIMIT,
    
        extended: true,
      })
    );
    
    frasergr's avatar
    frasergr committed
    systemEndpoints(apiRouter);
    
    extensionEndpoints(apiRouter);
    
    frasergr's avatar
    frasergr committed
    workspaceEndpoints(apiRouter);
    chatEndpoints(apiRouter);
    
    adminEndpoints(apiRouter);
    inviteEndpoints(apiRouter);
    
    timothycarambat's avatar
    timothycarambat committed
    utilEndpoints(apiRouter);
    
    developerEndpoints(app, apiRouter);
    
    frasergr's avatar
    frasergr committed
    apiRouter.post("/v/:command", async (request, response) => {
    
    timothycarambat's avatar
    timothycarambat committed
      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;
    
    timothycarambat's avatar
    timothycarambat committed
      } catch (e) {
    
        console.log(e.message, e);
        response.sendStatus(500).end();
    
    timothycarambat's avatar
    timothycarambat committed
      }
    
    frasergr's avatar
    frasergr committed
    if (process.env.NODE_ENV !== "development") {
    
      app.use(
        express.static(path.resolve(__dirname, "public"), { extensions: ["js"] })
      );
    
    frasergr's avatar
    frasergr committed
    
      app.use("/", function (_, response) {
        response.sendFile(path.join(__dirname, "public", "index.html"));
    
    Timothy Carambat's avatar
    Timothy Carambat committed
    
      app.get("/robots.txt", function (_, response) {
        response.type("text/plain");
        response.send("User-agent: *\nDisallow: /").end();
      });
    
    app.all("*", function (_, response) {
    
    timothycarambat's avatar
    timothycarambat committed
      response.sendStatus(404);
    });
    
    
    if (!!process.env.ENABLE_HTTPS) {
      bootSSL(app, process.env.SERVER_PORT || 3001);
    } else {
      bootHTTP(app, process.env.SERVER_PORT || 3001);
    }