2019-09-06 01:20:24 +02:00
'use strict'
require ( 'make-promises-safe' ) ; // installs an 'unhandledRejection' handler
const path = require ( 'path' ) ;
2019-09-07 02:23:55 +02:00
let siteConfig ;
try {
siteConfig = require ( './config.json' ) ;
} catch ( ex ) {
console . error ( 'Please copy `config.example.json` to `config.json` and fill it with your server\'s data.' ) ;
process . exit ( 1 ) ;
}
2019-09-06 01:20:24 +02:00
const fastify = require ( 'fastify' ) ( {
2019-09-07 02:12:56 +02:00
logger : process . env . NODE _ENV !== 'production' ,
2019-09-06 01:20:24 +02:00
} ) ;
2019-09-07 06:17:45 +02:00
fastify . decorate ( 'siteConfig' , siteConfig ) ; // Insert siteConfig into global fastify instance
fastify . register ( require ( 'fastify-helmet' ) ) ; // Add security stuff
fastify . register ( require ( 'fastify-compress' ) ) ; // Compress output data for smaller packet delivery
fastify . register ( require ( 'fastify-formbody' ) ) ; // Enable fastify to parse data sent by POST from forms
fastify . register ( require ( 'fastify-static' ) , { // Enable delivering static content efficiently
root : path . join ( _ _dirname , 'public' ) , // all static content will be delivered from the public/ folder
2019-09-06 01:20:24 +02:00
} ) ;
2019-09-07 06:17:45 +02:00
fastify . register ( require ( 'point-of-view' ) , { // Adds the `view()` function to fastify's `reply` objects
2019-09-06 01:20:24 +02:00
engine : {
2019-09-07 06:17:45 +02:00
handlebars : require ( 'handlebars' ) , // Use handlebar as the render engine for `reply.view()`
2019-09-06 01:20:24 +02:00
} ,
2019-09-07 06:17:45 +02:00
templates : 'views' , // Search for all files referenced in `reply.view()` within the `views/` folder
2019-09-06 01:20:24 +02:00
options : {
2019-09-07 06:17:45 +02:00
useHtmlMinifier : require ( 'html-minifier' ) , // Add a minifier to the rendered HTML output
2019-09-06 01:20:24 +02:00
htmlMinifierOptions : {
removeComments : true ,
removeCommentsFromCDATA : true ,
collapseWhitespace : true ,
collapseBooleanAttributes : true ,
removeEmptyAttributes : true
} ,
2019-09-07 06:17:45 +02:00
partials : { // Specifies the Handlebars Partials so `point-of-view` knows where they are within the `views` folder and what they're called when referenced in a `.hbs` file
2019-09-07 00:24:42 +02:00
layout : 'layout.hbs' ,
2019-09-06 01:20:24 +02:00
header : 'partials/header.hbs' ,
footer : 'partials/footer.hbs' ,
}
} ,
} ) ;
2019-09-07 06:17:45 +02:00
fastify . register ( require ( 'fastify-cookie' ) ) ; // Enable reading and setting http-level cookies for the sole purpose of storing login tokens
fastify . register ( require ( 'fastify-jwt' ) , { // Enable creating, parsing, and verifying JSON Web Tokens from the global fastify object
secret : fastify . siteConfig . jwtSecretKey , // The secret key used to generate JWTs. Make it big and random!
} ) ;
// Every request, check to see if a valid token exists
fastify . addHook ( 'onRequest' , ( request , reply , done ) => {
request . isLoggedInUser = typeof request . cookies . token !== 'undefined' && fastify . jwt . verify ( request . cookies . token ) ;
done ( ) ;
} ) ;
2019-09-06 01:20:24 +02:00
2019-09-07 02:23:55 +02:00
2019-09-06 01:20:24 +02:00
// Routes
fastify . register ( require ( './routes/resources' ) ) ;
fastify . register ( require ( './routes/home' ) ) ;
2019-09-07 01:20:27 +02:00
fastify . register ( require ( './routes/search' ) ) ;
2019-09-06 01:20:24 +02:00
// Start the server
2019-09-07 02:23:55 +02:00
fastify . listen ( fastify . siteConfig . port , function ( err , address ) {
2019-09-06 01:20:24 +02:00
if ( err ) {
fastify . log . error ( err ) ;
process . exit ( 1 ) ;
}
fastify . log . info ( ` server listening on ${ address } ` ) ;
} ) ;