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

This commit is contained in:
Daniel Avila 2023-03-22 01:33:49 -04:00
parent 47a6cfcafd
commit e25aa74d7b

View file

@ -1,29 +1,37 @@
const express = require('express'); const express = require('express');
const session = require('express-session') const session = require('express-session');
const connectDb = require('../lib/db/connectDb'); const connectDb = require('../lib/db/connectDb');
const migrateDb = require('../lib/db/migrateDb'); const migrateDb = require('../lib/db/migrateDb');
const indexSync = require('../lib/db/indexSync');
const path = require('path'); const path = require('path');
const cors = require('cors'); const cors = require('cors');
const routes = require('./routes'); 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 projectPath = path.join(__dirname, '..', '..', 'client');
connectDb().then(() => {
console.log('Connected to MongoDB');
migrateDb();
});
const port = process.env.PORT || 3080;
const host = process.env.HOST || 'localhost';
const userSystemEnabled = process.env.ENABLE_USER_SYSTEM || false;
const projectPath = path.join(__dirname, '..', '..', 'client');
(async () => {
await connectDb();
console.log('Connected to MongoDB');
await migrateDb();
await indexSync();
const app = express();
app.use(errorController);
app.use(cors()); app.use(cors());
app.use(express.json()); app.use(express.json());
app.use(express.static(path.join(projectPath, 'public'))); app.use(express.static(path.join(projectPath, 'public')));
app.set('trust proxy', 1) // trust first proxy app.set('trust proxy', 1); // trust first proxy
app.use(session({ app.use(
session({
secret: 'chatgpt-clone-random-secrect', secret: 'chatgpt-clone-random-secrect',
resave: false, resave: false,
saveUninitialized: true, 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 */ comment back in if using auth routes i guess */
@ -34,12 +42,10 @@ app.use(session({
app.get('/api/me', function (req, res) { app.get('/api/me', function (req, res) {
if (userSystemEnabled) { if (userSystemEnabled) {
const user = req?.session?.user const user = req?.session?.user;
if (user) if (user) res.send(JSON.stringify({ username: user?.username, display: user?.display }));
res.send(JSON.stringify({username: user?.username, display: user?.display})); else res.send(JSON.stringify(null));
else
res.send(JSON.stringify(null));
} else { } else {
res.send(JSON.stringify({ username: 'anonymous_user', display: 'Anonymous User' })); res.send(JSON.stringify({ username: 'anonymous_user', display: 'Anonymous User' }));
} }
@ -53,7 +59,6 @@ app.use('/api/customGpts', routes.authenticatedOr401, routes.customGpts);
app.use('/api/prompts', routes.authenticatedOr401, routes.prompts); app.use('/api/prompts', routes.authenticatedOr401, routes.prompts);
app.use('/auth', routes.auth); app.use('/auth', routes.auth);
app.get('/api/models', function (req, res) { app.get('/api/models', function (req, res) {
const hasOpenAI = !!process.env.OPENAI_KEY; const hasOpenAI = !!process.env.OPENAI_KEY;
const hasChatGpt = !!process.env.CHATGPT_TOKEN; const hasChatGpt = !!process.env.CHATGPT_TOKEN;
@ -64,7 +69,23 @@ app.get('/api/models', function (req, res) {
app.listen(port, host, () => { app.listen(port, host, () => {
if (host == '0.0.0.0') if (host == '0.0.0.0')
console.log(`Server listening on all interface at port ${port}. Use http://localhost:${port} to access it`); console.log(
`Server listening on all interface at port ${port}. Use http://localhost:${port} to access it`
);
else else
console.log(`Server listening at http://${host=='0.0.0.0'?'localhost':host}:${port}`); console.log(
`Server listening at http://${host == '0.0.0.0' ? 'localhost' : host}:${port}`
);
});
})();
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);
}
}); });