Skip to content

Commit

Permalink
move folder-checks to entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfagun74 committed Apr 2, 2024
1 parent 480a829 commit 4f55ac5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 53 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# GameVault Backend Server Changelog

## 11.0.2

Recommended Gamevault App Version: `v1.8.2.0` or `v1.9.0.0`

### Changes

- Moved folder-checks to entrypoint to avoid permissions issues.

## 11.0.1

Recommended Gamevault App Version: `v1.8.2.0` or `v1.9.0.0`
Expand Down
31 changes: 29 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
#!/bin/sh
set -e

# If running as root, it means the --user directive for Docker CLI/Compose was not used
# Use then the PUID env
# Function to create a directory if it does not exist and set the proper ownership
createDirectoryIfNotExist() {
if [ ! -d "$1" ]; then
echo "Directory \"$1\" does not exist. Trying to create a new one..."
sudo mkdir -p "$1"
sudo chown "$PUID":"$PGID" "$1"
fi
}

# Check and create necessary folders if they do not exist
check_folders() {
if [ "$MOCK_FILES" = "true" ]; then
echo "Not checking or creating any folders because MOCK_FILES is set to true"
return
fi

createDirectoryIfNotExist "$FILES_VOLUME"
createDirectoryIfNotExist "$IMAGES_VOLUME"
createDirectoryIfNotExist "$LOGS_VOLUME"
createDirectoryIfNotExist "$SQLITEDB_VOLUME"
}

# Perform directory checks
check_folders

# Existing logic for user permissions and running the node process
if [ "$(id -u)" = '0' ]; then
groupmod -o -g "$PGID" node
usermod -o -u "$PUID" node
# Ensure that all specified directories are owned by the node user
sudo chown -R "$PUID":"$PGID" "$FILES_VOLUME" "$IMAGES_VOLUME" "$LOGS_VOLUME" "$SQLITEDB_VOLUME"
# Execute the command as the node user
sudo -u "#$PUID" -g "#$PGID" -E node "${@}"
else # if using the user directive, run normally
exec node "${@}"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gamevault-backend",
"version": "11.0.1",
"version": "11.0.2",
"description": "the self-hosted gaming platform for drm-free games",
"author": "Alkan Alper, Schäfer Philip GbR / Phalcode",
"private": true,
Expand Down
51 changes: 1 addition & 50 deletions src/modules/files/files.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { watch } from "chokidar";
import { debounce } from "lodash";
import { Readable } from "stream";
import { Throttle } from "stream-throttle";
import { mkdir, readdir, stat } from "fs/promises";
import { readdir, stat } from "fs/promises";
import { Cron } from "@nestjs/schedule";

@Injectable()
Expand All @@ -41,7 +41,6 @@ export class FilesService implements OnApplicationBootstrap {
) {}

onApplicationBootstrap() {
this.checkFolders();
this.index("Initial indexing on application start").catch((error) => {
this.logger.error(error, "Error in initial file indexing");
});
Expand Down Expand Up @@ -550,52 +549,4 @@ export class FilesService implements OnApplicationBootstrap {
type,
});
}

/** Checks and creates necessary folders if they do not exist. */
private checkFolders() {
if (configuration.TESTING.MOCK_FILES) {
this.logger.warn(
"Not checking or creating any folders because TESTING_MOCK_FILES is set to true",
);
return;
}

this.createDirectoryIfNotExist(
configuration.VOLUMES.FILES,
`Directory "${configuration.VOLUMES.FILES}" does not exist. Trying to create a new one...`,
);

this.createDirectoryIfNotExist(
configuration.VOLUMES.IMAGES,
`Directory "${configuration.VOLUMES.IMAGES}" does not exist. Trying to create a new one...`,
);

if (configuration.SERVER.LOG_FILES_ENABLED) {
this.createDirectoryIfNotExist(
configuration.VOLUMES.LOGS,
`Directory "${configuration.VOLUMES.LOGS}" does not exist. Trying to create a new one...`,
);
}

if (
configuration.DB.SYSTEM === "SQLITE" &&
!configuration.TESTING.IN_MEMORY_DB
) {
this.createDirectoryIfNotExist(
configuration.VOLUMES.SQLITEDB,
`Directory "${configuration.VOLUMES.SQLITEDB}" does not exist. Trying to create a new one...`,
);
}
}

/** Creates a directory if it does not exist. */
private async createDirectoryIfNotExist(
path: string,
errorMessage: string,
): Promise<void> {
if (!existsSync(path)) {
this.logger.error(errorMessage);
await mkdir(path);
}
}
}

0 comments on commit 4f55ac5

Please sign in to comment.