From ef12e17ac7e90b3ea84c91d47c27c7665eed89a3 Mon Sep 17 00:00:00 2001 From: Denis Anikin Date: Tue, 17 Oct 2023 04:29:23 +0300 Subject: [PATCH] Not so fancy multistage ts build --- back/.dockerignore | 2 + back/Dockerfile | 9 +++- back/package.json | 7 +-- back/src/_tmp/otherViews.ts.tmp | 46 +++++++++++++++++++ .../socialMediaViews.ts.tmp} | 0 back/src/otherViews.ts | 28 ----------- 6 files changed, 59 insertions(+), 33 deletions(-) create mode 100644 back/src/_tmp/otherViews.ts.tmp rename back/src/{socialMediaView.ts.tmp => _tmp/socialMediaViews.ts.tmp} (100%) diff --git a/back/.dockerignore b/back/.dockerignore index 22e2329..677fc96 100644 --- a/back/.dockerignore +++ b/back/.dockerignore @@ -1,4 +1,6 @@ node_modules/ +build/ +src/_tmp .env .DS_Store *.lmdb* diff --git a/back/Dockerfile b/back/Dockerfile index e62e671..6bce352 100644 --- a/back/Dockerfile +++ b/back/Dockerfile @@ -1,10 +1,15 @@ -FROM node:20.8.0-alpine - +FROM node:20.8.0-alpine as builder # possibly for future usage # RUN apt-get update -y # RUN apt-get install -y build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev WORKDIR /srv/www COPY . . RUN npm i +RUN npm run build-prod + +FROM node:20.8.0-alpine +WORKDIR /srv/www +COPY --chown=node:node --from=builder /srv/www/build/ ./ +RUN npm i --omit=dev USER node CMD ["npm", "start"] diff --git a/back/package.json b/back/package.json index b8573de..e85a922 100644 --- a/back/package.json +++ b/back/package.json @@ -6,9 +6,10 @@ "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "build": "tsc -p tsconfig.json && cp *.ttf build/", - "build-prod": "npm run build && cp package.json build/", - "start": "node build/index.js", + "build": "tsc -p tsconfig.json", + "build-prod": "npm run build && cp package*.json build/", + "start-dev": "node build/index.js", + "start": "node index.js", "dev": "npm run build && DEBUG=1 npm run start" }, "author": "", diff --git a/back/src/_tmp/otherViews.ts.tmp b/back/src/_tmp/otherViews.ts.tmp new file mode 100644 index 0000000..93ea34f --- /dev/null +++ b/back/src/_tmp/otherViews.ts.tmp @@ -0,0 +1,46 @@ +import { FastifyReply, FastifyRequest } from "fastify"; +// import { open as openLmdb } from "lmdb"; +// import * as config from "./config.js"; + +type RedirectRequest = FastifyRequest<{ + Querystring: { where: string }; +}>; +// type StoreRequest = FastifyRequest<{ +// Querystring: { fullpath: string }; +// }>; + +// const lmdbHandler = openLmdb(config.READS_STORE_PATH, {}); + +// export async function storeReadCount( +// request: StoreRequest, +// serverReply: FastifyReply +// ) { +// if ( +// !request.query.fullpath || +// !request.query.fullpath.startsWith("/") || +// request.query.fullpath.length < config.MIN_URL_FOR_READS_LENGTH +// ) { +// await serverReply.code(400).send("Not provided proper fullpath"); +// } +// const preparedPath = request.query.fullpath +// .substring(0, config.MAX_URL_FOR_READS_LENGTH) +// .trim(); +// const previousCountRaw = parseInt(await lmdbHandler.get(preparedPath), 10); +// const newCountValue = (isNaN(previousCountRaw) ? 0 : previousCountRaw) + 1; +// await lmdbHandler.put(preparedPath, newCountValue); +// serverReply.code(200).send({ count: newCountValue }); +// await serverReply; +// } + +export async function redirectSomewhere( + request: RedirectRequest, + serverReply: FastifyReply +) { + const preparedWhere = request.query.where.trim(); + if (preparedWhere) { + serverReply.code(301).redirect(preparedWhere); + } else { + serverReply.code(400).send("Not provided place where to redirect"); + } + await serverReply; +} diff --git a/back/src/socialMediaView.ts.tmp b/back/src/_tmp/socialMediaViews.ts.tmp similarity index 100% rename from back/src/socialMediaView.ts.tmp rename to back/src/_tmp/socialMediaViews.ts.tmp diff --git a/back/src/otherViews.ts b/back/src/otherViews.ts index 93ea34f..b2c247b 100644 --- a/back/src/otherViews.ts +++ b/back/src/otherViews.ts @@ -1,36 +1,8 @@ import { FastifyReply, FastifyRequest } from "fastify"; -// import { open as openLmdb } from "lmdb"; -// import * as config from "./config.js"; type RedirectRequest = FastifyRequest<{ Querystring: { where: string }; }>; -// type StoreRequest = FastifyRequest<{ -// Querystring: { fullpath: string }; -// }>; - -// const lmdbHandler = openLmdb(config.READS_STORE_PATH, {}); - -// export async function storeReadCount( -// request: StoreRequest, -// serverReply: FastifyReply -// ) { -// if ( -// !request.query.fullpath || -// !request.query.fullpath.startsWith("/") || -// request.query.fullpath.length < config.MIN_URL_FOR_READS_LENGTH -// ) { -// await serverReply.code(400).send("Not provided proper fullpath"); -// } -// const preparedPath = request.query.fullpath -// .substring(0, config.MAX_URL_FOR_READS_LENGTH) -// .trim(); -// const previousCountRaw = parseInt(await lmdbHandler.get(preparedPath), 10); -// const newCountValue = (isNaN(previousCountRaw) ? 0 : previousCountRaw) + 1; -// await lmdbHandler.put(preparedPath, newCountValue); -// serverReply.code(200).send({ count: newCountValue }); -// await serverReply; -// } export async function redirectSomewhere( request: RedirectRequest,