Store data in memory instead of writing to file

This commit is contained in:
LordMathis 2019-12-07 19:28:18 +01:00
parent d4f41ec33e
commit feb5f7ccee
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
4 changed files with 34 additions and 33 deletions

View File

@ -8,6 +8,7 @@ import jsonfile from 'jsonfile'
import { ServerRenderer } from './utils/serverRender' import { ServerRenderer } from './utils/serverRender'
import { Scanner } from './utils/scanner' import { Scanner } from './utils/scanner'
import { DataGetter } from './utils/dataGetter' import { DataGetter } from './utils/dataGetter'
import { DataHolder } from './utils/dataHolder'
const port = process.env.PORT || 3000 const port = process.env.PORT || 3000
const app = express() const app = express()
@ -18,7 +19,8 @@ if (config == null) {
throw new Error('Config file not found!') throw new Error('Config file not found!')
} }
const scanner = new Scanner(config) const dataHolder = new DataHolder()
const scanner = new Scanner(config, dataHolder)
const watcher = chokidar.watch(path.join(process.cwd(), 'content'), { const watcher = chokidar.watch(path.join(process.cwd(), 'content'), {
ignored: /(^|[/\\])\../, // ignore dotfiles ignored: /(^|[/\\])\../, // ignore dotfiles
@ -66,7 +68,7 @@ if (head == null) {
} }
} }
const dataGetter = new DataGetter(config) const dataGetter = new DataGetter(config, dataHolder)
const serverRenderer = new ServerRenderer(head, config, dataGetter) const serverRenderer = new ServerRenderer(head, config, dataGetter)
app.get('*', serverRenderer.render.bind(serverRenderer)) app.get('*', serverRenderer.render.bind(serverRenderer))

View File

@ -3,8 +3,9 @@ import jsonfile from 'jsonfile'
import path from 'path' import path from 'path'
export class DataGetter { export class DataGetter {
constructor (config) { constructor (config, dataHolder) {
this.config = config this.config = config
this.dataHolder = dataHolder
} }
getData (reqPath) { getData (reqPath) {
@ -15,7 +16,7 @@ export class DataGetter {
getDataFromFile (reqPath) { getDataFromFile (reqPath) {
if (reqPath === '') { if (reqPath === '') {
return this.readJson(path.join(process.cwd(), 'data.json')) return Promise.resolve(this.dataHolder.data)
} else if (reqPath === 'resume') { } else if (reqPath === 'resume') {
const fileName = path.join(process.cwd(), 'content', reqPath + '.md') const fileName = path.join(process.cwd(), 'content', reqPath + '.md')
return this.readFile(fileName, 'resume', 'utf8') return this.readFile(fileName, 'resume', 'utf8')

22
src/utils/dataHolder.js Normal file
View File

@ -0,0 +1,22 @@
export class DataHolder {
constructor () {
this.data = {
posts: [],
other: {}
}
}
addPost (post) {
this.data.posts.push(post)
}
addOther (filename, data) {
this.data.other[filename] = data
}
deleteFile (filepath) {
this.data.posts = this.dataHolder.data.posts.filter((post) =>
post.filename !== filepath
)
}
}

View File

@ -2,20 +2,12 @@ import fs from 'fs'
import path from 'path' import path from 'path'
import fm from 'front-matter' import fm from 'front-matter'
import moment from 'moment' import moment from 'moment'
import jsonfile from 'jsonfile'
import zlib from 'zlib' import zlib from 'zlib'
export class Scanner { export class Scanner {
constructor (config) { constructor (config, dataHolder) {
this.config = config this.config = config
this.initData() this.dataHolder = dataHolder
}
initData () {
this.data = {
posts: [],
other: {}
}
} }
addFile (filepath) { addFile (filepath) {
@ -25,7 +17,6 @@ export class Scanner {
} else { } else {
this.readfile(filepath) this.readfile(filepath)
.then((data) => this.processFile(data[0], data[1])) .then((data) => this.processFile(data[0], data[1]))
.then(() => this.writeData())
} }
} }
@ -35,9 +26,7 @@ export class Scanner {
} }
deleteFile (filepath) { deleteFile (filepath) {
this.data.posts = this.data.posts.filter((post) => this.dataHolder.deleteFile(filepath)
post.filename !== filepath
)
} }
readfile (filePath) { readfile (filePath) {
@ -113,27 +102,14 @@ export class Scanner {
link: '/post/' + metadata.filename link: '/post/' + metadata.filename
} }
this.data.posts.push(post) this.dataHolder.addPost(post)
} else { } else {
this.data.other[metadata.filename] = data this.dataHolder.addOther(metadata.filename, data)
} }
return Promise.resolve() return Promise.resolve()
} }
writeData () {
return new Promise((resolve, reject) => {
jsonfile.writeFile(path.join(process.cwd(), 'data.json'), this.data, (err) => {
if (err) {
reject(err)
} else {
this.initData()
resolve()
}
})
})
}
fileMetadata (filepath) { fileMetadata (filepath) {
const paths = filepath.split('/') const paths = filepath.split('/')
const basename = path.basename(filepath) const basename = path.basename(filepath)