This commit is contained in:
cupcakearmy 2021-11-17 16:20:50 +01:00
parent ad13a6f0c1
commit cbc8ab0325
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
6 changed files with 33 additions and 21 deletions

View File

@ -19,5 +19,7 @@ COPY --from=builder /app/dist ./dist
ENV ASSETS=/data ENV ASSETS=/data
ENV ADDRESS=0.0.0.0 ENV ADDRESS=0.0.0.0
EXPOSE 80 EXPOSE 80
CMD [ "node", "dist/src" ]
CMD [ "node", "." ]

View File

@ -4,6 +4,7 @@
"description": "", "description": "",
"author": "Niccolo Borgioli", "author": "Niccolo Borgioli",
"license": "MIT", "license": "MIT",
"main": "dist/src",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"dev": "tsnd src" "dev": "tsnd src"

View File

@ -17,7 +17,7 @@ import ms from 'ms'
import { storage } from '../storage' import { storage } from '../storage'
import { transform } from '../transform' import { transform } from '../transform'
import { sha3, sortObjectByKeys, testForPrefixOrRegexp, validateSyncOrFail } from '../utils/utils' import { sha3, sortObjectByKeys, testForPrefixOrRegexp, validateSyncOrFail } from '../utils/utils'
import { Config, NullableStringOrRegexpArray, URLClean } from '../config' import { Config, URLClean } from '../config'
import { supportsAvif, supportsWebP } from '../utils/caniuse' import { supportsAvif, supportsWebP } from '../utils/caniuse'
import { ForbiddenError } from '../utils/errors' import { ForbiddenError } from '../utils/errors'

View File

@ -8,6 +8,15 @@ import { init as initHooks } from './fastify/hooks'
export const App = fastify({ logger: { prettyPrint: true } }) export const App = fastify({ logger: { prettyPrint: true } })
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()
})
async function main() {
try {
// Internal // Internal
initConfig(App) initConfig(App)
initStorage() initStorage()
@ -17,19 +26,11 @@ initMiddleware(App)
initHooks(App) initHooks(App)
initRoutes(App) initRoutes(App)
process.on('SIGINT', async function () { // Start
App.log.info('Stopping server')
// Close with 2s timeout
await Promise.race([App.close(), new Promise((resolve) => setTimeout(resolve, 2000))])
process.exit()
})
async function start() {
try {
await App.listen(Config.port, Config.address) await App.listen(Config.port, Config.address)
} catch (err) { } catch (err) {
App.log.error(err) App.log.error(err)
process.exit(1) process.exit(1)
} }
} }
start() main()

View File

@ -1,20 +1,22 @@
import { Config, StorageType } from '../config' import { Config, StorageType } from '../config'
import { Local } from './local' import { Local } from './local'
export interface Storage { export abstract class Storage {
read(path: string): Promise<Buffer> abstract read(path: string): Promise<Buffer>
write(path: string, data: Buffer): Promise<void> abstract write(path: string, data: Buffer): Promise<void>
exists(path: string): Promise<boolean> abstract exists(path: string): Promise<boolean>
delete(path: string): Promise<void> abstract delete(path: string): Promise<void>
readStream(path: string): Promise<NodeJS.ReadableStream> abstract readStream(path: string): Promise<NodeJS.ReadableStream>
writeStream(path: string): Promise<NodeJS.WritableStream> abstract writeStream(path: string): Promise<NodeJS.WritableStream>
// list(path: string): Promise<string[]> // list(path: string): Promise<string[]>
abstract init(): Promise<void>
} }
export let storage: Storage export let storage: Storage
export function init() { export async function init() {
if (!storage) { if (!storage) {
switch (Config.storage) { switch (Config.storage) {
case StorageType.Local: case StorageType.Local:
@ -23,5 +25,6 @@ export function init() {
default: default:
throw new Error(`Unknown storage type: ${Config.storage}`) throw new Error(`Unknown storage type: ${Config.storage}`)
} }
await storage.init()
} }
} }

View File

@ -1,5 +1,6 @@
import { resolve, join } from 'path' import { resolve, join } from 'path'
import fs from 'fs' import fs from 'fs'
import { promisify } from 'util'
import { Storage } from './' import { Storage } from './'
@ -8,6 +9,10 @@ export class Local implements Storage {
this.root = resolve(root) this.root = resolve(root)
} }
async init() {
await promisify(fs.mkdir)(this.root, { recursive: true })
}
read(path: string): Promise<Buffer> { read(path: string): Promise<Buffer> {
const file = join(this.root, path) const file = join(this.root, path)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {