🔥 feat: Add Support for Serving Embedded Static Files
Description
This pull request introduces a new feature to Fiber v2 that enables serving static files directly from an embedded filesystem (embed.FS). The enhancement allows developers to bundle static resources with their Go binary using Go's embed functionality, eliminating the need for external file dependencies in production.
The main change adds a new StaticFilesystem method that accepts an embed.FS instance, making it seamless to serve embedded static files while maintaining consistency with Fiber's existing static file serving API.
-
Added StaticFilesystem method to support serving static resources from embed.FS
-
Implemented registerStaticFS helper function in router.go
-
Maintained backward compatibility with existing static file serving functionality
Fixes # (issue)
Changes introduced
List the new features or adjustments introduced in this pull request. Provide details on benchmarks, documentation updates, changelog entries, and if applicable, the migration guide.
- [x] Benchmarks: Describe any performance benchmarks and improvements related to the changes.
- [ ] Documentation Update: Detail the updates made to the documentation and links to the changed files.
- [ ] Changelog/What's New: Include a summary of the additions for the upcoming release notes.
- [ ] Migration Guide: If necessary, provide a guide or steps for users to migrate their existing code to accommodate these changes.
- [ ] API Alignment with Express: Explain how the changes align with the Express API.
- [ ] API Longevity: Discuss the steps taken to ensure that the new or updated APIs are consistent and not prone to breaking changes.
- [ ] Examples: Provide examples demonstrating the new features or changes in action.
Type of change
Please delete options that are not relevant.
- [x] New feature (non-breaking change which adds functionality)
- [x] Enhancement (improvement to existing features and functionality)
- [ ] Documentation update (changes to documentation)
- [ ] Performance improvement (non-breaking change which improves efficiency)
- [x] Code consistency (non-breaking change which improves code reliability and robustness)
Checklist
Before you submit your pull request, please make sure you meet these requirements:
- [ ] Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
- [ ] 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. - [ ] Added or updated unit tests to validate the effectiveness of the changes or new features.
- [ ] Ensured that new and existing unit tests pass locally with the changes.
- [ ] Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
- [ ] Aimed for optimal performance with minimal allocations in the new code.
- [ ] Provided benchmarks for the new code to analyze and improve upon.
Commit formatting
Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md
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
A new method, StaticFilesystem, has been added to the App struct to enable serving static files from any fs.FS filesystem, including embedded filesystems. Supporting logic is implemented via a new internal method, registerStaticFS, which handles configuration and routing for static file serving.
Changes
| File(s) | Change Summary |
|---|---|
| app.go | Added StaticFilesystem method to App struct for serving static files from an fs.FS filesystem. |
| router.go | Introduced registerStaticFS method to handle static file serving from io/fs.FS filesystems. |
Sequence Diagram(s)
sequenceDiagram
participant User
participant App
participant Router
participant FSHandler
User->>App: Call StaticFilesystem(prefix, filesystem, config)
App->>Router: registerStaticFS(prefix, filesystem, config)
Router->>FSHandler: Setup handler with config
User->>App: HTTP GET /static/file
App->>FSHandler: Serve file from fs.FS
FSHandler-->>User: Return file content or next middleware
Possibly related issues
- gofiber/template#394: Implements direct support for serving static files from any
fs.FSfilesystem, which matches the new functionality introduced.
Possibly related PRs
- gofiber/fiber#3006: Both PRs focus on serving static files via
io/fs, with this PR adding a method and registration logic, and the referenced PR restructuring middleware for unified static file support.
Suggested labels
✏️ Feature
Suggested reviewers
- gaby
- efectn
- ReneWerner87
Poem
A filesystem hops into view,
With static files both old and new.
Embedded or not, we serve with glee,
From code to client, as quick as can be!
The bunny’s proud, the code is neat—
Static files now taste extra sweet! 🐇
[!TIP]
⚡️ Faster reviews with caching
- CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure
Review - Disable Cacheat either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off theData Retentionsetting under your Organization Settings.Enjoy the performance boost—your workflow just got faster.
✨ Finishing Touches
- [ ] 📝 Generate Docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
🪧 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. -
Explain this complex logic. -
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 explain this code block. -
@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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase. -
@coderabbitai read src/utils.ts and explain its main purpose. -
@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.
-
Support
Need help? Create a ticket on our support page for assistance with any issues or questions.
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 generate docstringsto generate docstrings for this PR. -
@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this 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.
Thx for the effort Not sure if we should adopt this We actually have a feature stop for v2 so that all resources can focus on v3 Only bugs are still being fixed in v2
Is everything go 1.17 ready? V2 is supporting this old version
@miseelu can you provide tests for this feature
@miseelu just so you know, i'm currently trying to reduce the custom methods for adding routes so that this part should be prevented https://github.com/gofiber/fiber/blob/v2/router.go#L429-L444
in this issue https://github.com/gofiber/fiber/issues/3442#issuecomment-2876327394 so we may have to slightly modify your part again
the idea behind this is not to create the route struct in different places
@miseelu Please provide Unit-Tests and update the Documentation
@miseelu can you check our last hints
@miseelu I think you should tag your PR with “Work In Progress”, after all I don't think the changes you're making now will fit into the branch.
@ReneWerner87 Should we close this?