-
Notifications
You must be signed in to change notification settings - Fork 8k
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
FileFromFS #2112
FileFromFS #2112
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2112 +/- ##
=======================================
Coverage 98.38% 98.38%
=======================================
Files 41 41
Lines 2290 2296 +6
=======================================
+ Hits 2253 2259 +6
Misses 21 21
Partials 16 16 Continue to review full report at Codecov.
|
Hi! I'm just remembering about my PR) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
need @appleboy review the PR, thanks1 |
* Context.FileFromFS added * Context File and FileFromFS examples at README Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
* Context.FileFromFS added * Context File and FileFromFS examples at README Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Hi @nikandfor , can you help add more detail for this part:
What the exact sample content for I tried many cases, they all don't work for me, my sample code:
For the first parameter of Thank you! |
After diving into the source code, I found we just can't use index.html, it will be redirected, the source code in fs.go#serverFile() method. // name is '/'-separated, not filepath.Separator.
func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirect bool) {
const indexPage = "/index.html"
// redirect .../index.html to .../
// can't use Redirect() because that would make the path absolute,
// which would be a problem running under StripPrefix
if strings.HasSuffix(r.URL.Path, indexPage) {
localRedirect(w, r, "./")
return
} So I change the index.html name to report.html and works by following code: endpoint := r.Group("/diagnose")
endpoint.StaticFS("/assets", http.Dir("ui/build"))
var fs http.FileSystem = http.Dir("ui/build")
endpoint.GET("/reports/detail", func(c *gin.Context) {
c.FileFromFS("report.html", fs)
}) |
@baurine It's really interesting finding. const indexPage = "index.html"
filepath = strings.TrimSuffix(filepath, indexPage) |
hi @nikandfor , I just rename my index.html to report.html to workaround it... |
@thinkerou how do you think, do we need to add some fix to avoid such situations in the future? |
this was quite annoying, trying to serve an embedded asset called Maybe add a comment or throw an error |
I think so, rename |
I was missing feature of Serving file from
http.FileSystem
.Use cases:
/api/
handlers.FileSystem
dynamically.