Created
June 2, 2025 12:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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