-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement embed folder and a better organisation
- Loading branch information
Showing
11 changed files
with
198 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ jobs: | |
- go: 1.13.x | ||
- go: 1.14.x | ||
- go: 1.15.x | ||
- go: 1.16.x | ||
- go: master | ||
|
||
install: | ||
|
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 |
---|---|---|
|
@@ -6,4 +6,4 @@ require ( | |
github.com/stretchr/testify v1.4.0 | ||
) | ||
|
||
go 1.13 | ||
go 1.16 |
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,47 @@ | ||
package static | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
const INDEX = "index.html" | ||
|
||
type localFileSystem struct { | ||
http.FileSystem | ||
root string | ||
indexes bool | ||
} | ||
|
||
func LocalFile(root string, indexes bool) *localFileSystem { | ||
return &localFileSystem{ | ||
FileSystem: gin.Dir(root, indexes), | ||
root: root, | ||
indexes: indexes, | ||
} | ||
} | ||
|
||
func (l *localFileSystem) Exists(prefix string, filepath string) bool { | ||
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) { | ||
name := path.Join(l.root, p) | ||
stats, err := os.Stat(name) | ||
if err != nil { | ||
return false | ||
} | ||
if stats.IsDir() { | ||
if !l.indexes { | ||
index := path.Join(name, INDEX) | ||
_, err := os.Stat(index) | ||
if err != nil { | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
return false | ||
} |
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
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> |