2023-03-11 14:12:51 +08:00
|
|
|
const express = require('express');
|
2023-03-22 01:33:49 -04:00
|
|
|
const session = require('express-session');
|
2023-03-17 19:58:13 -04:00
|
|
|
const connectDb = require('../lib/db/connectDb');
|
|
|
|
const migrateDb = require('../lib/db/migrateDb');
|
2023-03-22 01:33:49 -04:00
|
|
|
const indexSync = require('../lib/db/indexSync');
|
2023-03-11 14:12:51 +08:00
|
|
|
const path = require('path');
|
|
|
|
const cors = require('cors');
|
|
|
|
const routes = require('./routes');
|
2023-03-22 01:33:49 -04:00
|
|
|
const errorController = require('./controllers/errorController');
|
|
|
|
|
2023-03-11 14:12:51 +08:00
|
|
|
const port = process.env.PORT || 3080;
|
2023-03-22 01:33:49 -04:00
|
|
|
const host = process.env.HOST || 'localhost';
|
|
|
|
const userSystemEnabled = process.env.ENABLE_USER_SYSTEM || false;
|
2023-03-11 14:12:51 +08:00
|
|
|
const projectPath = path.join(__dirname, '..', '..', 'client');
|
2023-03-22 01:33:49 -04:00
|
|
|
|
|
|
|
(async () => {
|
|
|
|
await connectDb();
|
2023-03-13 21:59:25 -04:00
|
|
|
console.log('Connected to MongoDB');
|
2023-03-22 01:33:49 -04:00
|
|
|
await migrateDb();
|
|
|
|
await indexSync();
|
2023-03-11 14:12:51 +08:00
|
|
|
|
2023-03-22 01:33:49 -04:00
|
|
|
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
|
|
|
|
})
|
|
|
|
);
|
2023-03-11 14:12:51 +08:00
|
|
|
|
2023-03-22 01:33:49 -04:00
|
|
|
/* chore: potential redirect error here, can only comment out this block;
|
2023-03-20 02:58:41 -04:00
|
|
|
comment back in if using auth routes i guess */
|
2023-03-22 01:33:49 -04:00
|
|
|
// app.get('/', routes.authenticatedOrRedirect, function (req, res) {
|
|
|
|
// console.log(path.join(projectPath, 'public', 'index.html'));
|
|
|
|
// res.sendFile(path.join(projectPath, 'public', 'index.html'));
|
|
|
|
// });
|
2023-03-11 14:12:51 +08:00
|
|
|
|
2023-03-22 01:33:49 -04:00
|
|
|
app.get('/api/me', function (req, res) {
|
|
|
|
if (userSystemEnabled) {
|
|
|
|
const user = req?.session?.user;
|
2023-03-14 01:24:43 +08:00
|
|
|
|
2023-03-22 01:33:49 -04:00
|
|
|
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' }));
|
|
|
|
}
|
|
|
|
});
|
2023-03-14 01:24:43 +08:00
|
|
|
|
2023-03-22 01:33:49 -04:00
|
|
|
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);
|
2023-03-11 14:12:51 +08:00
|
|
|
|
2023-03-22 01:33:49 -04:00
|
|
|
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;
|
2023-03-16 14:44:14 +08:00
|
|
|
|
2023-03-22 01:33:49 -04:00
|
|
|
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}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
})();
|
2023-03-16 14:44:14 +08:00
|
|
|
|
2023-03-22 01:33:49 -04:00
|
|
|
let messageCount = 0;
|
|
|
|
process.on('uncaughtException', (err) => {
|
2023-03-22 21:23:01 -04:00
|
|
|
if (!err.message.includes('fetch failed')) {
|
|
|
|
console.error('There was an uncaught error:', err.message);
|
|
|
|
}
|
|
|
|
|
2023-03-22 01:33:49 -04:00
|
|
|
if (err.message.includes('fetch failed')) {
|
2023-03-22 21:23:01 -04:00
|
|
|
if (messageCount === 0) {
|
|
|
|
console.error('Meilisearch error, search will be disabled');
|
|
|
|
messageCount++;
|
|
|
|
}
|
2023-03-22 01:33:49 -04:00
|
|
|
} else {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
});
|