cache readme data

This commit is contained in:
Mylloon 2022-06-23 02:54:42 +02:00
parent 41996ca804
commit 991c5313c5
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 24 additions and 5 deletions

View file

@ -5,7 +5,7 @@ const readme = require('../utils/readme')
// Home Page
router.get('/', (_, res) => {
readme.data()
readme.get()
.then(data => res.render('index', { readme: marked.parse(data) }))
})

View file

@ -1,10 +1,29 @@
const fetch = require('node-fetch')
async function data () {
return await fetch('https://git.kennel.ml/Anri/Constnium/raw/branch/main/README.md')
.then(res => res.text())
// URL of the repo
const repoURL = 'https://git.kennel.ml/Anri/Constnium/raw/branch/main/README.md'
// 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 = {
data
get
}