mirror of
https://github.com/Qortal/qortal-ui.git
synced 2025-11-02 05:37:58 +00:00
Redsign qortal-ui repo
This commit is contained in:
34
core/server/ServerFactory.js
Normal file
34
core/server/ServerFactory.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const Path = require('path')
|
||||
const Hapi = require('@hapi/hapi')
|
||||
const Inert = require('@hapi/inert')
|
||||
|
||||
function serverFactory(routes, address, port, tls) {
|
||||
this.server = new Hapi.Server({
|
||||
routes: {
|
||||
files: {
|
||||
relativeTo: Path.join(__dirname, '../')
|
||||
}
|
||||
},
|
||||
address: address,
|
||||
port: port,
|
||||
tls: tls
|
||||
})
|
||||
|
||||
this.startServer = async () => {
|
||||
try {
|
||||
await this.server.register([Inert])
|
||||
|
||||
this.server.route(routes)
|
||||
|
||||
await this.server.start()
|
||||
|
||||
delete this.startServer
|
||||
return this.server
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = serverFactory
|
||||
106
core/server/routes/createCommonRoutes.js
Normal file
106
core/server/routes/createCommonRoutes.js
Normal file
@@ -0,0 +1,106 @@
|
||||
const path = require('path')
|
||||
|
||||
const routesOptions = {
|
||||
security: {
|
||||
hsts: {
|
||||
maxAge: 15768000,
|
||||
includeSubDomains: true,
|
||||
preload: true
|
||||
},
|
||||
xframe: 'sameorigin'
|
||||
}
|
||||
}
|
||||
|
||||
const createRoutes = config => [
|
||||
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/img/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: config.build.options.imgDir,
|
||||
redirectToSlash: true,
|
||||
index: true
|
||||
}
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/language/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: path.join(__dirname, '../../language'),
|
||||
redirectToSlash: true,
|
||||
index: true
|
||||
}
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/font/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: path.join(__dirname, '../../font'),
|
||||
redirectToSlash: true,
|
||||
index: true
|
||||
}
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/sound/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: path.join(__dirname, '../../sound/'),
|
||||
redirectToSlash: true,
|
||||
index: true
|
||||
}
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/emoji/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: path.join(__dirname, '../../emoji/'),
|
||||
redirectToSlash: true,
|
||||
index: true
|
||||
}
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/memory-pow/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: path.join(__dirname, '../../memory-pow/'),
|
||||
redirectToSlash: true,
|
||||
index: true
|
||||
}
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/getConfig',
|
||||
handler: (request, h) => {
|
||||
const response = {
|
||||
config: {
|
||||
...config
|
||||
}
|
||||
}
|
||||
|
||||
delete response.config.user.tls
|
||||
delete response.config.build
|
||||
return JSON.stringify(response)
|
||||
},
|
||||
options: routesOptions
|
||||
}
|
||||
]
|
||||
|
||||
module.exports = createRoutes
|
||||
141
core/server/routes/createPrimaryRoutes.js
Normal file
141
core/server/routes/createPrimaryRoutes.js
Normal file
@@ -0,0 +1,141 @@
|
||||
const path = require('path')
|
||||
|
||||
const createCommonRoutes = require('./createCommonRoutes.js')
|
||||
|
||||
const createPrimaryRoutes = (config, plugins) => {
|
||||
const routes = createCommonRoutes(config)
|
||||
|
||||
let myPlugins = plugins
|
||||
|
||||
const pluginFolders = {}
|
||||
|
||||
const routesOptions = {
|
||||
security: {
|
||||
hsts: {
|
||||
maxAge: 15768000,
|
||||
includeSubDomains: true,
|
||||
preload: true
|
||||
},
|
||||
xframe: 'sameorigin'
|
||||
}
|
||||
}
|
||||
|
||||
plugins.reduce((obj, plugin) => {
|
||||
obj[plugin.name] = plugin.folder
|
||||
return obj
|
||||
}, pluginFolders)
|
||||
|
||||
|
||||
routes.push(
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/',
|
||||
handler: (request, reply) => {
|
||||
return reply.redirect('/app')
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/{path*}',
|
||||
handler: (request, h) => {
|
||||
const filePath = path.join(__dirname, '../../public/index.html')
|
||||
const response = h.file(filePath, {
|
||||
confine: true
|
||||
})
|
||||
response.header('Access-Control-Allow-Origin', request.info.host)
|
||||
return response
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/getPlugins',
|
||||
handler: (request, h) => {
|
||||
return { plugins: myPlugins.map(p => p.name) }
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/build/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: config.build.options.outputDir,
|
||||
redirectToSlash: true,
|
||||
index: true
|
||||
}
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/src/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: path.join(__dirname, '../../src'),
|
||||
redirectToSlash: true,
|
||||
index: true
|
||||
}
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/plugin/{path*}',
|
||||
handler: (request, h) => {
|
||||
|
||||
const plugin = request.params.path.split('/')[0]
|
||||
const filePath = path.join(pluginFolders[plugin], '../', request.params.path)
|
||||
|
||||
const response = h.file(filePath, {
|
||||
confine: false
|
||||
})
|
||||
response.header('Access-Control-Allow-Origin', request.info.host)
|
||||
return response
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/plugin/404',
|
||||
handler: (request, h) => {
|
||||
const response = h.file(path.join(config.server.primary.page404))
|
||||
response.header('Access-Control-Allow-Origin', request.info.host)
|
||||
return response
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/qortal-components/plugin-mainjs-loader.html',
|
||||
handler: (request, h) => {
|
||||
const response = h.file(path.join(__dirname, '../../src/plugins/plugin-mainjs-loader.html'), {
|
||||
confine: false
|
||||
})
|
||||
response.header('Access-Control-Allow-Origin', request.info.host)
|
||||
return response
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/qortal-components/plugin-mainjs-loader.js',
|
||||
handler: (request, h) => {
|
||||
const file = path.join(config.build.options.outputDir, '/plugins/plugin-mainjs-loader.js')
|
||||
|
||||
const response = h.file(file, {
|
||||
confine: false
|
||||
})
|
||||
response.header('Access-Control-Allow-Origin', request.info.host)
|
||||
return response
|
||||
},
|
||||
options: routesOptions
|
||||
},
|
||||
|
||||
)
|
||||
|
||||
return routes
|
||||
}
|
||||
|
||||
module.exports = createPrimaryRoutes
|
||||
24
core/server/server.js
Normal file
24
core/server/server.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const ServerFactory = require('./ServerFactory.js')
|
||||
|
||||
const createPrimaryRoutes = require('./routes/createPrimaryRoutes.js')
|
||||
|
||||
const createServer = (config, plugins) => {
|
||||
this.start = async function () {
|
||||
const primaryServer = new ServerFactory(createPrimaryRoutes(config, plugins), config.user.server.primary.host, config.user.server.primary.port, config.user.tls.enabled ? config.user.tls.options : void 0)
|
||||
primaryServer.startServer()
|
||||
.then(server => {
|
||||
console.log(`Qortal UI Server started at ${server.info.uri} and listening on ${server.info.address}`)
|
||||
})
|
||||
.catch(e => {
|
||||
console.error(e)
|
||||
})
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
const serverExports = {
|
||||
createServer
|
||||
}
|
||||
|
||||
module.exports = serverExports
|
||||
Reference in New Issue
Block a user