nfp_sites/api/server.mjs

85 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-07-20 00:33:06 +00:00
import { Flaska, QueryHandler, JsonHandler, FormidableHandler } from 'flaska'
import formidable from 'formidable'
2022-04-05 16:47:24 +00:00
2022-06-16 22:44:43 +00:00
import { initPool } from './db.mjs'
2022-04-05 16:47:24 +00:00
import config from './config.mjs'
import PageRoutes from './page/routes.mjs'
import ServeHandler from './serve.mjs'
2022-07-20 00:33:06 +00:00
import ArticleRoutes from './article/routes.mjs'
import AuthenticationRoutes from './authentication/routes.mjs'
import { authenticate } from './authentication/security.mjs'
2022-04-05 14:43:23 +00:00
export function run(http, port, core) {
let localUtil = new core.sc.Util(import.meta.url)
// Create our server
2022-04-05 14:43:23 +00:00
const flaska = new Flaska({
2022-07-20 00:33:06 +00:00
appendHeaders: {
'Content-Security-Policy': `default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src * data: blob:; font-src 'self' data:; object-src 'none'; frame-ancestors 'none'`,
},
2022-04-05 14:43:23 +00:00
log: core.log,
2022-04-05 16:47:24 +00:00
nonce: ['script-src'],
nonceCacheLength: 50,
2022-04-05 14:43:23 +00:00
}, http)
2022-06-16 22:44:43 +00:00
// Create our database pool
2022-06-16 22:44:43 +00:00
let pool = initPool(core, config.get('mssql'))
// configure our server
2022-04-05 16:47:24 +00:00
if (config.get('NODE_ENV') === 'development') {
flaska.devMode()
}
2022-04-05 14:43:23 +00:00
flaska.before(function(ctx) {
ctx.state.started = new Date().getTime()
ctx.db = pool
2022-04-05 14:43:23 +00:00
})
2022-04-05 16:47:24 +00:00
flaska.before(QueryHandler())
2022-04-05 14:43:23 +00:00
flaska.after(function(ctx) {
let ended = new Date().getTime()
var requestTime = ended - ctx.state.started
let status = ''
let level = 'info'
if (ctx.status >= 400) {
status = ctx.status + ' '
level = 'warn'
}
if (ctx.status >= 500) {
level = 'error'
}
ctx.log[level]({
duration: requestTime,
status: ctx.status,
}, `<-- ${status}${ctx.method} ${ctx.url}`)
})
2022-04-05 16:47:24 +00:00
const page = new PageRoutes()
flaska.get('/api/pagetree', page.getPageTree.bind(page))
2022-06-28 08:15:51 +00:00
flaska.get('/api/frontpage', page.getPage.bind(page))
flaska.get('/api/pages/:path', page.getPage.bind(page))
2022-04-19 15:51:47 +00:00
// flaska.get('/api/pages/:pageId', page.getSinglePage.bind(page))
2022-04-05 16:47:24 +00:00
2022-07-20 00:33:06 +00:00
const article = new ArticleRoutes()
flaska.get('/api/articles/:path', article.getArticle.bind(article))
flaska.get('/api/auth/articles', authenticate(), article.auth_getAllArticles.bind(article))
flaska.get('/api/auth/articles/:path', authenticate(), article.auth_getSingleArticle.bind(article))
flaska.put('/api/auth/articles/:path', [authenticate(), FormidableHandler(formidable) ], article.auth_updateCreateSingleArticle.bind(article))
2022-04-19 15:51:47 +00:00
// flaska.get('/api/pages/:pageId/articles/public', article.getPublicAllPageArticles.bind(article))
2022-04-05 14:43:23 +00:00
2022-07-20 00:33:06 +00:00
const authentication = new AuthenticationRoutes()
flaska.post('/api/authentication/login', JsonHandler(), authentication.login.bind(authentication))
const serve = new ServeHandler({
root: localUtil.getPathFromRoot('../public'),
2022-06-16 22:44:43 +00:00
version: core.app.running,
frontend: config.get('frontend:url'),
2022-04-05 14:43:23 +00:00
})
flaska.get('/::file', serve.serve.bind(serve))
2022-04-05 14:43:23 +00:00
return flaska.listenAsync(port).then(function() {
core.log.info('Server is listening on port ' + port)
})
}