Skip to content

Fix Meilisearch error and refactor of the server index.js #832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/lib/db/indexSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ let currentTimeout = null;

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

const searchEnabled = process.env.SEARCH && process.env.SEARCH.toLowerCase() === 'true';
try {
if (!process.env.MEILI_HOST || !process.env.MEILI_MASTER_KEY || !searchEnabled) {
Expand Down
83 changes: 44 additions & 39 deletions api/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const cors = require('cors');
const routes = require('./routes');
const errorController = require('./controllers/ErrorController');
const passport = require('passport');

const port = process.env.PORT || 3080;
const host = process.env.HOST || 'localhost';
const projectPath = path.join(__dirname, '..', '..', 'client');
Expand All @@ -20,22 +21,19 @@ const {
setupOpenId,
} = require('../strategies');

// Init the config and validate it
const config = require('../../config/loader');
config.validate(); // Validate the config

(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 +49,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 +68,55 @@ 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}`);
}
});
})();
};

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();
}
};

startServer();

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