Skip to content

Commit c74aa89

Browse files
authored
Fix Meilisearch error and refactor of the server index.js (danny-avila#832)
* fix meilisearch error at startup * limit the nesting * disable useless console log * fix(indexSync.js): removed redundant searchEnabled * refactor(index.js): moved configureSocialLogins to a new file * refactor(socialLogins.js): removed unnecessary conditional
1 parent a5e78df commit c74aa89

File tree

3 files changed

+62
-50
lines changed

3 files changed

+62
-50
lines changed

api/lib/db/indexSync.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ const Conversation = require('../../models/schema/convoSchema');
22
const Message = require('../../models/schema/messageSchema');
33
const { MeiliSearch } = require('meilisearch');
44
let currentTimeout = null;
5+
const searchEnabled = process.env?.SEARCH?.toLowerCase() === 'true';
56

67
// eslint-disable-next-line no-unused-vars
78
async function indexSync(req, res, next) {
8-
const searchEnabled = process.env.SEARCH && process.env.SEARCH.toLowerCase() === 'true';
9+
if (!searchEnabled) {
10+
return;
11+
}
12+
913
try {
1014
if (!process.env.MEILI_HOST || !process.env.MEILI_MASTER_KEY || !searchEnabled) {
1115
throw new Error('Meilisearch not configured, search will be disabled.');

api/server/index.js

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
11
const express = require('express');
2-
const session = require('express-session');
32
const connectDb = require('../lib/db/connectDb');
43
const indexSync = require('../lib/db/indexSync');
54
const path = require('path');
65
const cors = require('cors');
76
const routes = require('./routes');
87
const errorController = require('./controllers/ErrorController');
98
const passport = require('passport');
9+
const configureSocialLogins = require('./socialLogins');
10+
1011
const port = process.env.PORT || 3080;
1112
const host = process.env.HOST || 'localhost';
1213
const projectPath = path.join(__dirname, '..', '..', 'client');
13-
const {
14-
jwtLogin,
15-
passportLogin,
16-
googleLogin,
17-
githubLogin,
18-
discordLogin,
19-
facebookLogin,
20-
setupOpenId,
21-
} = require('../strategies');
22-
23-
// Init the config and validate it
24-
const config = require('../../config/loader');
25-
config.validate(); // Validate the config
14+
const { jwtLogin, passportLogin } = require('../strategies');
2615

27-
(async () => {
16+
const startServer = async () => {
2817
await connectDb();
2918
console.log('Connected to MongoDB');
3019
await indexSync();
3120

3221
const app = express();
22+
23+
// Middleware
3324
app.use(errorController);
3425
app.use(express.json({ limit: '3mb' }));
3526
app.use(express.urlencoded({ extended: true, limit: '3mb' }));
3627
app.use(express.static(path.join(projectPath, 'dist')));
3728
app.use(express.static(path.join(projectPath, 'public')));
38-
3929
app.set('trust proxy', 1); // trust first proxy
4030
app.use(cors());
4131

@@ -51,38 +41,11 @@ config.validate(); // Validate the config
5141
passport.use(await passportLogin());
5242

5343
if (process.env.ALLOW_SOCIAL_LOGIN === 'true') {
54-
if (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {
55-
passport.use(await googleLogin());
56-
}
57-
if (process.env.FACEBOOK_CLIENT_ID && process.env.FACEBOOK_CLIENT_SECRET) {
58-
passport.use(await facebookLogin());
59-
}
60-
if (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
61-
passport.use(await githubLogin());
62-
}
63-
if (process.env.DISCORD_CLIENT_ID && process.env.DISCORD_CLIENT_SECRET) {
64-
passport.use(await discordLogin());
65-
}
66-
if (
67-
process.env.OPENID_CLIENT_ID &&
68-
process.env.OPENID_CLIENT_SECRET &&
69-
process.env.OPENID_ISSUER &&
70-
process.env.OPENID_SCOPE &&
71-
process.env.OPENID_SESSION_SECRET
72-
) {
73-
app.use(
74-
session({
75-
secret: process.env.OPENID_SESSION_SECRET,
76-
resave: false,
77-
saveUninitialized: false,
78-
}),
79-
);
80-
app.use(passport.session());
81-
await setupOpenId();
82-
}
44+
configureSocialLogins(app);
8345
}
46+
8447
app.use('/oauth', routes.oauth);
85-
// api endpoint
48+
// API Endpoints
8649
app.use('/api/auth', routes.auth);
8750
app.use('/api/user', routes.user);
8851
app.use('/api/search', routes.search);
@@ -97,21 +60,23 @@ config.validate(); // Validate the config
9760
app.use('/api/plugins', routes.plugins);
9861
app.use('/api/config', routes.config);
9962

100-
// static files
63+
// Static files
10164
app.get('/*', function (req, res) {
10265
res.sendFile(path.join(projectPath, 'dist', 'index.html'));
10366
});
10467

10568
app.listen(port, host, () => {
10669
if (host == '0.0.0.0') {
10770
console.log(
108-
`Server listening on all interface at port ${port}. Use http://localhost:${port} to access it`,
71+
`Server listening on all interfaces at port ${port}. Use http://localhost:${port} to access it`,
10972
);
11073
} else {
11174
console.log(`Server listening at http://${host == '0.0.0.0' ? 'localhost' : host}:${port}`);
11275
}
11376
});
114-
})();
77+
};
78+
79+
startServer();
11580

11681
let messageCount = 0;
11782
process.on('uncaughtException', (err) => {

api/server/socialLogins.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const session = require('express-session');
2+
const passport = require('passport');
3+
const {
4+
googleLogin,
5+
githubLogin,
6+
discordLogin,
7+
facebookLogin,
8+
setupOpenId,
9+
} = require('../strategies');
10+
11+
const configureSocialLogins = (app) => {
12+
if (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {
13+
passport.use(googleLogin());
14+
}
15+
if (process.env.FACEBOOK_CLIENT_ID && process.env.FACEBOOK_CLIENT_SECRET) {
16+
passport.use(facebookLogin());
17+
}
18+
if (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
19+
passport.use(githubLogin());
20+
}
21+
if (process.env.DISCORD_CLIENT_ID && process.env.DISCORD_CLIENT_SECRET) {
22+
passport.use(discordLogin());
23+
}
24+
if (
25+
process.env.OPENID_CLIENT_ID &&
26+
process.env.OPENID_CLIENT_SECRET &&
27+
process.env.OPENID_ISSUER &&
28+
process.env.OPENID_SCOPE &&
29+
process.env.OPENID_SESSION_SECRET
30+
) {
31+
app.use(
32+
session({
33+
secret: process.env.OPENID_SESSION_SECRET,
34+
resave: false,
35+
saveUninitialized: false,
36+
}),
37+
);
38+
app.use(passport.session());
39+
setupOpenId();
40+
}
41+
};
42+
43+
module.exports = configureSocialLogins;

0 commit comments

Comments
 (0)