@kohanajs/platform-web-fastify
Version:
The Web Platform support using fastify
49 lines (38 loc) • 1.49 kB
JavaScript
const { KohanaJS } = require('kohanajs');
const { RouteList } = require('@kohanajs/mod-route');
const RouteAdapter = require('../routeAdapter/Fastify');
const path = require('path');
class ServerAdapterFastify {
static async setup() {
const app = require('fastify')({
logger: false,
ignoreTrailingSlash: true,
});
app.register(require('@fastify/static'), {
root: path.normalize(`${KohanaJS.APP_PATH}/../public/media`),
prefix: '/media/',
})
app.register(require('@fastify/formbody'));
app.addContentTypeParser('multipart/form-data', function (request, payload, done) {
done();
})
if (KohanaJS.config.cookie) {
app.register(require('@fastify/cookie'), {
secret: KohanaJS.config.cookie.salt,
parseOptions: KohanaJS.config.cookie.options,
});
}
await app.register(require('@fastify/express'));
if (KohanaJS.config.setup?.notFound) {
app.setNotFoundHandler((request, reply) => {
// Default not found handler with preValidation and preHandler hooks
const { language } = request.params;
const url = language ? `/${language}/pages/404` : '/pages/404';
reply.redirect(`${url}?s=${request.url.replace('?', '%3F').replaceAll('&', '%26')}`);
});
}
RouteList.createRoute(app, RouteAdapter);
return {listen:port => app.listen({port})};
}
}
module.exports = ServerAdapterFastify;