From 8c165e3121827d228e7e6b2e286bccda678904b8 Mon Sep 17 00:00:00 2001 From: HerrHase Date: Thu, 28 Jul 2022 22:24:12 +0200 Subject: [PATCH] adding --- packages/server/_bootstrap.js | 41 +++++++++++++++-- packages/server/http/{ => api}/comment.js | 10 ++++ packages/server/http/notfound.js | 6 ++- packages/server/http/sitemap.js | 56 +++++++++++++++++++++++ packages/server/http/static.js | 6 +-- packages/server/stores/Settings.js | 15 ++++-- 6 files changed, 122 insertions(+), 12 deletions(-) rename packages/server/http/{ => api}/comment.js (79%) create mode 100644 packages/server/http/sitemap.js diff --git a/packages/server/_bootstrap.js b/packages/server/_bootstrap.js index e8e747a..e949a44 100644 --- a/packages/server/_bootstrap.js +++ b/packages/server/_bootstrap.js @@ -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) diff --git a/packages/server/http/comment.js b/packages/server/http/api/comment.js similarity index 79% rename from packages/server/http/comment.js rename to packages/server/http/api/comment.js index c8c6ea2..fb07261 100644 --- a/packages/server/http/comment.js +++ b/packages/server/http/api/comment.js @@ -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 + }) }) } \ No newline at end of file diff --git a/packages/server/http/notfound.js b/packages/server/http/notfound.js index 1d9b6bc..5c83bc4 100644 --- a/packages/server/http/notfound.js +++ b/packages/server/http/notfound.js @@ -1,5 +1,5 @@ /** - * notfound + * notfound * * @author Björn Hase * @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') }) } \ No newline at end of file diff --git a/packages/server/http/sitemap.js b/packages/server/http/sitemap.js new file mode 100644 index 0000000..87a1f5c --- /dev/null +++ b/packages/server/http/sitemap.js @@ -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 + * @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) => { + + }) + + + }) +} \ No newline at end of file diff --git a/packages/server/http/static.js b/packages/server/http/static.js index b8586a2..f7b678f 100644 --- a/packages/server/http/static.js +++ b/packages/server/http/static.js @@ -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 * @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 * */ diff --git a/packages/server/stores/Settings.js b/packages/server/stores/Settings.js index 8e576f8..5d8803b 100644 --- a/packages/server/stores/Settings.js +++ b/packages/server/stores/Settings.js @@ -1,11 +1,13 @@ import DirectusAbstractStore from './DirectusAbstract.js' /** + * Settings * - * + * @author Björn Hase + * @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 \ No newline at end of file +export default SettingsStore \ No newline at end of file