Merge pull request #52 from tycrek/better-custom-frontend

frontend name now set in config
pull/53/head
Josh Moore 4 years ago committed by GitHub
commit 029eb2e098
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 } = 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');
@ -32,7 +32,7 @@ const { name: ASS_NAME, version: ASS_VERSION } = require('./package.json');
log.blank().info(`* ${ASS_NAME} v${ASS_VERSION} *`).blank(); log.blank().info(`* ${ASS_NAME} v${ASS_VERSION} *`).blank();
// Set up premium frontend // Set up premium frontend
const FRONTEND_NAME = 'ass-x'; // <-- Change this to use a custom frontend const FRONTEND_NAME = frontendName;
const ASS_PREMIUM = fs.existsSync(`./${FRONTEND_NAME}/package.json`) ? (require('submodule'), require(`./${FRONTEND_NAME}`)) : { enabled: false }; const ASS_PREMIUM = fs.existsSync(`./${FRONTEND_NAME}/package.json`) ? (require('submodule'), require(`./${FRONTEND_NAME}`)) : { enabled: false };
//#region Variables, module setup //#region Variables, module setup
@ -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}`);
(ASS_PREMIUM.enabled && ASS_PREMIUM.index) app.get('/', (req, res, next) => ASS_INDEX // skipcq: JS-0229
? 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);
@ -86,12 +86,10 @@ app.use('/', ROUTERS.upload);
ASS_PREMIUM.enabled && app.use(ASS_PREMIUM.endpoint, ASS_PREMIUM.router); // skipcq: JS-0093 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 // '/: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 app.use('/:resourceId', (req, _res, next) => (req.resourceId = req.params.resourceId, next()), ROUTERS.resource); // skipcq: JS-0086, JS-0090
app.use((err, req, res, next) => { // Error handler
console.error(err); app.use((err, _req, res, _next) => log.error(err).err(err).callback(() => res.sendStatus(CODE_INTERNAL_SERVER_ERROR))); // skipcq: JS-0128
res.sendStatus(CODE_INTERNAL_SERVER_ERROR);
});
// Host the server // Host the server
log log
@ -99,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'}`));

@ -10,6 +10,8 @@ const config = {
gfyIdSize: 2, gfyIdSize: 2,
resourceIdType: 'random', resourceIdType: 'random',
mediaStrict: false, mediaStrict: false,
frontendName: 'ass-x',
indexFile: '',
s3enabled: false, s3enabled: false,
}; };
@ -141,6 +143,18 @@ function doSetup() {
default: config.mediaStrict, default: config.mediaStrict,
required: false required: false
}, },
frontendName: {
description: 'Name of your frontend (leave blank if not using frontends)',
type: 'string',
default: config.frontendName,
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',

@ -88,7 +88,7 @@ const logger = new TLog({
logger logger
.env('ASS_ENV') .env('ASS_ENV')
//.enable.process({ uncaughtException: false }).debug('Plugin enabled', 'Process') //.enable.process({ uncaughtException: false }).debug('Plugin enabled', 'Process')
.enable.express().debug('Plugin enabled', 'Express') .enable.express({ handle500: false }).debug('Plugin enabled', 'Express')
.enable.socket().debug('Plugin enabled', 'Socket'); .enable.socket().debug('Plugin enabled', 'Socket');
const idModes = { const idModes = {

Loading…
Cancel
Save