Description
I'm currently experimenting with VS Code for Web and am attempting to bundle it with my own product.
Naturally, I want to ditch the node.JS http mini server for that and integrate with Gin.
Unfortunately, this proves easier said than done as Gin's StaticFS
seems to widely differ from Express'.
Serving the sample/
folder through Gin (after having renamed index.html
to index.htm
, since there seems to be an issue keeping the name intact) and accessing the index
yields me more than a couple 404 as the browser attempts to load those files from localhost:8080/ rather than localhost:8080/ide.
I got part of the GUI functional by manually serving product.json
alongside the directory, but it's still far from functional and this is also not an elegant approach.
Is there any way to make Gin behave more akin to Express in the way it serves files?
How to reproduce
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.StaticFS("/ide", http.Dir("code/sample"))
router.Run(":8080")
}
Expectations
Serve all files from http://localhost:8080/code
and start up the Web Edition of VSCode without major changes.
Actual result
GET: http://localhost:8080/product.json 404 Not Found
, among others.
(Can be somewhat alleviated with router.StaticFile("/product.json", "code/sample/product.json")
. But that's not feasible).
Environment
- go version: 1.19.1
- gin version: v1.7.7
- operating system: Linux/AMD64 (WSL2)
EDIT
This works but is sub-ideal
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.StaticFS("/", gin.Dir("code/sample", true))
router.Run(":8080")
}
As soon as I choose a deeper route than /
, there's more 404 errors and it fails again.
Comment From: Gys
Why are you using StaticFS() instead of Static()? In my experience Static() works as expected. StaticFS() was added specificly for embedded files (see go:embed
).