Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

search: correctly register/export schema/models, also made IIFE

Daniel Avilae25aa74d7b9b
diff --git a/api/server/index.js b/api/server/index.js
index 93804a4390..362dce89ed 100644
--- a/api/server/index.js
+++ b/api/server/index.js
@@ -1,70 +1,91 @@
const express = require('express');
-const session = require('express-session')
+const session = require('express-session');
const connectDb = require('../lib/db/connectDb');
const migrateDb = require('../lib/db/migrateDb');
+const indexSync = require('../lib/db/indexSync');
const path = require('path');
const cors = require('cors');
const routes = require('./routes');
-const app = express();
+const errorController = require('./controllers/errorController');
+
const port = process.env.PORT || 3080;
-const host = process.env.HOST || 'localhost'
-const userSystemEnabled = process.env.ENABLE_USER_SYSTEM || false
+const host = process.env.HOST || 'localhost';
+const userSystemEnabled = process.env.ENABLE_USER_SYSTEM || false;
const projectPath = path.join(__dirname, '..', '..', 'client');
-connectDb().then(() => {
+
+(async () => {
+ await connectDb();
console.log('Connected to MongoDB');
- migrateDb();
-});
+ await migrateDb();
+ await indexSync();
-app.use(cors());
-app.use(express.json());
-app.use(express.static(path.join(projectPath, 'public')));
-app.set('trust proxy', 1) // trust first proxy
-app.use(session({
- secret: 'chatgpt-clone-random-secrect',
- resave: false,
- saveUninitialized: true,
-}))
+ const app = express();
+ app.use(errorController);
+ app.use(cors());
+ app.use(express.json());
+ app.use(express.static(path.join(projectPath, 'public')));
+ app.set('trust proxy', 1); // trust first proxy
+ app.use(
+ session({
+ secret: 'chatgpt-clone-random-secrect',
+ resave: false,
+ saveUninitialized: true
+ })
+ );
-/* chore: potential redirect error here, can only comment out this block;
+ /* chore: potential redirect error here, can only comment out this block;
comment back in if using auth routes i guess */
-// app.get('/', routes.authenticatedOrRedirect, function (req, res) {
-// console.log(path.join(projectPath, 'public', 'index.html'));
-// res.sendFile(path.join(projectPath, 'public', 'index.html'));
-// });
+ // app.get('/', routes.authenticatedOrRedirect, function (req, res) {
+ // console.log(path.join(projectPath, 'public', 'index.html'));
+ // res.sendFile(path.join(projectPath, 'public', 'index.html'));
+ // });
-app.get('/api/me', function (req, res) {
- if (userSystemEnabled) {
- const user = req?.session?.user
-
- if (user)
- res.send(JSON.stringify({username: user?.username, display: user?.display}));
- else
- res.send(JSON.stringify(null));
- } else {
- res.send(JSON.stringify({username: 'anonymous_user', display: 'Anonymous User'}));
- }
-});
+ app.get('/api/me', function (req, res) {
+ if (userSystemEnabled) {
+ const user = req?.session?.user;
-app.use('/api/search', routes.authenticatedOr401, routes.search);
-app.use('/api/ask', routes.authenticatedOr401, routes.ask);
-app.use('/api/messages', routes.authenticatedOr401, routes.messages);
-app.use('/api/convos', routes.authenticatedOr401, routes.convos);
-app.use('/api/customGpts', routes.authenticatedOr401, routes.customGpts);
-app.use('/api/prompts', routes.authenticatedOr401, routes.prompts);
-app.use('/auth', routes.auth);
+ if (user) res.send(JSON.stringify({ username: user?.username, display: user?.display }));
+ else res.send(JSON.stringify(null));
+ } else {
+ res.send(JSON.stringify({ username: 'anonymous_user', display: 'Anonymous User' }));
+ }
+ });
+ app.use('/api/search', routes.authenticatedOr401, routes.search);
+ app.use('/api/ask', routes.authenticatedOr401, routes.ask);
+ app.use('/api/messages', routes.authenticatedOr401, routes.messages);
+ app.use('/api/convos', routes.authenticatedOr401, routes.convos);
+ app.use('/api/customGpts', routes.authenticatedOr401, routes.customGpts);
+ app.use('/api/prompts', routes.authenticatedOr401, routes.prompts);
+ app.use('/auth', routes.auth);
-app.get('/api/models', function (req, res) {
- const hasOpenAI = !!process.env.OPENAI_KEY;
- const hasChatGpt = !!process.env.CHATGPT_TOKEN;
- const hasBing = !!process.env.BING_TOKEN;
+ app.get('/api/models', function (req, res) {
+ const hasOpenAI = !!process.env.OPENAI_KEY;
+ const hasChatGpt = !!process.env.CHATGPT_TOKEN;
+ const hasBing = !!process.env.BING_TOKEN;
- res.send(JSON.stringify({ hasOpenAI, hasChatGpt, hasBing }));
-});
+ res.send(JSON.stringify({ hasOpenAI, hasChatGpt, hasBing }));
+ });
+
+ app.listen(port, host, () => {
+ if (host == '0.0.0.0')
+ console.log(
+ `Server listening on all interface at port ${port}. Use http://localhost:${port} to access it`
+ );
+ else
+ console.log(
+ `Server listening at http://${host == '0.0.0.0' ? 'localhost' : host}:${port}`
+ );
+ });
+})();
-app.listen(port, host, () => {
- if (host=='0.0.0.0')
- console.log(`Server listening on all interface at port ${port}. Use http://localhost:${port} to access it`);
- else
- console.log(`Server listening at http://${host=='0.0.0.0'?'localhost':host}:${port}`);
-});
\ No newline at end of file
+let messageCount = 0;
+process.on('uncaughtException', (err) => {
+ // console.error('There was an uncaught error:', err.message);
+ if (err.message.includes('fetch failed')) {
+ if (messageCount === 0) console.error('Meilisearch error, search will be disabled');
+ messageCount++;
+ } else {
+ process.exit(1);
+ }
+});