🩹 Fix: static server in sub app with mount (#3104)
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:
- Modified the
PathRewritefunction inrouter.goto handle mount paths correctly for static file serving. - Updated the
Test_Route_Static_SubApptest inrouter_test.goto include a nested sub-app scenario. - 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.
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
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?
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
@coderabbitaiin 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
@coderabbitaiin 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 pauseto pause the reviews on a PR. -
@coderabbitai resumeto resume the paused reviews. -
@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository. -
@coderabbitai full reviewto do a full review from scratch and review all the files again. -
@coderabbitai summaryto regenerate the summary of the PR. -
@coderabbitai resolveresolve all the CodeRabbit review comments. -
@coderabbitai configurationto show the current CodeRabbit configuration for the repository. -
@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile 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.
@yinheli Does this issue affect v3 too? If so, can you do a Pull Request against main for v3. Thanks!
https://github.com/gofiber/fiber/issues/3442#issuecomment-2876327394
@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