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.

99 lines
2.0 KiB

import fastify from 'fastify'
import dotenv from 'dotenv'
import path from 'path'
// getting .env
dotenv.config({ path: path.join(path.resolve(), '/../../.env') })
// create server
const server = fastify()
/**
* Add liquidjs
*
*
*/
import { Liquid } from 'liquidjs'
import view from '@fastify/view'
const engine = new Liquid({
root: path.join(path.resolve(), '/../frontend/views'),
extname: '.liquid',
})
server.register(view, {
engine: {
liquid: engine
}
})
/**
* adding preHandler
*
*/
import SettingsStore from './stores/settings.js'
// getting options from directus add to all views
server.addHook('preHandler', async function (request, response) {
const settingsStore = new SettingsStore()
const settings = await settingsStore.find()
response.locals = {
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)
export default server