From 752b96afdf2fa285492deaf3a676d2b2c2e47c9f Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Wed, 30 Jun 2021 13:24:28 +0300 Subject: Dockerize --- backend/Dockerfile | 27 +++++++++++++++++++++++++++ backend/config.ts | 7 +++++++ backend/index.ts | 6 +++--- backend/shared-types.ts | 3 +++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 backend/Dockerfile create mode 100644 backend/shared-types.ts (limited to 'backend') diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..1301b25 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,27 @@ +# Install dependencies only when needed +FROM node:alpine AS deps +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat +WORKDIR /app +COPY package.json package-lock.json ./ +RUN npm ci --only-prod + +# Rebuild the source code only when needed +FROM node:alpine AS runner +WORKDIR /app + +ENV NODE_ENV production +ENV PORT 4000 + +RUN addgroup -g 1001 -S nodejs +RUN adduser -S server -u 1001 + +COPY --from=deps /app/package.json ./package.json +COPY --from=deps /app/node_modules ./node_modules + +USER server + +EXPOSE 4000 + +COPY *.ts *.json ./ +CMD ["npm", "start] diff --git a/backend/config.ts b/backend/config.ts index 2585736..ae2462e 100644 --- a/backend/config.ts +++ b/backend/config.ts @@ -3,4 +3,11 @@ if (!process.env.POSTGRES_CONNECTION_STRING) { } export const postgresConnectionString = process.env.POSTGRES_CONNECTION_STRING; + +if (!process.env.FRONTEND_URL) { + throw new Error("FRONTEND_URL env var is not defined!"); +} + +export const frontendUrl = process.env.FRONTEND_URL; + export const port = process.env.PORT || 4000; diff --git a/backend/index.ts b/backend/index.ts index 5a974d6..9efec62 100644 --- a/backend/index.ts +++ b/backend/index.ts @@ -8,15 +8,15 @@ import { sql, } from "slonik"; import * as config from "./config"; -import { ValueRecord } from "../shared-types"; -import { Server as SocketIOServer, Socket } from "socket.io"; +import { ValueRecord } from "./shared-types"; +import { Server as SocketIOServer } from "socket.io"; const app = express(); const httpServer = http.createServer(app); const io = new SocketIOServer(httpServer, { cors: { - origin: "*", + origin: config.frontendUrl, }, }); diff --git a/backend/shared-types.ts b/backend/shared-types.ts new file mode 100644 index 0000000..51f3281 --- /dev/null +++ b/backend/shared-types.ts @@ -0,0 +1,3 @@ +export interface ValueRecord { + value: number; +} -- cgit v1.3