Skip to content

add docker & api route #122

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 2 commits into from
Closed

add docker & api route #122

wants to merge 2 commits into from

Conversation

wvl94
Copy link
Contributor

@wvl94 wvl94 commented Nov 14, 2024

💡 PR Summary generated by FirstMate

Overview: Added Docker support and a new API route for fetching examples by ID.

Changes:
Dockerfile:

  • Created a Dockerfile using Node.js 19-alpine for containerization.
  • Set working directory, added user/group, and installed git.

API Route Update:

  • Added a new GET route /get/:id in exampleRouter.js for fetching example data.

Configuration Change:

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

TLDR: Introduced Docker support and a new API route for fetching examples; updated server port configuration. Focus on the Dockerfile and new route in exampleRouter.js.

Generated by FirstMate and automatically updated on every commit.

Copy link

firstmate bot commented Nov 14, 2024

PR Review

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

    ❌ Documentation drift: Update documentation for default server port change from 8080 to 80.
    ❌ Dockerfile optimization: Implement multi-stage build in Dockerfile for optimized image size and security.
    ❌ Security issues: Add grantAccessByPermissionMiddleware to /get/:id route for access control.

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

Comment on lines +4 to 5
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

You changed the default server port from 8080 to 80. This change seems important, so it might be smart to change the documentation accordingly.

For example, update the documentation to reflect the new default port:

## Running the Application

To start the application, use the following command:

```bash
npm start

The application will now run on port 80 by default.

Comment on lines +1 to +10
FROM node:19-alpine

WORKDIR /usr/src/app

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

RUN apk add --no-cache git
COPY . .
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

Your Dockerfile should implement a multi-stage build to optimize image size and enhance security. Consider adding a build stage and a final stage like this:

# Build stage
FROM node:19-alpine AS build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .

# Final stage
FROM node:19-alpine
WORKDIR /usr/src/app
COPY --from=build /usr/src/app .
CMD ["npm", "start", "--no-update-notifier"]

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

Comment on lines +12 to +13
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

The new route /get/:id lacks the grantAccessByPermissionMiddleware, which is essential for proper permission checks. Update the route as follows to include the middleware:

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

This ensures that the necessary access control is enforced.

(Based on guideline 'Routes must check permissions')

Copy link

firstmate bot commented Nov 14, 2024

alt text

I analysed the failed pipeline job Validate docker:

Failure Analysis

The pipeline crashed during the Docker build step, specifically when attempting to install the git package using the command apk add --no-cache git. The error message indicates a permission issue:

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

This suggests that the Dockerfile is trying to run this command as a non-root user (appuser), which does not have the necessary permissions to install packages. The relevant section of the Dockerfile is:

USER appuser
RUN apk add --no-cache git

Suggested Fix

To resolve the permission issue, you can switch back to the root user before running the apk add command and then switch back to appuser afterward. Modify the Dockerfile as follows:

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

This change ensures that the package installation has the necessary permissions while maintaining the intended user context for subsequent commands.

@wvl94 wvl94 closed this Nov 14, 2024
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