106 lines
3.1 KiB
JavaScript
106 lines
3.1 KiB
JavaScript
const express = require("express");
|
|
const cors = require('cors');
|
|
const cookieParser = require('cookie-parser');
|
|
const passport = require('passport');
|
|
const path = require('path');
|
|
|
|
const dotenv = require('dotenv');
|
|
|
|
if(process.env.NODE_ENV) {
|
|
console.log(`.env.${process.env.NODE_ENV}`);
|
|
dotenv.config({
|
|
path: `.env.${process.env.NODE_ENV}`
|
|
});
|
|
} else {
|
|
dotenv.config();
|
|
}
|
|
|
|
const app = express();
|
|
const connectDB = require("./db");
|
|
|
|
|
|
// PUBLIC
|
|
const uploadDir = path.join(__dirname, 'uploads');
|
|
app.use('/api/public', express.static(uploadDir));
|
|
|
|
// JSON LIMIT EXPRESS
|
|
app.use(express.json({ limit: '50mb' }));
|
|
app.use(express.urlencoded({
|
|
extended: true,
|
|
limit: '50mb'
|
|
}));
|
|
|
|
// connect database
|
|
connectDB();
|
|
|
|
// MIDDLEWARE
|
|
app.use(passport.initialize());
|
|
require('./services/passport')(passport);
|
|
|
|
// CORS
|
|
app.use(cookieParser());
|
|
|
|
// Services talk to Dragonroll from their own application (a Foundry world, an
|
|
// Obsidian vault, the Discord bot), so their link routes cannot be limited to
|
|
// one origin. What they may do is decided by the link code each of them holds.
|
|
const openCorsPaths = [
|
|
'/api/foundry/sync', '/foundry/sync',
|
|
'/api/link/start', '/link/start',
|
|
'/api/link/pickup', '/link/pickup',
|
|
];
|
|
const openCors = cors({
|
|
origin: true,
|
|
methods: ['GET', 'POST', 'OPTIONS'],
|
|
allowedHeaders: ['Authorization', 'Content-Type', 'X-Dragonroll-Discord-Bot-Secret'],
|
|
});
|
|
for (const path of openCorsPaths) {
|
|
app.use(path, openCors);
|
|
}
|
|
|
|
const appCors = cors({
|
|
origin: 'http://localhost:3000',
|
|
credentials: true, // if using cookies/auth
|
|
});
|
|
app.use((req, res, next) => {
|
|
if (openCorsPaths.some((path) => req.path === path || req.path.startsWith(`${path}/`))) return next();
|
|
appCors(req, res, next);
|
|
});
|
|
|
|
// ROUTES (NO AUTH)
|
|
app.use('/api/user', require('./routes/user'));
|
|
app.use('/api/discord', require('./routes/discord'));
|
|
// The one way every service (Foundry, Discord, Obsidian) links itself to a
|
|
// campaign: it asks for a sign-up link, a user opens it and picks the campaign.
|
|
// Each service authenticates with its own proof: the Discord bot with its
|
|
// secret, Foundry and Obsidian with the link code they were handed.
|
|
app.use('/api/link', require('./routes/service-link'));
|
|
app.use('/api/foundry', require('./routes/foundry-link'));
|
|
app.use('/foundry', require('./routes/foundry-link'));
|
|
|
|
// AUTH
|
|
checkAuth = passport.authenticate('jwt', { session: false });
|
|
app.use(checkAuth);
|
|
|
|
// ROUTES WITH AUTH
|
|
app.use('/api/campaign', require('./routes/campaign'));
|
|
app.use('/api/note', require('./routes/note'));
|
|
app.use('/api/folder', require('./routes/folder'));
|
|
app.use('/api/actor', require('./routes/actor'));
|
|
app.use('/api/item', require('./routes/item'));
|
|
app.use('/api/map', require('./routes/map'));
|
|
app.use('/api/foundry', require('./routes/foundry'));
|
|
/*
|
|
app.use('/campaign', require('./routes/campaign'));
|
|
app.use('/datagen', require('./routes/datagen'));
|
|
app.use('/admin', require('./routes/admin'));
|
|
*/
|
|
|
|
app.get("/api/test", (req, res) => {
|
|
console.log("Hey");
|
|
res.json({"message": "Hello from backend!"});
|
|
});
|
|
|
|
app.listen(5000, () => {
|
|
console.log("Server running on port 5000");
|
|
});
|