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 ADDRESS=0.0.0.0
EXPOSE 80
CMD [ "node", "dist/src" ]
CMD [ "node", "." ]

View File

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

View File

@ -17,7 +17,7 @@ import ms from 'ms'
import { storage } from '../storage'
import { transform } from '../transform'
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 { ForbiddenError } from '../utils/errors'

View File

@ -8,15 +8,6 @@ import { init as initHooks } from './fastify/hooks'
export const App = fastify({ logger: { prettyPrint: true } })
// Internal
initConfig(App)
initStorage()
// Fastify
initMiddleware(App)
initHooks(App)
initRoutes(App)
process.on('SIGINT', async function () {
App.log.info('Stopping server')
// Close with 2s timeout
@ -24,12 +15,22 @@ process.on('SIGINT', async function () {
process.exit()
})
async function start() {
async function main() {
try {
// Internal
initConfig(App)
initStorage()
// Fastify
initMiddleware(App)
initHooks(App)
initRoutes(App)
// Start
await App.listen(Config.port, Config.address)
} catch (err) {
App.log.error(err)
process.exit(1)
}
}
start()
main()

View File

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

View File

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