cache readme data
This commit is contained in:
parent
41996ca804
commit
991c5313c5
2 changed files with 24 additions and 5 deletions
|
@ -5,7 +5,7 @@ const readme = require('../utils/readme')
|
||||||
|
|
||||||
// Home Page
|
// Home Page
|
||||||
router.get('/', (_, res) => {
|
router.get('/', (_, res) => {
|
||||||
readme.data()
|
readme.get()
|
||||||
.then(data => res.render('index', { readme: marked.parse(data) }))
|
.then(data => res.render('index', { readme: marked.parse(data) }))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,29 @@
|
||||||
const fetch = require('node-fetch')
|
const fetch = require('node-fetch')
|
||||||
|
|
||||||
async function data () {
|
// URL of the repo
|
||||||
return await fetch('https://git.kennel.ml/Anri/Constnium/raw/branch/main/README.md')
|
const repoURL = 'https://git.kennel.ml/Anri/Constnium/raw/branch/main/README.md'
|
||||||
.then(res => res.text())
|
|
||||||
|
// Store the data from the repo
|
||||||
|
let data = null
|
||||||
|
|
||||||
|
// Store last time data as been pulled from the repo
|
||||||
|
let timestamp = 0
|
||||||
|
|
||||||
|
// Retrieve the data from the repo
|
||||||
|
async function get () {
|
||||||
|
const now = Date.now()
|
||||||
|
// If data is older than one day, refresh it
|
||||||
|
if (Math.ceil(Math.abs(now - timestamp) / (1000 * 3600 * 24)) > 2 || data === null) {
|
||||||
|
await fetch(repoURL)
|
||||||
|
.then(res => {
|
||||||
|
data = res.text()
|
||||||
|
timestamp = now
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
data
|
get
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue