Skip to content

Commit

Permalink
Fix Meilisearch error and refactor of the server index.js (danny-avil…
Browse files Browse the repository at this point in the history
…a#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
  • Loading branch information
berry-13 authored Aug 24, 2023
1 parent 37347d4 commit a569020
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 50 deletions.
6 changes: 5 additions & 1 deletion api/lib/db/indexSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ const Conversation = require('../../models/schema/convoSchema');
const Message = require('../../models/schema/messageSchema');
const { MeiliSearch } = require('meilisearch');
let currentTimeout = null;
const searchEnabled = process.env?.SEARCH?.toLowerCase() === 'true';

// eslint-disable-next-line no-unused-vars
async function indexSync(req, res, next) {
const searchEnabled = process.env.SEARCH && process.env.SEARCH.toLowerCase() === 'true';
if (!searchEnabled) {
return;
}

try {
if (!process.env.MEILI_HOST || !process.env.MEILI_MASTER_KEY || !searchEnabled) {
throw new Error('Meilisearch not configured, search will be disabled.');
Expand Down
63 changes: 14 additions & 49 deletions api/server/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
const express = require('express');
const session = require('express-session');
const connectDb = require('../lib/db/connectDb');
const indexSync = require('../lib/db/indexSync');
const path = require('path');
const cors = require('cors');
const routes = require('./routes');
const errorController = require('./controllers/ErrorController');
const passport = require('passport');
const configureSocialLogins = require('./socialLogins');

const port = process.env.PORT || 3080;
const host = process.env.HOST || 'localhost';
const projectPath = path.join(__dirname, '..', '..', 'client');
const {
jwtLogin,
passportLogin,
googleLogin,
githubLogin,
discordLogin,
facebookLogin,
setupOpenId,
} = require('../strategies');

// Init the config and validate it
const config = require('../../config/loader');
config.validate(); // Validate the config
const { jwtLogin, passportLogin } = require('../strategies');

(async () => {
const startServer = async () => {
await connectDb();
console.log('Connected to MongoDB');
await indexSync();

const app = express();

// Middleware
app.use(errorController);
app.use(express.json({ limit: '3mb' }));
app.use(express.urlencoded({ extended: true, limit: '3mb' }));
app.use(express.static(path.join(projectPath, 'dist')));
app.use(express.static(path.join(projectPath, 'public')));

app.set('trust proxy', 1); // trust first proxy
app.use(cors());

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

if (process.env.ALLOW_SOCIAL_LOGIN === 'true') {
if (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {
passport.use(await googleLogin());
}
if (process.env.FACEBOOK_CLIENT_ID && process.env.FACEBOOK_CLIENT_SECRET) {
passport.use(await facebookLogin());
}
if (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
passport.use(await githubLogin());
}
if (process.env.DISCORD_CLIENT_ID && process.env.DISCORD_CLIENT_SECRET) {
passport.use(await discordLogin());
}
if (
process.env.OPENID_CLIENT_ID &&
process.env.OPENID_CLIENT_SECRET &&
process.env.OPENID_ISSUER &&
process.env.OPENID_SCOPE &&
process.env.OPENID_SESSION_SECRET
) {
app.use(
session({
secret: process.env.OPENID_SESSION_SECRET,
resave: false,
saveUninitialized: false,
}),
);
app.use(passport.session());
await setupOpenId();
}
configureSocialLogins(app);
}

app.use('/oauth', routes.oauth);
// api endpoint
// API Endpoints
app.use('/api/auth', routes.auth);
app.use('/api/user', routes.user);
app.use('/api/search', routes.search);
Expand All @@ -97,21 +60,23 @@ config.validate(); // Validate the config
app.use('/api/plugins', routes.plugins);
app.use('/api/config', routes.config);

// static files
// Static files
app.get('/*', function (req, res) {
res.sendFile(path.join(projectPath, 'dist', 'index.html'));
});

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`,
`Server listening on all interfaces 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}`);
}
});
})();
};

startServer();

let messageCount = 0;
process.on('uncaughtException', (err) => {
Expand Down
43 changes: 43 additions & 0 deletions api/server/socialLogins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const session = require('express-session');
const passport = require('passport');
const {
googleLogin,
githubLogin,
discordLogin,
facebookLogin,
setupOpenId,
} = require('../strategies');

const configureSocialLogins = (app) => {
if (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {
passport.use(googleLogin());
}
if (process.env.FACEBOOK_CLIENT_ID && process.env.FACEBOOK_CLIENT_SECRET) {
passport.use(facebookLogin());
}
if (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
passport.use(githubLogin());
}
if (process.env.DISCORD_CLIENT_ID && process.env.DISCORD_CLIENT_SECRET) {
passport.use(discordLogin());
}
if (
process.env.OPENID_CLIENT_ID &&
process.env.OPENID_CLIENT_SECRET &&
process.env.OPENID_ISSUER &&
process.env.OPENID_SCOPE &&
process.env.OPENID_SESSION_SECRET
) {
app.use(
session({
secret: process.env.OPENID_SESSION_SECRET,
resave: false,
saveUninitialized: false,
}),
);
app.use(passport.session());
setupOpenId();
}
};

module.exports = configureSocialLogins;

0 comments on commit a569020

Please sign in to comment.