Skip to content

add docker & api route #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

add docker & api route #119

wants to merge 1 commit into from

Conversation

wvl94
Copy link
Contributor

@wvl94 wvl94 commented Nov 13, 2024

💡 PR Summary generated by FirstMate

Overview: Added Docker support and a new API route for enhanced functionality.

Changes:
Dockerfile creation:

  • Added a Dockerfile to containerize the application using Node.js 19-alpine.
  • Configured environment variables and user permissions for better security.

API route updates:

  • Introduced a new route /get/:id in exampleRouter.js for fetching data by ID.
  • Maintained existing route functionality with permission middleware.

Configuration adjustment:

  • Changed default server port from 8080 to 80 in appConfig.js for consistency.

TLDR: This PR adds Docker support, a new API route for data retrieval, and adjusts the server port configuration. Review the Dockerfile and new route in exampleRouter.js.

Generated by FirstMate and automatically updated on every commit.

Copy link

firstmate bot commented Nov 13, 2024

PR Review

⚠️ It seems that you can still improve the quality of your PR:

    ❌ Documentation drift: Update documentation for port changes in Dockerfile and appConfig.js.
    ❌ Configuration issues: Avoid hardcoding server port in appConfig.js; use environment variables.
    ❌ Docker best practices: Implement multi-stage builds in Dockerfile for smaller image size.
    ❌ Security compliance: Ensure routes check permissions using grantAccessByPermissionMiddleware.

Generated by Firstmate to make sure you can focus on coding new features.

Comment on lines +2 to +12
ENV PORT 8080

WORKDIR /usr/src/app

RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

RUN apk add --no-cache git
COPY . .
EXPOSE 8080
CMD ["npm", "start", "--no-update-notifier"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alt text

You changed the application port to 8080 in the Dockerfile. This change seems important, so it might be smart to change the documentation accordingly. Additionally, the default port in appConfig.js was changed to 80. Ensure the documentation reflects the correct ports for running the application.

Example documentation update:

## Running the Application

To start the application, use the following command:

```bash
docker run -p 8080:8080 your-image-name

The application will be accessible at http://localhost:8080.

Configuration

The application runs on port 80 by default unless specified otherwise.

Comment on lines 3 to 5
serverPort() {
return process.env.PORT || 8080;
return process.env.PORT || 80;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alt text

Avoid hardcoding the server port in appConfig.js. Instead, use environment variables to manage this setting for better flexibility. You can update the method like this:

-         return process.env.PORT || 8080;
+         return process.env.PORT || process.env.DEFAULT_PORT || 80;

(Based on guideline 'No hardcoded configuration or secrets')

Comment on lines +1 to +12
FROM node:19-alpine
ENV PORT 8080

WORKDIR /usr/src/app

RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

RUN apk add --no-cache git
COPY . .
EXPOSE 8080
CMD ["npm", "start", "--no-update-notifier"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alt text

Consider using multi-stage builds in your Dockerfile to reduce the final image size and improve security. You can create a builder stage to compile your application and then copy the necessary artifacts to a smaller base image. Here's a simplified example:

FROM node:19-alpine AS builder
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
FROM node:19-alpine
WORKDIR /usr/src/app
COPY --from=builder /usr/src/app .
EXPOSE 8080
CMD ["npm", "start", "--no-update-notifier"]

(Based on guideline 'Dockerfiles should use multi-stage builds')

Comment on lines +12 to +14
router.route("/get/:id").get(exampleController.getById)


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alt text

Ensure that all routes check permissions using the grantAccessByPermissionMiddleware function for security and access control. Modify the route as follows:

- router.route("/get/:id").get(exampleController.getById)
+ router.route("/get/:id").get(grantAccessByPermissionMiddleware([API_PERMISSIONS.PUBLIC_ENDPOINT]), exampleController.getById)

This will help maintain proper access control across your routes.

(Based on guideline 'Routes should check permissions')

Copy link

firstmate bot commented Nov 13, 2024

alt text

I analysed the failed pipeline job Validate docker:

Failure Analysis

The pipeline crashed during the Docker build step, specifically when trying to install git using the command apk add --no-cache git. The logs indicate a permission issue:

ERROR: Unable to lock database: Permission denied
ERROR: Failed to open apk database: Permission denied

This failure is likely due to the fact that the Dockerfile switches to a non-root user (appuser) before attempting to install packages. Non-root users typically do not have permission to modify the package manager's database.

Suggested Fix

To resolve this issue, you should install git while still running as the root user. You can do this by rearranging the USER directive in your Dockerfile. Here’s a small change you can make:

-    USER appuser
+    # Switch to appuser after installing necessary packages
+    RUN apk add --no-cache git
+    USER appuser

This change ensures that the apk add --no-cache git command runs with the necessary permissions, preventing the permission denied error.

@wvl94 wvl94 closed this Nov 13, 2024
Copy link

firstmate bot commented Nov 13, 2024

alt text

I analysed the failed pipeline job Validate docker:

Failure Analysis

The pipeline crashed during the Docker build step, specifically when trying to install git using the command apk add --no-cache git. The logs indicate a permission issue:

ERROR: Unable to lock database: Permission denied
ERROR: Failed to open apk database: Permission denied

This failure is likely due to the fact that the Dockerfile switches to a non-root user (appuser) before attempting to install packages. Non-root users typically do not have permission to modify the package database.

Suggested Fix

To resolve this issue, you can modify the Dockerfile to install git while still running as the root user. Here’s a small change to make:

- USER appuser
+ RUN apk add --no-cache git
+ USER appuser

This way, the apk add command runs with the necessary permissions, and you can switch back to the non-root user afterward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant