summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app.js35
-rw-r--r--source/controller.js67
-rw-r--r--source/readfile.js60
3 files changed, 112 insertions, 50 deletions
diff --git a/app.js b/app.js
index 3998b0b..31609a0 100644
--- a/app.js
+++ b/app.js
@@ -81,9 +81,16 @@ class App {
break
default:
if (this.path.string.endsWith('/') && this.path.string.length > 1) this.path.string = this.path.string.substring(0, this.path.string.length - 1)
- if (output.has(this.path.string)) {res.send(this.readfile.GetMain())} else {res.sendStatus(404)}
+ if (output.has(this.path.string)) {
+ try {
+ output.get(this.path.string).then(output => res.send(this.readfile.GetMain(output.meta)))
+ } catch {
+ res.send(this.readfile.GetMain(output.get(this.path.string)))
+ }
+ } else {
+ res.sendStatus(404)
+ }
break
-
}
})
})
@@ -97,17 +104,25 @@ class App {
this.controller.Main(req.body.value).then(output => {
if (output.has(this.path.string)) {
const data = this.readfile.Create(output.get(this.path.string))
- data.then(output => {
- try {
+ try {
+ output.get(this.path.string).then(output => {
+ const object = this.readfile.Create(output.html)
const window = new JSDOM('').window
const DOMPurify = createDOMPurify(window)
- const clean = DOMPurify.sanitize(output.layouts.header + output.layouts.data + output.layouts.footer, { ADD_TAGS: ["iframe"], ADD_ATTR: ['allow', 'allowfullscreen', 'frameborder', 'scrolling'] })
+ object.then(output => {
+ const clean = DOMPurify.sanitize(output.layouts.header + output.layouts.data + output.layouts.footer, { ADD_TAGS: ["iframe"], ADD_ATTR: ['allow', 'allowfullscreen', 'frameborder', 'scrolling'] })
+ res.send(clean)
+ })
+ })
+ } catch {
+ const object = this.readfile.Create(output.get(this.path.string).html)
+ const window = new JSDOM('').window
+ const DOMPurify = createDOMPurify(window)
+ object.then(output => {
+ const clean = DOMPurify.sanitize(output.layouts.header + output.layouts.data + output.layouts.footer, { ADD_TAGS: ['iframe'], ADD_ATTR: ['allow', 'allowfullscreen', 'frameborder', 'scrolling'] })
res.send(clean)
- } catch (err) {
- logger.Error(err)
- res.send('Not Found')
- }
- })
+ })
+ }
} else {
res.send('Not Found')
}
diff --git a/source/controller.js b/source/controller.js
index 2c8cb37..c66df01 100644
--- a/source/controller.js
+++ b/source/controller.js
@@ -4,44 +4,26 @@ const logger = require('./logger')()
// Reads all scripts in the `scripts` folder, sends to `Method` class to get all methods in the class, and send output to `app.js`
class Controller {
- constructor() {}
-
- async Main(x) {
- return new Promise((res, rej) => {
- this.#LoadModules(x).then(output => {
- if (output) {
- res(output)
- } else {
- logger.Error('Unable to load modules')
- rej(false)
- }
- })
- })
- }
-
- #GetMethods(x) {return Method().GetMethods(x)}
- async #LoadScripts() {
- const loadScripts = new Promise((res, rej) => {
+ constructor() {
+ this.main = new Promise((res, rej) => {
fs.readdir('./scripts/', (err, files) => {
if (err) {
- // ! Remember logging
- logger.Error(`Error reading directory ${err}`)
- rej(false)
+ logger.Error(`${err.code}: Failed to read directory`)
+ rej(`${err.code}: Failed to read directory`)
}
this.require = new Map()
files.forEach(file => {
try {
- this.require.set(file.replace('.js', ''), require('../scripts/' + file)())
+ this.require.set(file.replace('.js', ''), require(`../scripts/${file}`)())
} catch (err) {
- logger.Error(err)
+ logger.Error(`${err.code}: Failed to read ${file}`)
+ rej(`${err.code}: Failed to read ${file}`)
}
})
res(this.require)
})
- })
-
- return loadScripts.then(output => {
+ }).then(output => {
return new Promise((res, rej) => {
const map = output
const object = {
@@ -55,21 +37,38 @@ class Controller {
object.return.set(key, this.#GetMethods(value))
})
} else {
- logger.Error(`'index.js' does not exist`)
- rej(false)
+ logger.Error(`ERROR: 'index.js' does not exist`)
+ rej(`ERROR: 'index.js' does not exist`)
}
res(object.return)
})
})
}
+
+ async Main(x) {
+ return new Promise((res, rej) => {
+ this.#LoadModules(x).then(output => {
+ if (output) {
+ res(output)
+ } else {
+ logger.Error('ERROR: Unable to load modules')
+ rej('ERROR: Unable to load modules')
+ }
+ })
+ })
+ }
+
+ #GetMethods(x) {return Method().GetMethods(x)}
async #LoadModules(x) {
return new Promise((res, rej) => {
- this.#LoadScripts().then(output => {
+ this.main.then(output => {
if (output.get('index')) {
this.methods = new Map()
+
output.forEach((value, key) => {
this.key = key
- if (this.key == 'index') {
+
+ if (this.key === 'index') {
this.methods.set('/', value.get('Main')())
} else {
value.forEach((value, key) => {
@@ -81,13 +80,15 @@ class Controller {
})
}
})
- if (this.methods != undefined) {
+ if (this.methods !== undefined) {
res(this.methods)
} else {
- rej(false)
+ logger.Error(`Error: Failed to get methods`)
+ rej(`Error: Failed to get methods`)
}
} else {
- logger.Error(`'index.js' does not exist`)
+ logger.Error(`Error: 'index.js does not exist'`)
+ rej(`Error: 'index.js' does not exist`)
}
})
})
diff --git a/source/readfile.js b/source/readfile.js
index 394ea2d..b0e0ed2 100644
--- a/source/readfile.js
+++ b/source/readfile.js
@@ -16,13 +16,13 @@ class ReadFile {
this.object = {
baseUrl: x.baseUrl,
mime: '',
- main: this.#Build(),
+ main: '',
types: x.mime,
layouts: {
header: `
<section id='header'>
<nav class='menulist' id='menu-list'>
- <img src='' alt='logo'/>
+ <img class='logo' src='' alt='logo'/>
<a class='list' href='/'>Home</a>
<a class='list' href='/about'>About</a>
<a class='list' href='/blog'>Blog</a>
@@ -56,13 +56,60 @@ class ReadFile {
}
}
- GetMain() {return this.object.main}
+ GetMain(x) {
+ const data = {}
+ try {
+ data.header = `
+ <html>
+ <head>
+ <title>Welcome to my website!</title>
+ <link rel='stylesheet' href='/css/prism.css'/>
+ <link rel='stylesheet' href='/css/main.css'/>
+ <meta name='description' content='${x.description}'>
+ <meta name='keywords' content='${x.keywords}'>
+ </head>
+ <body>
+ <section id='main'>
+ `,
+ data.footer = `
+ </section>
+ <script src='/js/purify.js'></script>
+ <script src='/js/main.js'></script>
+ </body>
+ </html>
+ `
+ } catch {
+ data.header = `
+ <html>
+ <head>
+ <title>Welcome to my website!</title>
+ <link rel='stylesheet' href='/css/main.css'/>
+ <link rel='stylesheet' href='/css/prism.css'/>
+ <meta name='description' content=''>
+ <meta name='keywords' content=''>
+ </head>
+ <body>
+ <section id='main'>
+ `
+ data.footer = `
+ </section>
+ <script src='/js/purify.js'></script>
+ <script src='/js/main.js'></script>
+ </body>
+ </html>
+ `
+ }
+ return this.#RemoveSpaces(data.header + data.footer)
+ }
async Create(x) {
try {
return this.#SetData({data: x, mime: this.object.types})
} catch(err) {
try {
- return await x.then(output => {return this.#SetData({data: output, mime: this.object.types})})
+ return await x.then(output => {
+ console.log(output)
+ return this.#SetData({data: output, mime: this.object.types})
+ })
} catch (err) {
logger.Error(`${err.code}: Failed to create HTML`)
}
@@ -81,7 +128,7 @@ class ReadFile {
}
GetFavicon() {
const object = {
- file: fs.createReadStream('./assets/favicon/favicon.ico').on('error', (err) => {
+ file: fs.createReadStream('./favicon/favicon.ico').on('error', (err) => {
logger.Error(`${err.code}: Failed to get 'favicon.ico'`)
return null
}),
@@ -121,9 +168,8 @@ class ReadFile {
object.layouts.data = this.#RemoveSpaces(x.data)
return object
}
- async #GetFileType(x) {return this.object.types[path.extname(`./assets/${x[1]}/${x[2]}`).slice(1)] || 'text/plain'}
- #Build() {return fs.readFileSync('./views/layouts/header.html', 'utf-8') + fs.readFileSync('./views/layouts/footer.html', 'utf-8')}
#RemoveSpaces(x) {return x.replace(/^(\n)\s+/gm, '$1')}
+ async #GetFileType(x) {return this.object.types[path.extname(`./assets/${x[1]}/${x[2]}`).slice(1)] || 'text/plain'}
}
module.exports = ReadFile