Custom indexes are now pluggable without frontends, and are very easy :D

pull/52/head
tycrek 3 years ago
parent 95dc9cf838
commit b171c365ad
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -282,6 +282,32 @@ Webhooks will show the filename, mimetype, size, upload timestamp, thumbail, & a
[create a new Webhook]: https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks [create a new Webhook]: https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks
## Custom index
By default, ass directs the index route `/` to this README. Follow these steps to use a custom index:
1. Run `npm run setup` to re-run the setup script.
- The defaults are set by your existing config, so you can press `Enter` to accept the defaults on most prompts.
- The one setting you want to change is `Filename for your custom index`. Enter a name for your index, including `.js` (custom index's must be `.js` files).
2. Make a new file matching the name you entered.
3. Your index file needs to export a single function taking three arguments: `(req, res, next)`. Some code samples for common use cases are provided below.
4. Restart ass. The startup info logs should say **`Custom index:`**` enabled`.
### Custom index code samples
**Redirect to a custom frontend registration page**
```js
module.exports = (req, res, next) => res.redirect('/register');
```
**Send an HTML file**
```js
const path = require('path');
module.exports = (req, res, next) => res.sendFile(path.join(__dirname, 'index.html'));
```
## Custom frontends ## Custom frontends
ass is intended to provide a strong backend for developers to build their own frontends around. The easiest way to do this is with a [Git Submodule]. Your submodule should be a **separate** git repo. Make sure you [adjust the `FRONTEND_NAME`] to match your frontend. To make updates easier, it is recommended to make a new branch. Since submodules are their own dedicated projects, you are free to build the router however you wish, as long as it exports the required items detailed below. ass is intended to provide a strong backend for developers to build their own frontends around. The easiest way to do this is with a [Git Submodule]. Your submodule should be a **separate** git repo. Make sure you [adjust the `FRONTEND_NAME`] to match your frontend. To make updates easier, it is recommended to make a new branch. Since submodules are their own dedicated projects, you are free to build the router however you wish, as long as it exports the required items detailed below.
@ -317,29 +343,6 @@ const data = require('../data');
These values are recognized globally throughout ass, so they will stay up-to-date as users upload. 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][ctw1].** **For a detailed walkthrough on developing your first frontend, [consult the wiki][ctw1].**
[Git Submodule]: https://git-scm.com/book/en/v2/Git-Tools-Submodules [Git Submodule]: https://git-scm.com/book/en/v2/Git-Tools-Submodules

@ -13,7 +13,7 @@ if (doSetup) {
} }
// Load the config // Load the config
const { host, port, useSsl, isProxied, s3enabled, frontendName } = require('./config.json'); const { host, port, useSsl, isProxied, s3enabled, frontendName, indexFile } = require('./config.json');
//#region Imports //#region Imports
const fs = require('fs-extra'); const fs = require('fs-extra');
@ -69,15 +69,15 @@ useSsl && app.use(helmet.hsts({ preload: true })); // skipcq: JS-0093
// Don't process favicon requests // Don't process favicon requests
app.use(nofavicon); app.use(nofavicon);
// Index can be overridden by a frontend // Use custom index, otherwise render README.md
app.get('/', (req, res, next) => const ASS_INDEX = fs.existsSync(`./${indexFile}/`) ? require(`./${indexFile}`) : false;
(ASS_PREMIUM.enabled && ASS_PREMIUM.index) app.get('/', (req, res, next) => !!ASS_INDEX
? ASS_PREMIUM.index(req, res, next) ? ASS_INDEX(req, res, next)
: fs.readFile(path('README.md')) : fs.readFile(path('README.md'))
.then((bytes) => bytes.toString()) .then((bytes) => bytes.toString())
.then(marked) .then(marked)
.then((d) => res.render('index', { data: d })) .then((d) => res.render('index', { data: d }))
.catch(next)); .catch(next));
// Upload router // Upload router
app.use('/', ROUTERS.upload); app.use('/', ROUTERS.upload);
@ -97,6 +97,6 @@ log
.info('Files', `${data.size}`) .info('Files', `${data.size}`)
.info('StorageEngine', data.name, data.type) .info('StorageEngine', data.name, data.type)
.info('Frontend', ASS_PREMIUM.enabled ? ASS_PREMIUM.brand : 'disabled', `${ASS_PREMIUM.enabled ? `${getTrueHttp()}${getTrueDomain()}${ASS_PREMIUM.endpoint}` : ''}`) .info('Frontend', ASS_PREMIUM.enabled ? ASS_PREMIUM.brand : 'disabled', `${ASS_PREMIUM.enabled ? `${getTrueHttp()}${getTrueDomain()}${ASS_PREMIUM.endpoint}` : ''}`)
.info('Index redirect', ASS_PREMIUM.enabled && ASS_PREMIUM.index ? `enable` : 'disabled') .info('Custom index', !!ASS_INDEX ? `enabled` : 'disabled')
.blank() .blank()
.express().Host(app, port, host, () => log.success('Ready for uploads', `Storing resources ${s3enabled ? 'in S3' : 'on disk'}`)); .express().Host(app, port, host, () => log.success('Ready for uploads', `Storing resources ${s3enabled ? 'in S3' : 'on disk'}`));

@ -11,6 +11,7 @@ const config = {
resourceIdType: 'random', resourceIdType: 'random',
mediaStrict: false, mediaStrict: false,
frontendName: 'ass-x', frontendName: 'ass-x',
indexFile: '',
s3enabled: false, s3enabled: false,
}; };
@ -148,6 +149,12 @@ function doSetup() {
default: config.frontendName, default: config.frontendName,
required: false required: false
}, },
indexFile: {
description: 'Filename for your custom index, if using one (must be a JS file)',
type: 'string',
default: config.indexFile,
required: false
},
s3enabled: { s3enabled: {
description: 'Enable uploading to S3 storage endpoints', description: 'Enable uploading to S3 storage endpoints',
type: 'boolean', type: 'boolean',

Loading…
Cancel
Save