From cbc8ab0325c7b79dff4315334c4eef21a5d91df1 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Wed, 17 Nov 2021 16:20:50 +0100 Subject: [PATCH] progress --- Dockerfile | 4 +++- package.json | 1 + src/controllers/image.ts | 2 +- src/index.ts | 23 ++++++++++++----------- src/storage/index.ts | 19 +++++++++++-------- src/storage/local.ts | 5 +++++ 6 files changed, 33 insertions(+), 21 deletions(-) diff --git a/Dockerfile b/Dockerfile index d7e2725..142184e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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", "." ] diff --git a/package.json b/package.json index 11930ae..6999c02 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "", "author": "Niccolo Borgioli", "license": "MIT", + "main": "dist/src", "scripts": { "build": "tsc", "dev": "tsnd src" diff --git a/src/controllers/image.ts b/src/controllers/image.ts index 1d9f40f..662a019 100644 --- a/src/controllers/image.ts +++ b/src/controllers/image.ts @@ -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' diff --git a/src/index.ts b/src/index.ts index 60b8d52..5d7c991 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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() diff --git a/src/storage/index.ts b/src/storage/index.ts index 2aa5264..ff01d64 100644 --- a/src/storage/index.ts +++ b/src/storage/index.ts @@ -1,20 +1,22 @@ import { Config, StorageType } from '../config' import { Local } from './local' -export interface Storage { - read(path: string): Promise - write(path: string, data: Buffer): Promise - exists(path: string): Promise - delete(path: string): Promise +export abstract class Storage { + abstract read(path: string): Promise + abstract write(path: string, data: Buffer): Promise + abstract exists(path: string): Promise + abstract delete(path: string): Promise - readStream(path: string): Promise - writeStream(path: string): Promise + abstract readStream(path: string): Promise + abstract writeStream(path: string): Promise // list(path: string): Promise + + abstract init(): Promise } 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() } } diff --git a/src/storage/local.ts b/src/storage/local.ts index a6a8bb2..1658a6b 100644 --- a/src/storage/local.ts +++ b/src/storage/local.ts @@ -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 { const file = join(this.root, path) return new Promise((resolve, reject) => {