gin icon indicating copy to clipboard operation
gin copied to clipboard

feat(fs): Export, test and document OnlyFilesFS

Open joeig opened this issue 3 weeks ago • 1 comments

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)})

joeig avatar Apr 29 '24 14:04 joeig

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.

appleboy avatar May 07 '24 22:05 appleboy

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.

joeig avatar May 08 '24 06:05 joeig

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

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%) :arrow_down:
go-1.21 99.18% <100.00%> (-0.03%) :arrow_down:
go-1.22 99.18% <100.00%> (?)
macos-latest 99.18% <100.00%> (-0.03%) :arrow_down:
ubuntu-latest 99.18% <100.00%> (-0.03%) :arrow_down:

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

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar May 08 '24 07:05 codecov[bot]

@joeig Thanks for your explanation and effort.

appleboy avatar May 08 '24 07:05 appleboy