Quellcode durchsuchen

refactor: Added support for prod and TypeScript dev backend mode

Owen Diffey vor 1 Jahr
Ursprung
Commit
5e60329f2c
9 geänderte Dateien mit 40 neuen und 11 gelöschten Zeilen
  1. 1 0
      .env.example
  2. 1 0
      .wiki/Configuration.md
  3. 7 3
      backend/Dockerfile
  4. 14 0
      backend/entrypoint.sh
  5. 6 0
      backend/nodemon.json
  6. 6 6
      backend/package.json
  7. 1 1
      backend/tsconfig.json
  8. 3 0
      docker-compose.yml
  9. 1 1
      musare.sh

+ 1 - 0
.env.example

@@ -5,6 +5,7 @@ DOCKER_COMMAND=docker
 
 BACKEND_HOST=127.0.0.1
 BACKEND_PORT=8080
+BACKEND_MODE=prod
 
 FRONTEND_HOST=127.0.0.1
 FRONTEND_PORT=80

+ 1 - 0
.wiki/Configuration.md

@@ -119,6 +119,7 @@ application within the container is listening on `21017`.
 | `DOCKER_COMMAND` | Should be either `docker` or `podman`.  |
 | `BACKEND_HOST` | Backend container host. |
 | `BACKEND_PORT` | Backend container port. |
+| `BACKEND_MODE` | Should be either `prod` or `dev`. |
 | `FRONTEND_HOST` | Frontend container host. |
 | `FRONTEND_PORT` | Frontend container port. |
 | `FRONTEND_MODE` | Should be either `prod` or `dev`. |

+ 7 - 3
backend/Dockerfile

@@ -10,8 +10,8 @@ RUN npm install --silent
 
 FROM node:18 AS musare_backend
 
-ARG CONTAINER_MODE=prod
-ENV CONTAINER_MODE=${CONTAINER_MODE}
+ARG BACKEND_MODE=prod
+ENV BACKEND_MODE=${BACKEND_MODE}
 
 RUN mkdir -p /opt/app /opt/types
 WORKDIR /opt/app
@@ -19,7 +19,11 @@ WORKDIR /opt/app
 COPY . /opt/app
 COPY --from=backend_node_modules /opt/app/node_modules node_modules
 
-ENTRYPOINT bash -c '([[ "${CONTAINER_MODE}" == "dev" ]] && npm install --silent); npm run docker:dev'
+RUN bash -c '([[ "${BACKEND_MODE}" == "dev" ]] && exit 0) || npm run build'
+
+RUN chmod u+x entrypoint.sh
+
+ENTRYPOINT bash /opt/app/entrypoint.sh
 
 EXPOSE 8080/tcp
 EXPOSE 8080/udp

+ 14 - 0
backend/entrypoint.sh

@@ -0,0 +1,14 @@
+#!/bin/bash
+
+if [[ "${CONTAINER_MODE}" == "dev" ]]; then
+    npm install --silent
+fi
+
+if [[ "${BACKEND_MODE}" == "prod" ]]; then
+    if [[ "${CONTAINER_MODE}" == "dev" ]]; then
+        npm run build
+    fi
+    npm run prod
+elif [ "${BACKEND_MODE}" == "dev" ]; then
+    npm run dev -- --legacy-watch --no-stdin
+fi

+ 6 - 0
backend/nodemon.json

@@ -0,0 +1,6 @@
+{
+  "watch": ["src", "config"],
+  "ext": "ts,json",
+  "ignore": ["src/**/*.spec.ts", "node_modules"],
+  "exec": "ts-node ./src/main.ts"
+}

+ 6 - 6
backend/package.json

@@ -2,18 +2,18 @@
   "name": "musare-backend",
   "private": true,
   "version": "3.9.0-dev",
-  "type": "module",
+  "type": "commonjs",
   "description": "An open-source collaborative music listening and catalogue curation application. Currently supporting YouTube based content.",
   "main": "index.js",
   "author": "Musare Team",
   "license": "GPL-3.0",
   "repository": "https://github.com/Musare/Musare",
   "scripts": {
-    "dev": "nodemon --es-module-specifier-resolution=node",
-    "docker:dev": "nodemon --es-module-specifier-resolution=node --legacy-watch --no-stdin /opt/app",
-    "docker:prod": "node --es-module-specifier-resolution=node /opt/app",
-    "lint": "eslint --cache logic",
-    "typescript": "tsc --noEmit --skipLibCheck"
+    "dev": "nodemon",
+    "build": "tsc",
+    "prod": "node build/src/main.js",
+    "lint": "eslint --cache logic src",
+    "typescript": "tsc --noEmit"
   },
   "dependencies": {
     "async": "^3.2.4",

+ 1 - 1
backend/tsconfig.json

@@ -28,7 +28,7 @@
     "module": "commonjs",                                /* Specify what module code is generated. */
     // "rootDir": "./",                                  /* Specify the root folder within your source files. */
     // "moduleResolution": "node",                       /* Specify how TypeScript looks up a file from a given module specifier. */
-    // "baseUrl": "./",                                  /* Specify the base directory to resolve non-relative module names. */
+    "baseUrl": "./",                                  /* Specify the base directory to resolve non-relative module names. */
     // "paths": {},                                      /* Specify a set of entries that re-map imports to additional lookup locations. */
     // "rootDirs": [],                                   /* Allow multiple folders to be treated as one when resolving modules. */
     // "typeRoots": [],                                  /* Specify multiple folders that act like './node_modules/@types'. */

+ 3 - 0
docker-compose.yml

@@ -5,12 +5,15 @@ services:
     build:
       context: ./backend
       target: musare_backend
+      args:
+        BACKEND_MODE: "${BACKEND_MODE:-prod}"
     restart: ${RESTART_POLICY:-unless-stopped}
     volumes:
       - ./.git:/opt/app/.parent_git:ro
       - ./backend/config:/opt/app/config
       - ./types:/opt/types
     environment:
+      - BACKEND_MODE=${BACKEND_MODE:-prod}
       - CONTAINER_MODE=${CONTAINER_MODE:-prod}
     links:
       - mongo

+ 1 - 1
musare.sh

@@ -306,7 +306,7 @@ case $1 in
             fi
             if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *backend* ]]; then
                 echo -e "${CYAN}Running backend lint...${NC}"
-                ${dockerCompose} exec -T backend npx eslint $cache logic $fix
+                ${dockerCompose} exec -T backend npx eslint $cache logic src $fix
                 backendExitValue=$?
             fi
             if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *docs* ]]; then