fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🩹 Fix: static server in sub app with mount (#3104)

Open yinheli opened this issue 1 year ago • 3 comments

Description

This commit fixes an issue with static file serving in sub-app when using the Mount method. The problem was that static routes in sub-apps were not being properly handled when the sub-app was mounted to a parent app.

Key changes:

  1. Modified the PathRewrite function in router.go to handle mount paths correctly for static file serving.
  2. Updated the Test_Route_Static_SubApp test in router_test.go to include a nested sub-app scenario.
  3. Added assertions to verify correct static file serving in both sub-app and nested sub-app cases.

Fixes #3104

Changes introduced

  • [x] Examples:
    parentApp := fiber.New()
    subApp := fiber.New()
    
    subApp.Static("/public", "./public")
    parentApp.Mount("/sub", subApp)
    
    // Now, "/sub/public/*" will correctly serve static files from the "./public" directory
    

Type of change

  • [x] Enhancement (improvement to existing features and functionality)

Checklist

Before you submit your pull request, please make sure you meet these requirements:

  • [x] Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
  • [x] Conducted a self-review of the code and provided comments for complex or critical parts.
  • [ ] Updated the documentation in the /docs/ directory for Fiber's documentation.
  • [x] Added or updated unit tests to validate the effectiveness of the changes or new features.
  • [x] Ensured that new and existing unit tests pass locally with the changes.
  • [x] Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • [x] Aimed for optimal performance with minimal allocations in the new code.
  • [x] Provided benchmarks for the new code to analyze and improve upon.

yinheli avatar Sep 14 '24 15:09 yinheli

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

welcome[bot] avatar Sep 14 '24 15:09 welcome[bot]

Walkthrough

The pull request introduces modifications to the registerStatic function in the fiber package to enhance path handling for static file serving, particularly when applications are mounted at specific paths. It also adds two new test functions in router_test.go to verify the functionality of static file routing, ensuring that both direct access and access through sub-applications to static files work correctly. Additionally, a new field parentApp is added to the mountFields struct in mount.go to improve path management.

Changes

Files Change Summary
router.go Modified registerStatic function to enhance path rewriting logic for static file serving.
router_test.go Added Test_Route_Static_HasPrefix and Test_Route_Static_SubApp to test static file routing functionality.
mount.go Introduced parentApp field in mountFields struct and added FullMountPath method to App struct for better path management.

Assessment against linked issues

Objective Addressed Explanation
Static server in sub app does not work (Issue #3104)

🐰 In the meadow where I hop and play,
A path was fixed to brighten the day.
Static files now serve with glee,
In sub-apps, they’re wild and free!
So come along, let’s cheer and sing,
For routing joy that changes bring! 🌼


Recent review details

Configuration used: CodeRabbit UI Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0bd4c0eb07820bc3e4954a2e63953838f29404cb and d4153992cd311ff1057b3a73cc85f315006ea005.

Files selected for processing (3)
  • mount.go (3 hunks)
  • router.go (2 hunks)
  • router_test.go (2 hunks)
Files skipped from review as they are similar to previous changes (3)
  • mount.go
  • router.go
  • router_test.go

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>, please review it. -- 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 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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

coderabbitai[bot] avatar Sep 14 '24 15:09 coderabbitai[bot]

@yinheli Does this issue affect v3 too? If so, can you do a Pull Request against main for v3. Thanks!

gaby avatar Sep 15 '24 00:09 gaby

https://github.com/gofiber/fiber/issues/3442#issuecomment-2876327394

ReneWerner87 avatar May 13 '25 12:05 ReneWerner87

@yinheli thanks for the effort, but since it does not solve the basic problem that the order of the routes is wrong, because the routes get a wrong initial position, i would close the pull request and provide one myself in the next days

https://github.com/gofiber/fiber/issues/3442#issuecomment-2876327394

ReneWerner87 avatar May 14 '25 19:05 ReneWerner87