diff --git a/README.md b/README.md index 1983c40..3a60752 100755 --- a/README.md +++ b/README.md @@ -178,7 +178,39 @@ module.exports = { Now you should see `My awesome dashboard!` when you navigate to `http://your-ass-url/dashboard`. -**Disclaimer:** custom frontends are still experimental. Currently ass has no API, but I already plan on writing that very soon. For now, you can make your dashboard use `const users = require('../auth')` & `const data = require('../data')` (these values are recognized globally throughout ass, so they will stay up-to-date as users upload). +#### Accessing data + +If you want to access resource & user data within your frontend router, just add these two lines near the top of your router: + +```js +const users = require('../auth'); +const data = require('../data'); +``` + +These values are recognized globally throughout ass, so they will stay up-to-date as users upload. + +#### Custom index + +By default, ass directs the app index to this README. To change it, just add an `index` function to your router exports: + +```js + +function index(req, res, next) { + // redirect user to dashboard + res.redirect('/dashboard/user'); + + // you can also use req & next as you normally + // would in an Express route handler +} + +module.exports = { + router, + index, + enabled: true, + brand: `${name} v${version}`, + endpoint: '/dashboard', +}; +``` **For a detailed walkthrough on developing your first frontend, [consult the wiki](https://github.com/tycrek/ass/wiki/Writing-a-custom-frontend).** diff --git a/ass.js b/ass.js index 9771073..f649aba 100755 --- a/ass.js +++ b/ass.js @@ -13,6 +13,7 @@ const { host, port, useSsl, diskFilePath, isProxied } = require('./config.json') const fs = require('fs-extra'); const express = require('express'); const helmet = require('helmet'); +const marked = require('marked'); const uploadRouter = require('./routers/upload'); const resourceRouter = require('./routers/resource'); const { path, log } = require('./utils'); @@ -59,9 +60,23 @@ useSsl && app.use(helmet.hsts({ preload: true })); // skipcq: JS-0093 // Don't process favicon requests (custom middleware) app.use((req, res, next) => (req.url.includes('favicon.ico') ? res.sendStatus(CODE_NO_CONTENT) : next())); -// Assign routers ('/:resouceId' always needs to be LAST since it's a catch-all route) +// Index can be overridden by a frontend +app.get('/', (req, res, next) => + (ASS_PREMIUM.enabled && ASS_PREMIUM.index) + ? ASS_PREMIUM.index(req, res, next) + : fs.readFile(path('README.md')) + .then((bytes) => bytes.toString()) + .then(marked) + .then((d) => res.render('index', { data: d })) + .catch(next)); + +// Upload router app.use('/', ROUTERS.upload); + +// Attach frontend, if enabled ASS_PREMIUM.enabled && app.use(ASS_PREMIUM.endpoint, ASS_PREMIUM.router); // skipcq: JS-0093 + +// '/:resouceId' always needs to be LAST since it's a catch-all route app.use('/:resourceId', (req, _, next) => (req.resourceId = req.params.resourceId, next()), ROUTERS.resource); // skipcq: JS-0086, JS-0090 // Error handler diff --git a/routers/upload.js b/routers/upload.js index 1e84fc9..9b230c4 100644 --- a/routers/upload.js +++ b/routers/upload.js @@ -1,5 +1,4 @@ const fs = require('fs-extra'); -const marked = require('marked'); //const rateLimit = require('express-rate-limit'); const { DateTime } = require('luxon'); const { WebhookClient, MessageEmbed } = require('discord.js'); @@ -14,14 +13,6 @@ const ASS_LOGO = 'https://cdn.discordapp.com/icons/848274994375294986/8d339d4a2f const express = require('express'); const router = express.Router(); -// Index -router.get('/', (_req, res, next) => - fs.readFile(path('README.md')) - .then((bytes) => bytes.toString()) - .then(marked) - .then((d) => res.render('index', { data: d })) - .catch(next)); - // Rate limit middleware /* router.use('/', rateLimit({ windowMs: 1000 * 60, // 60 seconds // skipcq: JS-0074