This commit is contained in:
2021-11-17 15:36:58 +01:00
parent fc79be588f
commit ad13a6f0c1
12 changed files with 244 additions and 37 deletions

View File

@@ -1,6 +1,9 @@
import type { FastifyInstance } from 'fastify'
import convict from 'convict'
import yaml from 'js-yaml'
convict.addFormat(require('convict-format-with-validator').ipaddress)
export enum StorageType {
Local = 'local',
// S3 = 's3',
@@ -36,6 +39,20 @@ function formatNullableStringOrRegexpArray(values: any) {
convict.addParser({ extension: ['yml', 'yaml'], parse: (s) => yaml.load(s, { schema: Schema }) })
const config = convict({
// Server
port: {
doc: 'The port to bind.',
format: 'port',
default: 80,
env: 'PORT',
},
address: {
doc: 'The address to bind.',
format: 'ipaddress',
default: '127.0.0.1',
env: 'ADDRESS',
},
// Security
allowedDomains: {
doc: 'The domains that are allowed to be used as image sources',
@@ -89,16 +106,18 @@ for (const file of ['morphus.yaml', 'morphus.yaml', 'morphus.json']) {
} catch {}
}
try {
config.validate({ allowed: 'strict' })
} catch (e) {
if (e instanceof Error) {
console.error(e.message)
} else {
console.error(e)
export function init(App: FastifyInstance) {
try {
config.validate({ allowed: 'strict' })
App.log.info(config.toString())
} catch (e) {
if (e instanceof Error) {
App.log.error(e.message)
} else {
App.log.error(e)
}
process.exit(1)
}
process.exit(1)
}
export const Config = config.get()
console.debug(Config)
export const Config = config.get()

View File

@@ -177,9 +177,9 @@ export const image: RouteHandlerMethod = async (request, reply) => {
}
if (Config.allowedHosts) {
const host = request.headers.host
console.debug('Testing host', host, Config.allowedHosts)
if (!host || !testForPrefixOrRegexp(host, Config.allowedHosts)) return ForbiddenError(reply, 'host not allowed')
const origin = request.headers.origin
if (!origin || !testForPrefixOrRegexp(origin, Config.allowedHosts))
return ForbiddenError(reply, 'origin not allowed')
}
// @ts-ignore

View File

@@ -1,2 +1,9 @@
export * from './image'
export * from './version'
import { FastifyInstance } from 'fastify'
import { image } from './image'
import { version } from './version'
export function init(App: FastifyInstance) {
App.get('/api/image', image)
App.get('/version', version)
}

8
src/fastify/hooks.ts Normal file
View File

@@ -0,0 +1,8 @@
import { FastifyInstance } from 'fastify'
export function init(App: FastifyInstance) {
App.addHook('preHandler', (request, reply, done) => {
reply.header('Server', 'morphus')
done()
})
}

View File

@@ -0,0 +1,8 @@
import { FastifyInstance } from 'fastify'
export function init(App: FastifyInstance) {
App.register(require('under-pressure'))
App.register(require('fastify-caching'))
App.register(require('fastify-compress'), { global: true })
App.register(require('fastify-cors'), { origin: '*' })
}

View File

@@ -1,35 +1,34 @@
// Require the framework and instantiate it
import fastify from 'fastify'
import compress from 'fastify-compress'
import cors from 'fastify-cors'
import underPressure from 'under-pressure'
import './config'
import { version } from './controllers'
import { image } from './controllers/image'
import { init } from './storage'
import { Config, init as initConfig } from './config'
import { init as initRoutes } from './controllers'
import { init as initStorage } from './storage'
import { init as initMiddleware } from './fastify/middleware'
import { init as initHooks } from './fastify/hooks'
init()
export const App = fastify({ logger: { prettyPrint: true } })
const app = fastify({ logger: true })
app.register(underPressure)
app.register(require('fastify-caching'))
app.register(compress, { global: true })
app.register(cors, { origin: true })
// Internal
initConfig(App)
initStorage()
app.addHook('preHandler', (request, reply, done) => {
reply.header('Server', 'morphus')
done()
// Fastify
initMiddleware(App)
initHooks(App)
initRoutes(App)
process.on('SIGINT', async function () {
App.log.info('Stopping server')
// Close with 2s timeout
await Promise.race([App.close(), new Promise((resolve) => setTimeout(resolve, 2000))])
process.exit()
})
app.get('/api/image', image)
app.get('/version', version)
async function start() {
try {
await app.listen(3000)
await App.listen(Config.port, Config.address)
} catch (err) {
app.log.error(err)
App.log.error(err)
process.exit(1)
}
}