-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhance static file serving capabilities
- Add documentation for serving local files and embedded folders in README.md - Add new example code for serving embedded folders in Go - Create new `embed_folder.go` file with functions to serve embedded folders - Create new `embed_folder_test.go` file with tests for serving embedded folders - Add HTML template files for embedded server example - Rename `static.go` to `local_file.go` and remove unused code - Create new `local_file_test.go` file with tests for serving local files - Create new `serve.go` file with middleware handler for serving static files - Rename `static_test.go` to `serve_test.go` and refactor test functions - Remove redundant test case `TestListIndex` from `serve_test.go` Signed-off-by: Bo-Yi Wu <[email protected]>
- Loading branch information
Showing
11 changed files
with
215 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package static | ||
|
||
import ( | ||
"embed" | ||
"io/fs" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
type embedFileSystem struct { | ||
http.FileSystem | ||
} | ||
|
||
func (e embedFileSystem) Exists(prefix string, path string) bool { | ||
_, err := e.Open(path) | ||
return err == nil | ||
} | ||
|
||
func EmbedFolder(fsEmbed embed.FS, targetPath string) ServeFileSystem { | ||
fsys, err := fs.Sub(fsEmbed, targetPath) | ||
if err != nil { | ||
log.Fatalf("static.EmbedFolder - Invalid targetPath value - %s", err) | ||
} | ||
return embedFileSystem{ | ||
FileSystem: http.FS(fsys), | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package static | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
//go:embed test/data/server | ||
var server embed.FS | ||
|
||
var embedTests = []struct { | ||
targetURL string // input | ||
httpCode int // expected http code | ||
httpBody string // expected http body | ||
name string // test name | ||
}{ | ||
{"/404.html", 301, "<a href=\"/\">Moved Permanently</a>.\n\n", "Unknown file"}, | ||
{"/", 200, "<h1>Hello Embed</h1>", "Root"}, | ||
{"/index.html", 301, "", "Root by file name automatic redirect"}, | ||
{"/static.html", 200, "<h1>Hello Gin Static</h1>", "Other file"}, | ||
} | ||
|
||
func TestEmbedFolder(t *testing.T) { | ||
router := gin.New() | ||
router.Use(Serve("/", EmbedFolder(server, "test/data/server"))) | ||
router.NoRoute(func(c *gin.Context) { | ||
fmt.Printf("%s doesn't exists, redirect on /\n", c.Request.URL.Path) | ||
c.Redirect(301, "/") | ||
}) | ||
|
||
for _, tt := range embedTests { | ||
w := PerformRequest(router, "GET", tt.targetURL) | ||
assert.Equal(t, tt.httpCode, w.Code, tt.name) | ||
assert.Equal(t, tt.httpBody, w.Body.String(), tt.name) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Hello Embed</h1> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package static | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLocalFile(t *testing.T) { | ||
// SETUP file | ||
testRoot, _ := os.Getwd() | ||
f, err := ioutil.TempFile(testRoot, "") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
defer os.Remove(f.Name()) | ||
f.WriteString("Gin Web Framework") | ||
f.Close() | ||
|
||
dir, filename := filepath.Split(f.Name()) | ||
router := gin.New() | ||
router.Use(Serve("/", LocalFile(dir, true))) | ||
|
||
w := PerformRequest(router, "GET", "/"+filename) | ||
assert.Equal(t, w.Code, 200) | ||
assert.Equal(t, w.Body.String(), "Gin Web Framework") | ||
|
||
w = PerformRequest(router, "GET", "/") | ||
assert.Contains(t, w.Body.String(), `<a href="`+filename) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package static | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type ServeFileSystem interface { | ||
http.FileSystem | ||
Exists(prefix string, path string) bool | ||
} | ||
|
||
func ServeRoot(urlPrefix, root string) gin.HandlerFunc { | ||
return Serve(urlPrefix, LocalFile(root, false)) | ||
} | ||
|
||
// Serve returns a middleware handler that serves static files in the given directory. | ||
func Serve(urlPrefix string, fs ServeFileSystem) gin.HandlerFunc { | ||
fileserver := http.FileServer(fs) | ||
if urlPrefix != "" { | ||
fileserver = http.StripPrefix(urlPrefix, fileserver) | ||
} | ||
return func(c *gin.Context) { | ||
if fs.Exists(urlPrefix, c.Request.URL.Path) { | ||
fileserver.ServeHTTP(c.Writer, c.Request) | ||
c.Abort() | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Hello Embed</h1> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Hello Gin Static</h1> |