Stackedit/server/index.js

92 lines
3.3 KiB
JavaScript
Raw Normal View History

const compression = require('compression');
const serveStatic = require('serve-static');
const bodyParser = require('body-parser');
const path = require('path');
const user = require('./user');
const github = require('./github');
const gitee = require('./gitee');
const gitea = require('./gitea');
const pdf = require('./pdf');
const pandoc = require('./pandoc');
const conf = require('./conf');
const resolvePath = pathToResolve => path.join(__dirname, '..', pathToResolve);
2017-09-26 22:54:26 +00:00
module.exports = (app) => {
2017-09-27 20:27:12 +00:00
if (process.env.NODE_ENV === 'production') {
2018-01-20 00:58:21 +00:00
// Enable CORS for fonts
app.all('*', (req, res, next) => {
2018-04-05 22:55:51 +00:00
if (/\.(eot|ttf|woff2?|svg)$/.test(req.url)) {
2017-09-29 18:43:26 +00:00
res.header('Access-Control-Allow-Origin', '*');
2017-10-14 11:45:35 +00:00
}
2017-09-27 20:27:12 +00:00
next();
});
2018-01-20 00:58:21 +00:00
// Use gzip compression
2017-09-27 20:27:12 +00:00
app.use(compression());
}
2017-09-26 22:54:26 +00:00
app.get('/oauth2/githubToken', github.githubToken);
app.get('/oauth2/giteeToken', gitee.giteeToken);
app.get('/oauth2/giteaToken', gitea.giteaToken);
app.get('/conf', (req, res) => res.send(conf.publicValues));
app.get('/userInfo', user.userInfo);
app.post('/pdfExport', pdf.generate);
app.post('/pandocExport', pandoc.generate);
2017-11-04 19:14:52 +00:00
app.post('/paypalIpn', bodyParser.urlencoded({
extended: false,
}), user.paypalIpn);
2022-07-14 14:48:46 +00:00
app.get('/giteeClientId', (req, res) => {
const giteeClientIds = conf.values.giteeClientId.split(',');
// 仅一个 则直接返回
if (giteeClientIds.length === 1) {
res.send(giteeClientIds[0]);
return;
}
// 是否随机一个clientId 默认第一个 如果random 为1 则使用随机 避免单个应用接口次数用满无法自动切换其他应用
const random = req.query.random;
if (!random) {
res.send(giteeClientIds[0]);
return;
}
// 随机一个 排除第一个 因为第一个应用接口次数用完了
const clientId = giteeClientIds[Math.floor(((giteeClientIds.length - 1) * Math.random())) + 1];
2022-07-14 14:48:46 +00:00
res.send(clientId);
});
2018-01-07 16:22:04 +00:00
// Serve landing.html
app.get('/', (req, res) => res.sendFile(resolvePath('static/landing/index.html')));
2022-06-06 23:49:58 +00:00
// Serve privacy_policy.html
app.get('/privacy_policy.html', (req, res) => res.sendFile(resolvePath('static/landing/privacy_policy.html')));
2018-02-01 23:53:51 +00:00
// Serve sitemap.xml
app.get('/sitemap.xml', (req, res) => res.sendFile(resolvePath('static/sitemap.xml')));
2018-01-04 20:19:10 +00:00
// Serve callback.html
app.get('/oauth2/callback', (req, res) => res.sendFile(resolvePath('static/oauth2/callback.html')));
2018-01-04 20:19:10 +00:00
// Google Drive action receiver
app.get('/googleDriveAction', (req, res) =>
res.redirect(`./app#providerId=googleDrive&state=${encodeURIComponent(req.query.state)}`));
2022-11-13 07:16:46 +00:00
// Serve the static folder with 30 day max-age
2022-10-05 19:45:51 +00:00
app.use('/themes', serveStatic(resolvePath('static/themes'), {
2022-11-13 07:16:46 +00:00
maxAge: '30d',
2022-10-05 19:45:51 +00:00
}));
2022-11-13 07:16:46 +00:00
// Serve empty.js
app.get('/empty.js', (req, res) => res.send(''));
2017-09-26 22:54:26 +00:00
// Serve static resources
2017-09-27 20:27:12 +00:00
if (process.env.NODE_ENV === 'production') {
// Serve index.html in /app
app.get('/app', (req, res) => res.sendFile(resolvePath('dist/index.html')));
// Serve style.css with 1 day max-age
app.get('/style.css', (req, res) => res.sendFile(resolvePath('dist/style.css'), {
maxAge: '1d',
}));
// Serve the static folder with 1 year max-age
app.use('/static', serveStatic(resolvePath('dist/static'), {
maxAge: '1y',
}));
2017-09-26 22:54:26 +00:00
app.use(serveStatic(resolvePath('dist')));
2017-09-27 20:27:12 +00:00
}
2017-09-26 22:54:26 +00:00
};