From 766c915de1f0efb55daa9d0efe11b02ef119cca8 Mon Sep 17 00:00:00 2001 From: "Jonny_Bro (Nikita)" Date: Thu, 29 Aug 2024 10:38:51 +0500 Subject: [PATCH] Dockerize --- .dockerignore | 32 ++++++++++++++++++ .gitignore | 1 - Dockerfile | 82 +++++++++++++++++++++++++++++++++++++++++++++++ compose.yaml | 30 +++++++++++++++++ config.example.js | 18 ----------- config.js | 20 ++++++++++++ index.js | 2 +- package.json | 1 + pnpm-lock.yaml | 12 ++++++- 9 files changed, 177 insertions(+), 21 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 compose.yaml delete mode 100644 config.example.js create mode 100644 config.js diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..73a25a7 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,32 @@ +# For more help, visit the .dockerignore file reference guide [here](https://docs.docker.com/go/build-context-dockerignore/) + +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.next +**/.cache +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/charts +**/docker-compose* +**/compose.y*ml +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +**/build +**/dist +public/css/*.css +public/courses/*.txt +LICENSE +README.md diff --git a/.gitignore b/.gitignore index 9a10608..6f017a6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ public/css/style.css public/courses/* data/* -config.js !data/main_db.example.json # Logs diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1860351 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,82 @@ +# syntax=docker/dockerfile:1 + +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Dockerfile reference guide at +# https://docs.docker.com/go/dockerfile-reference/ + +# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7 + +ARG NODE_VERSION=18.17.1 +ARG PNPM_VERSION=9.7.1 + +################################################################################ +# Use node image for base image for all stages. +FROM node:${NODE_VERSION}-alpine as base + +# Set working directory for all build stages. +WORKDIR /usr/src/app + +# Install pnpm. +RUN --mount=type=cache,target=/root/.npm \ + npm install -g pnpm@${PNPM_VERSION} + +################################################################################ +# Create a stage for installing production dependecies. +FROM base as deps + +# Download dependencies as a separate step to take advantage of Docker's caching. +# Leverage a cache mount to /root/.local/share/pnpm/store to speed up subsequent builds. +# Leverage bind mounts to package.json and pnpm-lock.yaml to avoid having to copy them +# into this layer. +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \ + --mount=type=cache,target=/root/.local/share/pnpm/store \ + pnpm install --prod --frozen-lockfile + +################################################################################ +# Create a stage for building the application. +FROM deps as build + +# Download additional development dependencies before building, as some projects require +# "devDependencies" to be installed to build. If you don't need this, remove this step. +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \ + --mount=type=cache,target=/root/.local/share/pnpm/store \ + pnpm install --frozen-lockfile + +# Copy the rest of the source files into the image. +COPY . . + +# Run the build script. +RUN pnpm run build + +# This project specific +RUN mv data/main_db.example.json data/main_db.json +RUN chmod -R 777 data/ +RUN chmod -R 777 public/ +# End + +################################################################################ +# Create a new stage to run the application with minimal runtime dependencies +# where the necessary files are copied from the build stage. +FROM base as final + +# Use production node environment by default. +ENV NODE_ENV production + +# Run the application as a non-root user. +USER node + +# Copy package.json so that package manager commands can be used. +COPY package.json . + +# Copy the production dependencies from the deps stage and also +# the built application from the build stage into the image. +COPY --from=deps /usr/src/app/node_modules ./node_modules +COPY --from=build /usr/src/app/. ./. + +# Expose the port that the application listens on. +EXPOSE 6547 + +# Run the application. +CMD pnpm start diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..6e97f06 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,30 @@ +services: + server: + build: + context: . + environment: + # Set true for production database + PROD: true + # Your domain without / at the end + DOMAIN: http://localhost:6547 + # Port for the server + PORT: 6547 + # How often can user send request to API + RATELIMIT: 5000 # 5 seconds + # How often can user change IP address + IPCHANGETIME: 10800000 # 3 hours + # Your SteamAPI key + STEAMKEY: NO_KEY + # Secret for a cookie + COOKIE: NO_COOKIE + # Discord webhook url or leave empty + WEBHOOK: "" + volumes: + - courses:/usr/src/app/public/courses + - data:/usr/src/app/data + ports: + - 6547:6547 + +volumes: + courses: + data: \ No newline at end of file diff --git a/config.example.js b/config.example.js deleted file mode 100644 index bfc28df..0000000 --- a/config.example.js +++ /dev/null @@ -1,18 +0,0 @@ -module.exports = { - /* Set true for production database */ - production: true, - /* Your domain without / at the end */ - domain: "http://localhost", - /* Port for the server */ - port: 6547, - /* How often can user send request to API */ - rateLimitTime: 1000 * 5, // 5 seconds - /* How often can user change IP address */ - ipChangeTime: 1000 * 60 * 60 * 3, // 3 hours - /* Your SteamAPI key */ - steamKey: "", - /* Secret for a cookie */ - cookieSecret: "", - /* Discord webhook url or leave empty */ - webhook_url: "", -}; \ No newline at end of file diff --git a/config.js b/config.js new file mode 100644 index 0000000..ebc36bc --- /dev/null +++ b/config.js @@ -0,0 +1,20 @@ +require("dotenv").config(); + +module.exports = { + /* Set true for production database */ + production: process.env.PROD ? true : false || true, + /* Your domain without / at the end */ + domain: process.env.DOMAIN || "http://localhost:6547", + /* Port for the server */ + port: process.env.PORT || 6547, + /* How often can user send request to API */ + rateLimitTime: process.env.RATELIMIT || 1000 * 5, // 5 seconds + /* How often can user change IP address */ + ipChangeTime: process.env.IPCHANGETIME || 1000 * 60 * 60 * 3, // 3 hours + /* Your SteamAPI key */ + steamKey: process.env.STEAMKEY || "", + /* Secret for a cookie */ + cookieSecret: process.env.COOKIE || "", + /* Discord webhook url or leave empty */ + webhook_url: process.env.WEBHOOK || "", +}; \ No newline at end of file diff --git a/index.js b/index.js index 19c2865..97e7150 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ const { JsonDB, Config } = require("node-json-db"), const config = require("./config"); const db = new JsonDB(new Config(`data/${config.production ? "main" : "test"}_db`, true, true, "/")); -if (!config.cookieSecret || !config.steamKey) return console.log("Please check that you have filled steamKey and cookieSecret in config file"); +if (!config.cookieSecret || !config.steamKey || config.steamKey === "NO_KEY" || config.cookieSecret === "NO_COOKIE") return console.log("Please check that you have filled steamKey and cookieSecret in config file"); if (!fs.existsSync("public/courses/")) fs.mkdirSync("public/courses/"); // Express App diff --git a/package.json b/package.json index 2d749f1..049e675 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ }, "dependencies": { "daisyui": "^4.11.1", + "dotenv": "^16.4.5", "ejs": "^2.6.1", "express": "^4.16.1", "express-session": "^1.18.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5bd856c..1be0dbf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ importers: daisyui: specifier: ^4.11.1 version: 4.11.1(postcss@8.4.36) + dotenv: + specifier: ^16.4.5 + version: 16.4.5 ejs: specifier: ^2.6.1 version: 2.6.2 @@ -395,6 +398,10 @@ packages: domutils@3.1.0: resolution: {integrity: sha512-H78uMmQtI2AhgDJjWeQmHwJJ2bLPD3GMmO7Zja/ZZh84wkm+4ut+IUnUdRa8uCGX88DiVx1j6FRe1XfxEgjEZA==} + dotenv@16.4.5: + resolution: {integrity: sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==} + engines: {node: '>=12'} + eastasianwidth@0.2.0: resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==} @@ -1579,6 +1586,8 @@ snapshots: domelementtype: 2.3.0 domhandler: 5.0.3 + dotenv@16.4.5: {} + eastasianwidth@0.2.0: {} ee-first@1.1.1: {} @@ -2158,8 +2167,9 @@ snapshots: postcss-load-config@4.0.2(postcss@8.4.36): dependencies: lilconfig: 3.1.1 - postcss: 8.4.36 yaml: 2.4.1 + optionalDependencies: + postcss: 8.4.36 postcss-nested@6.0.1(postcss@8.4.36): dependencies: