Skip to content
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

fix: Use subrouter for API and correctly handle 404 errors #105

Conversation

adamantike
Copy link
Contributor

@adamantike adamantike commented Jul 8, 2024

What type of PR is this?

  • bug

What this PR does / why we need it:

Currently, the implementation for API v1 routes has the main drawback that any unknown path gets the fallback notFoundHandler, trying to access filesystem paths.

However, for API routes specifically, we can have a subrouter, and a default NotFound handler that returns 404.

Testing

With this change, requests to api/v1/<unknown> now correctly returns status code 404 instead of 200.

Summary by CodeRabbit

  • New Features

    • Added a new endpoint: /reporting/bill-of-materials.
  • Improvements

    • Restructured API routes under /v1 for better organization.
    • Enhanced endpoint handling for users, groups, actions, locations, labels, items, assets, notifiers, and maintenance.
    • Introduced middleware for certain asset-related endpoints.
    • Added a default NotFound handler for undefined routes.

Currently, the implementation for API v1 routes has the main drawback
that any unknown path gets the fallback `notFoundHandler`, trying to
access filesystem paths.

However, for API routes specifically, we can have a subrouter, and a
default NotFound handler that returns 404.

With this change, requests to `api/v1/<unknown>` now correctly returns
status code 404 instead of 200.
Copy link
Contributor

coderabbitai bot commented Jul 8, 2024

Walkthrough

Walkthrough

The changes involve a significant restructuring of the API's routing logic in the backend/app/api/routes.go file, refining how various endpoints are defined and organized under /v1. The BaseURLFunc has been removed from the controller.go file, reflecting the overall simplification of route handling.

Changes

File Change Summary
backend/app/api/handlers/v1/controller.go Removed the BaseURLFunc function.
backend/app/api/routes.go Restructured routing logic for /v1, updated route definitions, reorganized endpoint handling, added new endpoint /reporting/bill-of-materials, introduced assetMW middleware, added a default NotFound handler.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API Gateway
    participant Route Handler
    participant Controller

    Client->>+API Gateway: Request to /v1/status
    API Gateway->>+Route Handler: Direct to Status Handler
    Route Handler->>Controller: Fetch status data
    Controller-->>Route Handler: Status data
    Route Handler-->>-Client: Status Response

    Client->>+API Gateway: Request to /v1/reporting/bill-of-materials
    API Gateway->>+Route Handler: Direct to Reporting Handler
    Route Handler->>Controller: Fetch Bill of Materials
    Controller-->>Route Handler: Bill of Materials data
    Route Handler-->>-Client: Reporting Response
Loading

Poem

A change refined in code’s deep core,
Base URL no more, routes do soar,
/v1/status stands so tall,
Billing materials, we heed the call,
In routes reformed, the future's store.🚀


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6fcf996 and 0387ba0.

Files selected for processing (2)
  • backend/app/api/handlers/v1/controller.go (1 hunks)
  • backend/app/api/routes.go (2 hunks)
Files skipped from review due to trivial changes (1)
  • backend/app/api/handlers/v1/controller.go
Additional comments not posted (20)
backend/app/api/routes.go (20)

59-64: Initialization of API version 1 routing.

The initialization of API version 1 routing looks correct and sets the foundation for subsequent route definitions.


66-66: Initialization of currency handler.

The initialization of the currency handler is straightforward and follows the pattern established in the previous segment.


68-70: Definition of authentication providers.

The definition of authentication providers is clear and concise, utilizing the NewLocalProvider function.


72-73: User registration and login endpoints.

The user registration and login endpoints are correctly defined and utilize the appropriate handlers.


75-78: Middleware for user-related endpoints.

The middleware setup for user-related endpoints ensures proper authentication and role checks.


80-86: User-related endpoints.

The user-related endpoints are correctly defined, covering various user actions such as self-management and authentication.


88-93: Group-related endpoints.

The group-related endpoints are correctly defined, covering various group actions and statistics.


98-101: Action-related endpoints.

The action-related endpoints are correctly defined, covering various actions related to assets and items.


103-108: Location-related endpoints.

The location-related endpoints are correctly defined, covering various actions related to locations.


110-114: Label-related endpoints.

The label-related endpoints are correctly defined, covering various actions related to labels.


116-127: Item-related endpoints.

The item-related endpoints are correctly defined, covering various actions related to items, including creation, import, export, and management of custom fields.


129-131: Item attachment endpoints.

The item attachment endpoints are correctly defined, covering creation, update, and deletion of attachments.


133-136: Item maintenance endpoints.

The item maintenance endpoints are correctly defined, covering various actions related to maintenance logs.


138-138: Asset retrieval endpoint.

The asset retrieval endpoint is correctly defined and utilizes the appropriate handler.


140-145: Notifier-related endpoints.

The notifier-related endpoints are correctly defined, covering various actions related to user notifiers.


147-151: Middleware for asset-like endpoints.

The middleware setup for asset-like endpoints ensures proper authentication and role checks.


153-157: Asset-like endpoints for QR code generation and item attachments.

The asset-like endpoints for QR code generation and item attachments are correctly defined and utilize the appropriate handlers.


159-159: Reporting services endpoint.

The reporting services endpoint for bill of materials export is correctly defined and utilizes the appropriate handler.


162-162: Addition of default NotFound handler.

The addition of a default NotFound handler ensures that unknown API paths return a 404 status code, improving the API's robustness.


162-162: Ensure correct handling of 404 errors.

The r.NotFound(http.NotFound) line ensures that the correct 404 handler is used for unknown API paths, aligning with the PR's objective.

@tankerkiller125 tankerkiller125 merged commit d03e60d into sysadminsmedia:main Jul 8, 2024
@adamantike adamantike deleted the fix/use-api-subrouter-and-handle-api-404 branch July 8, 2024 15:47
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.

2 participants