Change the way data are loaded on the server

This commit is contained in:
LordMathis 2019-12-07 18:56:00 +01:00
parent a9b560346c
commit d4f41ec33e
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
7 changed files with 58 additions and 56 deletions

View File

@ -9,8 +9,7 @@
}, },
"baseUrl": "example.com", "baseUrl": "example.com",
"contentPath": "./content", "contentPath": "./content",
"renderPath": "./renders", "storage": "file",
"dataPath": "./src/utils/data.json",
"non-content-files": [ "non-content-files": [
"about.md", "resume.md" "about.md", "resume.md"
] ]

View File

@ -17,14 +17,14 @@ export default class MainContainer extends Component {
data = window.__INITIAL_DATA__ data = window.__INITIAL_DATA__
delete window.__INITIAL_DATA__ delete window.__INITIAL_DATA__
} }
this.state = { this.state = {
isLoadingBlog: !data[0].posts, isLoadingBlog: !data[0].posts,
isLoadingAbout: !data[0].other.about, isLoadingAbout: !data[0].other.about,
about: data[0].other.about, about: data[0].other.about,
posts: data[0].posts, posts: data[0].posts,
config: data[1] config: data[1]
} }
} }
render () { render () {

View File

@ -7,6 +7,7 @@ import chokidar from 'chokidar'
import jsonfile from 'jsonfile' 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'
const port = process.env.PORT || 3000 const port = process.env.PORT || 3000
const app = express() const app = express()
@ -65,7 +66,8 @@ if (head == null) {
} }
} }
const serverRenderer = new ServerRenderer(head, config) const dataGetter = new DataGetter(config)
const serverRenderer = new ServerRenderer(head, config, dataGetter)
app.get('*', serverRenderer.render.bind(serverRenderer)) app.get('*', serverRenderer.render.bind(serverRenderer))
app.listen(port, function (error) { app.listen(port, function (error) {

View File

@ -1,34 +0,0 @@
import fs from 'fs'
import jsonfile from 'jsonfile'
import path from 'path'
export function getData (reqPath = '') {
if (reqPath === '') {
return readJson(path.join(process.cwd(), 'data.json'))
} else if (reqPath === 'resume') {
const fileName = path.join(process.cwd(), 'content', reqPath + '.md')
return readFile(fileName, 'resume', 'utf8')
} else {
const fileName = path.join(process.cwd(), 'content', reqPath + '.md')
return readFile(fileName, 'post', 'utf8')
}
};
function readFile (fileName, type, options) {
return new Promise(function (resolve, reject) {
fs.readFile(fileName, options, (err, data) => {
err ? reject(err) : resolve({
type: type,
data: data
})
})
})
}
function readJson (dataPath) {
return new Promise(function (resolve, reject) {
jsonfile.readFile(dataPath, (err, data) => {
err ? reject(err) : resolve(data)
})
})
}

46
src/utils/dataGetter.js Normal file
View File

@ -0,0 +1,46 @@
import fs from 'fs'
import jsonfile from 'jsonfile'
import path from 'path'
export class DataGetter {
constructor (config) {
this.config = config
}
getData (reqPath) {
if (this.config.storage === 'file') {
return this.getDataFromFile(reqPath)
}
}
getDataFromFile (reqPath) {
if (reqPath === '') {
return this.readJson(path.join(process.cwd(), 'data.json'))
} else if (reqPath === 'resume') {
const fileName = path.join(process.cwd(), 'content', reqPath + '.md')
return this.readFile(fileName, 'resume', 'utf8')
} else {
const fileName = path.join(process.cwd(), 'content', reqPath + '.md')
return this.readFile(fileName, 'post', 'utf8')
}
}
readFile (fileName, type, options) {
return new Promise(function (resolve, reject) {
fs.readFile(fileName, options, (err, data) => {
err ? reject(err) : resolve({
type: type,
data: data
})
})
})
}
readJson (dataPath) {
return new Promise(function (resolve, reject) {
jsonfile.readFile(dataPath, (err, data) => {
err ? reject(err) : resolve(data)
})
})
}
}

View File

@ -1,28 +1,18 @@
import { MainContainer, ContentContainer } from '../containers' import { MainContainer, ContentContainer } from '../containers'
import { getData } from './api'
const routes = [ const routes = [
{ {
path: '/', path: '/',
exact: true, exact: true,
component: MainContainer, component: MainContainer
getData: (path = '') => getData(
path.split('/').pop()
)
}, },
{ {
path: '/post/:postname', path: '/post/:postname',
component: ContentContainer, component: ContentContainer
getData: (path = '') => getData(
path.split('/').pop()
)
}, },
{ {
path: '/resume', path: '/resume',
component: ContentContainer, component: ContentContainer
getData: (path = '') => getData(
path.split('/').pop()
)
} }
] ]

View File

@ -7,9 +7,10 @@ import serialize from 'serialize-javascript'
import manifest from '../../public/static/manifest.json' import manifest from '../../public/static/manifest.json'
export class ServerRenderer { export class ServerRenderer {
constructor (head, config) { constructor (head, config, dataGetter) {
this.head = head this.head = head
this.config = config this.config = config
this.dataGetter = dataGetter
} }
render (req, res, next) { render (req, res, next) {
@ -26,9 +27,7 @@ export class ServerRenderer {
) )
res.status(404).send(renderFullPage(markup, head, {}, config)) res.status(404).send(renderFullPage(markup, head, {}, config))
} else { } else {
const promise = activeRoute.getData const promise = this.dataGetter.getData(req.path.split('/').pop())
? activeRoute.getData(req.path)
: Promise.resolve()
promise.then((data) => { promise.then((data) => {
const context = [data, config] const context = [data, config]