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

feat(fs): Export, test and document OnlyFilesFS #3939

Merged
merged 1 commit into from May 8, 2024
Merged

Conversation

joeig
Copy link
Contributor

@joeig joeig commented Apr 29, 2024

I want to serve static assets from an embed.FS instance without directory listing. The onlyFilesFS wrapper is a perfect fit.

This PR exports onlyFilesFS, adds unit tests and documentation.

Usage example:

//go:embed dist/*
var assets embed.FS

router.StaticFS("/assets/", gin.OnlyFilesFS{FileSystem: http.FS(assets)})

@appleboy
Copy link
Member

appleboy commented May 7, 2024

We don't need export onlyFilesFS.

https://github.com/gin-gonic/examples/blob/0ebcc73e0c647a273847ee97519ee1dcc3dfb944/assets-in-binary/example02/main.go#L11-L20

//go:embed assets/* templates/*
var f embed.FS

func main() {
	router := gin.Default()
	templ := template.Must(template.New("").ParseFS(f, "templates/*.tmpl", "templates/foo/*.tmpl"))
	router.SetHTMLTemplate(templ)

	// example: /public/assets/images/example.png
	router.StaticFS("/public", http.FS(f))

Please revert the export and keep testing the code block.

@joeig
Copy link
Contributor Author

joeig commented May 8, 2024

router.StaticFS("/public", http.FS(f))

The problem with the example is that it allows directory listing. That's not an option for my use case, so I need OnlyFilesFS to avoid that.

Fixed the gci finding.

Copy link

codecov bot commented May 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.18%. Comparing base (3dc1cd6) to head (8b18c09).
Report is 48 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3939      +/-   ##
==========================================
- Coverage   99.21%   99.18%   -0.03%     
==========================================
  Files          42       43       +1     
  Lines        3182     2714     -468     
==========================================
- Hits         3157     2692     -465     
+ Misses         17       12       -5     
- Partials        8       10       +2     
Flag Coverage Δ
?
-tags "sonic avx" 99.18% <100.00%> (?)
-tags go_json 99.18% <100.00%> (?)
-tags nomsgpack 99.17% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 99.18% <100.00%> (-0.03%) ⬇️
go-1.21 99.18% <100.00%> (-0.03%) ⬇️
go-1.22 99.18% <100.00%> (?)
macos-latest 99.18% <100.00%> (-0.03%) ⬇️
ubuntu-latest 99.18% <100.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@appleboy appleboy merged commit 8791c96 into gin-gonic:master May 8, 2024
34 of 35 checks passed
@joeig joeig deleted the fs branch May 8, 2024 07:48
@appleboy
Copy link
Member

appleboy commented May 8, 2024

@joeig Thanks for your explanation and effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants