added support for custom index's attached through frontends

pull/29/head
tycrek 3 years ago
parent f903d2a4fb
commit 11c181adf9
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -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).**

@ -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

@ -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

Loading…
Cancel
Save