2019-05-06 00:01:57 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
const config = require('config');
|
|
|
|
const express = require('express');
|
|
|
|
const Router = require('express-promise-router');
|
2019-06-03 03:31:38 +00:00
|
|
|
const bodyParser = require('body-parser');
|
2020-12-19 22:01:17 +00:00
|
|
|
const session = require('express-session');
|
|
|
|
const KnexSessionStore = require('connect-session-knex')(session);
|
2021-11-20 22:59:15 +00:00
|
|
|
const { nanoid } = require('nanoid');
|
2019-05-06 00:01:57 +00:00
|
|
|
|
2020-03-11 02:03:19 +00:00
|
|
|
const logger = require('../logger')(__filename);
|
2020-12-19 22:01:17 +00:00
|
|
|
const knex = require('../knex');
|
2021-02-26 18:39:48 +00:00
|
|
|
const errorHandler = require('./error');
|
2019-12-16 01:39:13 +00:00
|
|
|
|
2021-02-27 02:52:27 +00:00
|
|
|
const pg = require('./postgraphile');
|
|
|
|
|
2021-03-13 03:26:24 +00:00
|
|
|
const {
|
|
|
|
login,
|
|
|
|
logout,
|
|
|
|
signup,
|
|
|
|
fetchMe,
|
|
|
|
} = require('./auth');
|
|
|
|
|
2019-11-10 03:20:22 +00:00
|
|
|
const {
|
2020-05-19 23:38:58 +00:00
|
|
|
fetchScene,
|
|
|
|
fetchScenes,
|
2020-12-18 23:40:36 +00:00
|
|
|
fetchScenePoster,
|
2019-11-10 03:20:22 +00:00
|
|
|
} = require('./releases');
|
|
|
|
|
2020-05-19 23:38:58 +00:00
|
|
|
const {
|
|
|
|
fetchActor,
|
|
|
|
fetchActors,
|
|
|
|
} = require('./actors');
|
2020-05-19 23:11:32 +00:00
|
|
|
|
2020-10-16 21:00:03 +00:00
|
|
|
const {
|
|
|
|
fetchEntity,
|
|
|
|
fetchEntities,
|
|
|
|
} = require('./entities');
|
|
|
|
|
|
|
|
const {
|
|
|
|
fetchTag,
|
|
|
|
fetchTags,
|
|
|
|
} = require('./tags');
|
|
|
|
|
2021-03-15 02:30:47 +00:00
|
|
|
const {
|
2021-03-20 01:49:17 +00:00
|
|
|
createStash,
|
2021-03-20 22:03:13 +00:00
|
|
|
removeStash,
|
2021-03-15 02:30:47 +00:00
|
|
|
stashActor,
|
|
|
|
stashScene,
|
2021-03-17 04:11:17 +00:00
|
|
|
stashMovie,
|
2021-03-15 02:30:47 +00:00
|
|
|
unstashActor,
|
|
|
|
unstashScene,
|
2021-03-17 04:11:17 +00:00
|
|
|
unstashMovie,
|
2021-03-20 01:03:30 +00:00
|
|
|
updateStash,
|
2021-03-15 02:30:47 +00:00
|
|
|
} = require('./stashes');
|
|
|
|
|
2021-04-04 19:52:19 +00:00
|
|
|
const {
|
|
|
|
addAlert,
|
|
|
|
removeAlert,
|
2021-04-25 01:08:50 +00:00
|
|
|
updateNotifications,
|
|
|
|
updateNotification,
|
2021-04-04 19:52:19 +00:00
|
|
|
} = require('./alerts');
|
|
|
|
|
2021-05-15 00:51:52 +00:00
|
|
|
function getIp(req) {
|
|
|
|
return req.headers['x-forwarded-for'] ? req.headers['x-forwarded-for'].split(',')[0] : req.connection.remoteAddress; // See src/ws
|
|
|
|
}
|
|
|
|
|
2020-05-29 20:43:03 +00:00
|
|
|
async function initServer() {
|
2020-05-14 02:26:05 +00:00
|
|
|
const app = express();
|
|
|
|
const router = Router();
|
2020-12-19 22:01:17 +00:00
|
|
|
const store = new KnexSessionStore({ knex });
|
2020-05-14 02:26:05 +00:00
|
|
|
|
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
|
|
|
|
router.use('/media', express.static(config.media.path));
|
|
|
|
router.use(express.static('public'));
|
|
|
|
|
|
|
|
router.use('/img', (req, res) => {
|
|
|
|
res.status(404).send();
|
|
|
|
});
|
|
|
|
|
|
|
|
router.use(bodyParser.json({ strict: false }));
|
2020-12-19 22:01:17 +00:00
|
|
|
router.use(session({ ...config.web.session, store }));
|
2020-05-14 02:26:05 +00:00
|
|
|
|
2021-03-14 03:54:43 +00:00
|
|
|
router.use(pg);
|
|
|
|
|
2020-12-20 18:49:57 +00:00
|
|
|
router.use((req, res, next) => {
|
|
|
|
req.session.safeId = req.session.safeId || nanoid();
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2021-05-15 00:51:52 +00:00
|
|
|
router.use((req, res, next) => {
|
|
|
|
const ip = getIp(req);
|
|
|
|
|
|
|
|
logger.silly(`${ip} (${req.headers['CF-IPCountry'] || 'country N/A'}) requested ${req.originalUrl} as ${req.session.user ? `${req.session.user.username} (${req.session.user.id})` : 'guest'}`);
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2021-03-13 03:26:24 +00:00
|
|
|
router.get('/api/session', fetchMe);
|
|
|
|
router.post('/api/session', login);
|
|
|
|
router.delete('/api/session', logout);
|
|
|
|
|
|
|
|
router.post('/api/users', signup);
|
|
|
|
|
2021-04-25 01:08:50 +00:00
|
|
|
router.patch('/api/users/:userId/notifications', updateNotifications);
|
|
|
|
router.patch('/api/users/:userId/notifications/:notificationId', updateNotification);
|
|
|
|
|
2021-03-20 01:49:17 +00:00
|
|
|
router.post('/api/stashes', createStash);
|
2021-03-20 01:03:30 +00:00
|
|
|
router.patch('/api/stashes/:stashId', updateStash);
|
2021-03-20 22:03:13 +00:00
|
|
|
router.delete('/api/stashes/:stashId', removeStash);
|
2021-03-20 01:03:30 +00:00
|
|
|
|
2021-03-15 02:30:47 +00:00
|
|
|
router.post('/api/stashes/:stashId/actors', stashActor);
|
|
|
|
router.post('/api/stashes/:stashId/scenes', stashScene);
|
2021-03-17 04:11:17 +00:00
|
|
|
router.post('/api/stashes/:stashId/movies', stashMovie);
|
2021-03-15 02:30:47 +00:00
|
|
|
|
|
|
|
router.delete('/api/stashes/:stashId/actors/:actorId', unstashActor);
|
|
|
|
router.delete('/api/stashes/:stashId/scenes/:sceneId', unstashScene);
|
2021-03-17 04:11:17 +00:00
|
|
|
router.delete('/api/stashes/:stashId/movies/:movieId', unstashMovie);
|
2021-03-15 02:30:47 +00:00
|
|
|
|
2021-04-04 19:52:19 +00:00
|
|
|
router.post('/api/alerts', addAlert);
|
2021-04-04 22:48:03 +00:00
|
|
|
router.delete('/api/alerts/:alertId', removeAlert);
|
2021-04-04 19:52:19 +00:00
|
|
|
|
2020-05-19 23:38:58 +00:00
|
|
|
router.get('/api/scenes', fetchScenes);
|
|
|
|
router.get('/api/scenes/:releaseId', fetchScene);
|
2020-12-18 23:40:36 +00:00
|
|
|
router.get('/api/scenes/:releaseId/poster', fetchScenePoster);
|
2020-05-19 23:38:58 +00:00
|
|
|
|
2020-12-18 23:40:36 +00:00
|
|
|
// router.get('/api/movies', fetchMovies);
|
|
|
|
// router.get('/api/movies/:releaseId', fetchMovie);
|
2020-05-19 23:11:32 +00:00
|
|
|
|
2020-05-19 23:38:58 +00:00
|
|
|
router.get('/api/actors', fetchActors);
|
2020-05-19 23:11:32 +00:00
|
|
|
router.get('/api/actors/:actorId', fetchActor);
|
2020-05-14 02:26:05 +00:00
|
|
|
|
2020-10-16 21:00:03 +00:00
|
|
|
router.get('/api/entities', async (req, res) => fetchEntities(req, res, null));
|
|
|
|
router.get('/api/entities/:entityId', async (req, res) => fetchEntity(req, res, null));
|
|
|
|
|
|
|
|
router.get('/api/channels', async (req, res) => fetchEntities(req, res, 'channel'));
|
|
|
|
router.get('/api/channels/:entityId', async (req, res) => fetchEntity(req, res, 'channel'));
|
|
|
|
|
|
|
|
router.get('/api/networks', async (req, res) => fetchEntities(req, res, 'network'));
|
|
|
|
router.get('/api/networks/:entityId', async (req, res) => fetchEntity(req, res, 'network'));
|
|
|
|
|
|
|
|
router.get('/api/studios', async (req, res) => fetchEntities(req, res, 'studio'));
|
|
|
|
router.get('/api/studios/:entityId', async (req, res) => fetchEntity(req, res, 'studio'));
|
|
|
|
|
|
|
|
router.get('/api/tags', fetchTags);
|
|
|
|
router.get('/api/tags/:tagId', fetchTag);
|
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
router.get('*', (req, res) => {
|
|
|
|
res.render(path.join(__dirname, '../../assets/index.ejs'), {
|
|
|
|
env: JSON.stringify({
|
|
|
|
sfw: !!req.headers.sfw || Object.prototype.hasOwnProperty.call(req.query, 'sfw'),
|
2021-06-27 22:05:24 +00:00
|
|
|
login: config.auth.login,
|
|
|
|
signup: config.auth.signup,
|
2020-12-20 18:49:57 +00:00
|
|
|
sessionId: req.session.safeId,
|
2020-05-14 02:26:05 +00:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-26 18:39:48 +00:00
|
|
|
router.use(errorHandler);
|
2020-05-14 02:26:05 +00:00
|
|
|
app.use(router);
|
|
|
|
|
|
|
|
const server = app.listen(config.web.port, config.web.host, () => {
|
|
|
|
const { address, port } = server.address();
|
|
|
|
|
|
|
|
logger.info(`Web server listening on ${address}:${port}`);
|
|
|
|
});
|
2019-05-06 00:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = initServer;
|