You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.2 KiB
64 lines
1.2 KiB
import fs from 'fs'
|
|
|
|
/**
|
|
* handle pages
|
|
*
|
|
* @author Björn Hase
|
|
* @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3
|
|
* @link https://gitea.node001.net/HerrHase/tellme-bot.git
|
|
*
|
|
*/
|
|
|
|
export default async function(fastify, opts)
|
|
{
|
|
|
|
/**
|
|
*
|
|
*
|
|
* @param {object} request
|
|
* @param {object} response
|
|
*
|
|
*/
|
|
fastify.get('/v1/:name', async function (request, response) {
|
|
|
|
// create file path
|
|
const filePath = process.env.APP_PUBLIC_PATH + '/' + request.params.name + '.json'
|
|
|
|
// if file not exists
|
|
if (!fs.existsSync(filePath)) {
|
|
return response
|
|
.code(404)
|
|
.send()
|
|
}
|
|
|
|
// loading data
|
|
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'))
|
|
|
|
// results
|
|
let results = []
|
|
|
|
// filter
|
|
if (request.query.filter) {
|
|
data.forEach((single) => {
|
|
|
|
})
|
|
} else {
|
|
results = data
|
|
}
|
|
|
|
// getting offset
|
|
let offset = !request.query.offset ? 0 : request.query.offset
|
|
|
|
// getting limit
|
|
let limit = !request.query.limit ? results.length : request.query.limit
|
|
|
|
// offset and limit
|
|
results = results.slice(offset, limit)
|
|
|
|
response
|
|
.header('Content-Type', 'application/json; charset=utf-8')
|
|
.code(200)
|
|
.send(results)
|
|
})
|
|
}
|