2020-11-05 02:47:58 +00:00
|
|
|
const Unit = require('libflitter/Unit')
|
|
|
|
const Express = require('express')
|
|
|
|
const path = require('path')
|
|
|
|
|
2020-11-08 01:49:32 +00:00
|
|
|
/**
|
|
|
|
* FrontendUnit
|
|
|
|
* @extends Unit
|
|
|
|
* ----------------------------------------------------------------------------------------
|
|
|
|
*/
|
2020-11-05 02:47:58 +00:00
|
|
|
class FrontendUnit extends Unit {
|
|
|
|
static get services() {
|
2020-11-07 18:10:19 +00:00
|
|
|
return [...super.services, 'configs', 'express', 'canon', 'utility', 'models']
|
2020-11-05 02:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(...args) {
|
|
|
|
super(...args)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fully qualified path to the root of the ionic app.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this.directory = this.utility.path(this.configs.get('server.frontend_path'))
|
|
|
|
}
|
|
|
|
|
2020-11-08 01:49:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param app
|
|
|
|
*
|
|
|
|
*/
|
2020-11-05 02:47:58 +00:00
|
|
|
async go(app) {
|
|
|
|
app.express.use('/app', [
|
|
|
|
this.canon.get('middleware::auth:UserOnly'),
|
|
|
|
(req, res, next) => {
|
2020-11-05 02:58:09 +00:00
|
|
|
if ( !req.user ) {
|
|
|
|
return res.redirect('/auth/login')
|
|
|
|
}
|
|
|
|
|
2020-11-05 02:47:58 +00:00
|
|
|
const allowed_extensions = [
|
|
|
|
'.html', '.js', '.css', '.svg', '.ttf', '.jpg', '.png',
|
|
|
|
'.jpeg', '.webmanifest', '.json', '.eot', '.svg', '.cur',
|
|
|
|
'.webp', '.gif', '.otf', '.woff', '.woff2', '.ani', '.map',
|
|
|
|
'.ico',
|
|
|
|
]
|
|
|
|
|
|
|
|
for ( const k1 in allowed_extensions ) {
|
|
|
|
if ( req.path.endsWith(allowed_extensions[k1]) ) return next()
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.sendFile(path.resolve(this.directory, 'index.html'))
|
|
|
|
},
|
|
|
|
Express.static(this.directory),
|
|
|
|
])
|
2020-11-07 18:10:19 +00:00
|
|
|
|
|
|
|
// Set the default setting values
|
|
|
|
const Setting = this.models.get('models::setting') // a built-in helper
|
|
|
|
const default_settings = this.configs.get('settings.default_settings') || []
|
|
|
|
|
|
|
|
for ( const default_setting of default_settings ) {
|
|
|
|
await Setting.guarantee(default_setting.key, default_setting.value)
|
|
|
|
}
|
2020-11-05 02:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = FrontendUnit
|