develop
HerrHase 2 years ago
parent 8b60ae7203
commit 8c165e3121

@ -35,31 +35,64 @@ server.register(view, {
*
*/
import OptionStore from './stores/Option.js'
import SettingsStore from './stores/settings.js'
// getting options from directus add to all views
server.addHook('preHandler', async function (request, response) {
const optionStore = new OptionStore()
const options = await optionStore.find()
const settingsStore = new SettingsStore()
const settings = await settingsStore.find()
response.locals = {
options: options
settings: settings
}
})
// check url for paged
server.addHook('preHandler', async function (request, response) {
const url = new URL(request.params.url)
const pathname = url.pathname.split('/')
// default value for paged
let paged = 1
// check if pathname has values
if (pathname.length > 0) {
const result = pathname[pathname.length - 1]
// is result is integer
if (Number.isInteger(result)) {
paged = result
}
}
// adding to response
response.locals = {
paged : paged
}
})
// routing
import commentHttp from './http/api/comment.js'
import notfoundHttp from './http/notfound.js'
import postHttp from './http/post.js'
// page and static has always the last routes, if no route before match, it try get a page
import sitemapHttp from './http/sitemap.js'
import pageHttp from './http/page.js'
import staticHttp from './http/static.js'
server
.register(commentHttp, {
'prefix': '/api/comment/v1/'
})
.register(notfoundHttp)
.register(postHttp, {
'prefix': '/blog'
})
.register(sitemapHttp)
.register(pageHttp)
.register(staticHttp)

@ -32,5 +32,15 @@ export default async function(fastify, opts) {
'content' : request.body.content
})
// if not comment has created send 400
if (!comment) {
return response
.code(400)
}
return response
.send({
'success': true
})
})
}

@ -1,5 +1,5 @@
/**
* notfound
* notfound
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
@ -17,7 +17,9 @@ export default async function(fastify, opts) {
*
*/
fastify.get('/404', async function(request, response) {
response.view('../frontend/views/404')
return response
.code(404)
.view('../frontend/views/404')
})
}

@ -0,0 +1,56 @@
import path from 'path'
import PageStore from './../stores/Page.js'
import directusResponseHandler from './../handlers/directusResponse.js'
/**
* page
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.node001.net/HerrHase/super-fastify-directus.git
*
*/
export default async function(fastify, opts) {
/**
* prehandler for all routes
*
* @param {[type]} request
* @param {[type]} reponse
* @param {Function} done
* @return {[type]}
*/
fastify.addHook('preHandler', async function(request, response) {
const pageStore = new PageStore()
// getting single page
let page = await pageStore.find(request.url)
directusResponseHandler.getOne(page, response)
})
/**
* handle single page
*
* @param {object} request
* @param {object} response
*
*/
fastify.get('/sitemap.xml', async function(request, response) {
const pageStore = new PageStore()
// getting single page
let pages = await pageStore.find()
pages.each((page, index) => {
})
})
}

@ -2,11 +2,11 @@ import fastifyStatic from '@fastify/static'
import path from 'path'
/**
* handle static files that are created by frontend package
* static files that are send frontend package
*
* @author Björn Hase
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://github.com/tentakelfabrik/fastify-lowdb-riotjs-lessons-learned
* @link https://gitea.node001.net/HerrHase/super-fastify-directus.git
*
*/

@ -1,11 +1,13 @@
import DirectusAbstractStore from './DirectusAbstract.js'
/**
* Settings
*
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.node001.net/HerrHase/super-fastify-directus.git
*
*/
class SettingsStore extends DirectusAbstractStore {
/**
@ -18,6 +20,13 @@ class SettingsStore extends DirectusAbstractStore {
super('directus_settings')
}
/**
* getting setting
*
*
* @return {object}
*
*/
find() {
return this.items.readByQuery({
fields: [
@ -32,4 +41,4 @@ class SettingsStore extends DirectusAbstractStore {
}
export default OptionStore
export default SettingsStore
Loading…
Cancel
Save