Improved frontend plugging (merged #56)

pull/57/head
Josh Moore 3 years ago committed by GitHub
commit 80ff74c8b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -310,43 +310,12 @@ module.exports = (req, res, next) => res.sendFile(path.join(__dirname, 'index.ht
## 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.
Sample submodule entry file:
```js
const { name, version } = require('./package.json');
const express = require('express');
const router = express.Router();
router.all('/', (_req, res) => res.send('My awesome dashboard!'));
// These exports are REQUIRED by ass, so don't forget to set them!
module.exports = {
router, // The dashboard router itself
enabled: true, // Required to activate frontend in ass; DO NOT change unless you want to disable your frontend
brand: `${name} v${version}`, // Printed in ass logs & reported to client. Can be changed to your liking
endpoint: '/dashboard' // URL to use for your dashboard router. ass will automatically set up Express to use this value. Can be changed to your liking
};
```
Now you should see `My awesome dashboard!` when you navigate to `http://your-ass-url/dashboard`.
#### 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.
ass is intended to provide a strong backend for developers to build their own frontends around. [Git Submodules] make it easy to create custom frontends. Submodules are their own projects, which means you are free to build the router however you wish, as long as it exports the required items. A custom frontend is really just an [Express.js router].
**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
[adjust the `FRONTEND_NAME`]: https://github.com/tycrek/ass/blob/d766bd15cf8ac851058c8abf37238f1608d8c305/ass.js#L24
[Git Submodules]: https://git-scm.com/book/en/v2/Git-Tools-Submodules
[Express.js router]: https://expressjs.com/en/guide/routing.html#express-router
[ctw1]: https://github.com/tycrek/ass/wiki/Writing-a-custom-frontend
## StorageEngines

@ -31,10 +31,6 @@ const { name: ASS_NAME, version: ASS_VERSION } = require('./package.json');
// Welcome :D
log.blank().info(`* ${ASS_NAME} v${ASS_VERSION} *`).blank();
// Set up premium frontend
const FRONTEND_NAME = frontendName;
const ASS_PREMIUM = fs.existsSync(`./${FRONTEND_NAME}/package.json`) ? (require('submodule'), require(`./${FRONTEND_NAME}`)) : { enabled: false };
//#region Variables, module setup
const app = express();
const ROUTERS = {
@ -82,8 +78,9 @@ app.get('/', (req, res, next) => ASS_INDEX // skipcq: JS-0229
// Upload router
app.use('/', ROUTERS.upload);
// Attach frontend, if enabled
ASS_PREMIUM.enabled && app.use(ASS_PREMIUM.endpoint, ASS_PREMIUM.router); // skipcq: JS-0093
// Set up custom frontend
const ASS_FRONTEND = fs.existsSync(`./${frontendName}/package.json`) ? (require('submodule'), require(`./${frontendName}`)) : { enabled: false };
ASS_FRONTEND.enabled && app.use(ASS_FRONTEND.endpoint, ASS_FRONTEND.router); // skipcq: JS-0093
// '/:resouceId' always needs to be LAST since it's a catch-all route
app.use('/:resourceId', (req, _res, next) => (req.resourceId = req.params.resourceId, next()), ROUTERS.resource); // skipcq: JS-0086, JS-0090
@ -96,7 +93,7 @@ log
.info('Users', `${Object.keys(users).length}`)
.info('Files', `${data.size}`)
.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_FRONTEND.enabled ? ASS_FRONTEND.brand : 'disabled', `${ASS_FRONTEND.enabled ? `${getTrueHttp()}${getTrueDomain()}${ASS_FRONTEND.endpoint}` : ''}`)
.info('Custom index', ASS_INDEX ? `enabled` : 'disabled')
.blank()
.express().Host(app, port, host, () => log.success('Ready for uploads', `Storing resources ${s3enabled ? 'in S3' : 'on disk'}`));

Loading…
Cancel
Save