Skip to content

Instantly share code, notes, and snippets.

@knjname
Created June 2, 2025 12:48
Show Gist options
  • Save knjname/e7c0af8e0fe71db9bcfb3463e4806d02 to your computer and use it in GitHub Desktop.
Save knjname/e7c0af8e0fe71db9bcfb3463e4806d02 to your computer and use it in GitHub Desktop.
Go Gin example for serving frontend/dist assets at "/" and also serving "/api/*" requests without conflicting issue
package main
import (
"embed"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
// See https://github.com/gin-gonic/gin/issues/301
func main() {
fs, _ := static.EmbedFolder(staticFiles, "frontend/dist")
router := gin.Default()
router.Use(static.Serve("/", fs))
router.GET("/api/ping", func(ctx *gin.Context) {
ctx.String(200, "pong")
})
router.Run(":8080")
}
//go:embed frontend/dist
var staticFiles embed.FS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment