From 9ae1c0d5419e17e630138760934a167ac5c8ce58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Omn=C3=A8s?= Date: Mon, 15 Mar 2021 19:11:23 +0100 Subject: [PATCH] feat: Implement embed folder and a better organisation --- example/embed/data/server/index.html | 1 + example/embed/example.go | 27 +++++++++++++++++++++++++++ static.go => serve.go | 0 static_test.go => serve_test.go | 0 4 files changed, 28 insertions(+) create mode 100644 example/embed/data/server/index.html create mode 100644 example/embed/example.go rename static.go => serve.go (100%) rename static_test.go => serve_test.go (100%) diff --git a/example/embed/data/server/index.html b/example/embed/data/server/index.html new file mode 100644 index 0000000..1d3fa49 --- /dev/null +++ b/example/embed/data/server/index.html @@ -0,0 +1 @@ +

Hello Embed

\ No newline at end of file diff --git a/example/embed/example.go b/example/embed/example.go new file mode 100644 index 0000000..6df55ce --- /dev/null +++ b/example/embed/example.go @@ -0,0 +1,27 @@ +package main + +import ( + "embed" + "fmt" + "net/http" + + "github.com/gin-contrib/static" + "github.com/gin-gonic/gin" +) + +//go:embed data +var server embed.FS + +func main() { + r := gin.Default() + r.Use(static.Serve("/", static.EmbedFolder(server, "data/server"))) + r.GET("/ping", func(c *gin.Context) { + c.String(200, "test") + }) + r.NoRoute(func(c *gin.Context) { + fmt.Printf("%s doesn't exists, redirect on /\n", c.Request.URL.Path) + c.Redirect(http.StatusMovedPermanently, "/") + }) + // Listen and Server in 0.0.0.0:8080 + r.Run(":8080") +} diff --git a/static.go b/serve.go similarity index 100% rename from static.go rename to serve.go diff --git a/static_test.go b/serve_test.go similarity index 100% rename from static_test.go rename to serve_test.go