fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🔥 feat: Add Support for Serving Embedded Static Files

Open miseelu opened this issue 8 months ago • 8 comments

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

miseelu avatar May 13 '25 13:05 miseelu

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 May 13 '25 13:05 welcome[bot]

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.FS filesystem, 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 Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting 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.

❤️ 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.
    • 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 @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @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 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 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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 May 13 '25 13:05 coderabbitai[bot]

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

ReneWerner87 avatar May 13 '25 15:05 ReneWerner87

@miseelu can you provide tests for this feature

ReneWerner87 avatar May 14 '25 13:05 ReneWerner87

@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

ReneWerner87 avatar May 14 '25 19:05 ReneWerner87

@miseelu Please provide Unit-Tests and update the Documentation

gaby avatar May 15 '25 12:05 gaby

@miseelu can you check our last hints

ReneWerner87 avatar May 18 '25 13:05 ReneWerner87

@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.

Skyenought avatar May 21 '25 21:05 Skyenought

@ReneWerner87 Should we close this?

gaby avatar Oct 25 '25 14:10 gaby