mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
PushTargetsRegexp: take advantage of the AssetNames on embedded file system
rel to: https://github.com/kataras/iris/issues/1562#issuecomment-660415246 Former-commit-id: 9e3fb4d71e14dda025a3af86cf210ff72127b716
This commit is contained in:
parent
552f990358
commit
e16abc96c6
|
@ -1,5 +1,6 @@
|
|||
// Code generated by bindata. DO NOT EDIT.
|
||||
// sources:
|
||||
// ..\http2push\assets\app2\app2app3\css\main.css
|
||||
// ..\http2push\assets\app2\app2app3\dirs\dir1\text.txt
|
||||
// ..\http2push\assets\app2\app2app3\dirs\dir2\text.txt
|
||||
// ..\http2push\assets\app2\app2app3\dirs\text.txt
|
||||
|
@ -60,6 +61,26 @@ func (fi gzipBindataFileInfo) Sys() interface{} {
|
|||
return nil
|
||||
}
|
||||
|
||||
var _gzipBindataAssetsapp2app2app3cssmaincss = []byte(
|
||||
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x4a\xca\x4f\xa9\x54\xa8\xe6\xe5\x52\x50\x50\x50\x48\x4a\x4c\xce\x4e\x2f" +
|
||||
"\xca\x2f\xcd\x4b\xd1\x4d\xce\xcf\xc9\x2f\xb2\x52\x48\xca\x29\x4d\xb5\xe6\xe5\xaa\x05\x04\x00\x00\xff\xff\x52\xd7" +
|
||||
"\xbb\x8b\x26\x00\x00\x00")
|
||||
|
||||
func gzipBindataAssetsapp2app2app3cssmaincss() (*gzipAsset, error) {
|
||||
bytes := _gzipBindataAssetsapp2app2app3cssmaincss
|
||||
info := gzipBindataFileInfo{
|
||||
name: "assets/app2/app2app3/css/main.css",
|
||||
size: 38,
|
||||
md5checksum: "",
|
||||
mode: os.FileMode(438),
|
||||
modTime: time.Unix(1595043712, 0),
|
||||
}
|
||||
|
||||
a := &gzipAsset{bytes: bytes, info: info}
|
||||
|
||||
return a, nil
|
||||
}
|
||||
|
||||
var _gzipBindataAssetsapp2app2app3dirsdir1texttxt = []byte(
|
||||
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x4a\x2c\x28\x30\xd2\x07\x11\x89\x05\x05\xc6\xfa\x29\x99\x45\xc5\x20\xc2" +
|
||||
"\x50\xbf\x24\xb5\xa2\x44\xaf\xa4\xa2\x04\x10\x00\x00\xff\xff\x87\xaf\x9d\x00\x20\x00\x00\x00")
|
||||
|
@ -118,17 +139,24 @@ func gzipBindataAssetsapp2app2app3dirstexttxt() (*gzipAsset, error) {
|
|||
}
|
||||
|
||||
var _gzipBindataAssetsapp2app2app3indexhtml = []byte(
|
||||
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb2\xc9\x30\xb4\xf3\x48\xcd\xc9\xc9\x57\x70\x2c\x28\x30\x72\x2c\x28\x30" +
|
||||
"\x56\xc8\xcc\x4b\x49\xad\xb0\xd1\xcf\x30\xb4\x03\x04\x00\x00\xff\xff\x74\xbe\xbd\x8c\x1d\x00\x00\x00")
|
||||
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x54\x90\x3f\x4f\xc3\x40\x0c\xc5\xf7\x48\xf9\x0e\xc6\x33\xed\x91\x76\x61" +
|
||||
"\xb8\x8b\x54\xf1\x47\x6c\x30\x94\x81\xf1\x7a\x31\x9c\x85\x73\x39\xe5\x4c\x4b\xbf\x3d\x4a\x68\x90\x58\x6c\x3d\xfb" +
|
||||
"\xbd\x9f\x64\xdb\xab\xfb\xe7\xbb\xfd\xdb\xcb\x03\x44\xed\xa5\xad\x2b\x3b\x75\x10\x9f\x3e\x1c\x52\xc2\xb6\xae\xa6" +
|
||||
"\x19\xf9\xae\xad\x2b\x00\x00\xdb\x93\x7a\x08\xd1\x8f\x85\xd4\xe1\xeb\xfe\x71\x75\x8b\xff\x76\xc9\xf7\xe4\xf0\xc8" +
|
||||
"\x74\xca\xc3\xa8\x08\x61\x48\x4a\x49\x1d\x9e\xb8\xd3\xe8\x3a\x3a\x72\xa0\xd5\x2c\xae\x81\x13\x2b\x7b\x59\x95\xe0" +
|
||||
"\x85\x5c\xb3\xbe\xf9\x63\x09\xa7\x4f\x18\x49\x1c\x16\x3d\x0b\x95\x48\xa4\x08\x71\xa4\x77\x87\x26\x7f\x1d\x84\x83" +
|
||||
"\xf1\x39\x6f\xe6\xe2\x73\xde\x9a\x50\x8a\xe9\x3d\xa7\x75\x28\x05\xc1\x2c\x20\x65\x15\x6a\x77\x39\x6f\x76\x39\x6f" +
|
||||
"\xad\xf9\xd5\x75\x65\xcd\xe5\xac\xba\xb2\x87\xa1\x3b\x2f\xfe\xd8\xb4\x4f\x24\x32\xc0\x12\x01\x4e\x1d\x7d\x5b\x13" +
|
||||
"\x9b\x39\x75\xf1\xce\x80\xe9\x67\x3f\x01\x00\x00\xff\xff\xef\x25\x54\xc8\x43\x01\x00\x00")
|
||||
|
||||
func gzipBindataAssetsapp2app2app3indexhtml() (*gzipAsset, error) {
|
||||
bytes := _gzipBindataAssetsapp2app2app3indexhtml
|
||||
info := gzipBindataFileInfo{
|
||||
name: "assets/app2/app2app3/index.html",
|
||||
size: 29,
|
||||
size: 323,
|
||||
md5checksum: "",
|
||||
mode: os.FileMode(438),
|
||||
modTime: time.Unix(1565946440, 0),
|
||||
modTime: time.Unix(1595043725, 0),
|
||||
}
|
||||
|
||||
a := &gzipAsset{bytes: bytes, info: info}
|
||||
|
@ -460,6 +488,7 @@ func GzipAssetNames() []string {
|
|||
// _gzipbindata is a table, holding each asset generator, mapped to its name.
|
||||
//
|
||||
var _gzipbindata = map[string]func() (*gzipAsset, error){
|
||||
"assets/app2/app2app3/css/main.css": gzipBindataAssetsapp2app2app3cssmaincss,
|
||||
"assets/app2/app2app3/dirs/dir1/text.txt": gzipBindataAssetsapp2app2app3dirsdir1texttxt,
|
||||
"assets/app2/app2app3/dirs/dir2/text.txt": gzipBindataAssetsapp2app2app3dirsdir2texttxt,
|
||||
"assets/app2/app2app3/dirs/text.txt": gzipBindataAssetsapp2app2app3dirstexttxt,
|
||||
|
@ -524,6 +553,9 @@ var _gzipbintree = &gzipBintree{Func: nil, Children: map[string]*gzipBintree{
|
|||
"assets": {Func: nil, Children: map[string]*gzipBintree{
|
||||
"app2": {Func: nil, Children: map[string]*gzipBintree{
|
||||
"app2app3": {Func: nil, Children: map[string]*gzipBintree{
|
||||
"css": {Func: nil, Children: map[string]*gzipBintree{
|
||||
"main.css": {Func: gzipBindataAssetsapp2app2app3cssmaincss, Children: map[string]*gzipBintree{}},
|
||||
}},
|
||||
"dirs": {Func: nil, Children: map[string]*gzipBintree{
|
||||
"dir1": {Func: nil, Children: map[string]*gzipBintree{
|
||||
"text.txt": {Func: gzipBindataAssetsapp2app2app3dirsdir1texttxt, Children: map[string]*gzipBintree{}},
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,6 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
|
@ -16,13 +18,9 @@ import (
|
|||
|
||||
var opts = iris.DirOptions{
|
||||
IndexName: "/index.html",
|
||||
PushTargets: map[string][]string{
|
||||
"/": { // Relative path without route prefix.
|
||||
"favicon.ico",
|
||||
"js/main.js",
|
||||
"css/main.css",
|
||||
// ^ Relative to the index, if need absolute ones start with a slash ('/').
|
||||
},
|
||||
PushTargetsRegexp: map[string]*regexp.Regexp{
|
||||
"/": iris.MatchCommonAssets,
|
||||
"/app2/app2app3": iris.MatchCommonAssets,
|
||||
},
|
||||
Compress: false,
|
||||
ShowList: true,
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
body {
|
||||
background-color: blue;
|
||||
}
|
|
@ -1 +1,15 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="/public/app2/app2app3/css/main.css" />
|
||||
<title>App2App3</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Hello App2App3 index</h1>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -248,30 +248,29 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
|
|||
if options.Asset != nil && options.AssetInfo != nil && options.AssetNames != nil {
|
||||
// Depends on the command the user gave to the go-bindata
|
||||
// the assset path (names) may be or may not be prepended with a slash.
|
||||
// What we do: we remove the ./ from the vdir which should be
|
||||
// What we do: we remove the ./ from the directory which should be
|
||||
// the same with the asset path (names).
|
||||
// we don't pathclean, because that will prepend a slash
|
||||
// go-bindata should give a correct path format.
|
||||
// On serve time we check the "paramName" (which is the path after the "requestPath")
|
||||
// so it has the first directory part missing, we use the "vdir" to complete it
|
||||
// so it has the first directory part missing, we use the "directory" to complete it
|
||||
// and match with the asset path (names).
|
||||
vdir := directory
|
||||
|
||||
if vdir[0] == '.' {
|
||||
vdir = vdir[1:]
|
||||
if directory[0] == '.' {
|
||||
directory = directory[1:]
|
||||
}
|
||||
|
||||
// second check for /something, (or ./something if we had dot on 0 it will be removed)
|
||||
if vdir[0] == '/' || vdir[0] == os.PathSeparator {
|
||||
vdir = vdir[1:]
|
||||
if directory[0] == '/' || directory[0] == os.PathSeparator {
|
||||
directory = directory[1:]
|
||||
}
|
||||
|
||||
// check for trailing slashes because new users may be do that by mistake
|
||||
// although all examples are showing the correct way but you never know
|
||||
// i.e "./assets/" is not correct, if was inside "./assets".
|
||||
// remove last "/".
|
||||
if trailingSlashIdx := len(vdir) - 1; vdir[trailingSlashIdx] == '/' {
|
||||
vdir = vdir[0:trailingSlashIdx]
|
||||
if trailingSlashIdx := len(directory) - 1; directory[trailingSlashIdx] == '/' {
|
||||
directory = directory[0:trailingSlashIdx]
|
||||
}
|
||||
|
||||
// select only the paths that we care;
|
||||
|
@ -281,11 +280,18 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
|
|||
for _, name := range options.AssetNames() {
|
||||
// i.e: name = static/css/main.css (including the directory, see `embeddedFileSystem.vdir`)
|
||||
|
||||
if !strings.HasPrefix(name, vdir) {
|
||||
if !strings.HasPrefix(name, directory) {
|
||||
continue
|
||||
}
|
||||
|
||||
names = append(names, strings.TrimPrefix(name, vdir))
|
||||
names = append(names, strings.TrimPrefix(name, directory))
|
||||
}
|
||||
|
||||
// Update the options.AssetNames with
|
||||
// the pre-calculated files we only care about.
|
||||
// See PushTargets(Regexp) bellow.
|
||||
options.AssetNames = func() []string {
|
||||
return names
|
||||
}
|
||||
|
||||
if len(names) == 0 {
|
||||
|
@ -293,11 +299,11 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
|
|||
}
|
||||
|
||||
asset := func(name string) ([]byte, error) {
|
||||
return options.Asset(vdir + name)
|
||||
return options.Asset(directory + name)
|
||||
}
|
||||
|
||||
assetInfo := func(name string) (os.FileInfo, error) {
|
||||
return options.AssetInfo(vdir + name)
|
||||
return options.AssetInfo(directory + name)
|
||||
}
|
||||
|
||||
dirNames := make(map[string]*embeddedDir)
|
||||
|
@ -336,7 +342,7 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
|
|||
}
|
||||
|
||||
fs = &embeddedFileSystem{
|
||||
vdir: vdir,
|
||||
vdir: directory,
|
||||
dirNames: dirNames,
|
||||
|
||||
asset: asset,
|
||||
|
@ -479,17 +485,53 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
|
|||
}
|
||||
|
||||
if regex, ok := options.PushTargetsRegexp[r.URL.Path]; ok {
|
||||
// TODO(@kataras): Fix: on physical directory the push targets regexp
|
||||
// will work on root indexes but NOT
|
||||
// at subindex(sub directory that contain an index file and assets).
|
||||
if pusher, ok := ctx.ResponseWriter().(http.Pusher); ok {
|
||||
for _, indexAsset := range getFilenamesRecursively(fs, indexDirectory, "") {
|
||||
|
||||
var (
|
||||
prefixURL string
|
||||
indexAssets []string
|
||||
)
|
||||
|
||||
// Use of the AssetNames (static list of filenames (no dirs)),
|
||||
// improves performance vs searching on files each time.
|
||||
if options.AssetNames != nil {
|
||||
// This is required on embedded, and we can use it
|
||||
// because info.Name returns the full name,
|
||||
// http.Dir does not though.
|
||||
prefixDir := strings.TrimPrefix(path.Dir(info.Name()), directory)
|
||||
if prefixDir == "" || prefixDir == "." {
|
||||
prefixDir = "/"
|
||||
}
|
||||
prefixURL = strings.TrimSuffix(r.RequestURI, prefixDir)
|
||||
// currentDirname := strings.TrimPrefix(r.RequestURI, prefixURL)
|
||||
|
||||
for _, assetName := range options.AssetNames() {
|
||||
// The file server may contain more than one directory with an index file
|
||||
// so we must use the files under THIS index directory one.
|
||||
if strings.HasPrefix(assetName, prefixDir) {
|
||||
assetName = strings.TrimPrefix(assetName, prefixURL)
|
||||
indexAssets = append(indexAssets, assetName)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
prefixURL = r.RequestURI
|
||||
indexAssets = getFilenamesRecursively(fs, indexDirectory, "")
|
||||
}
|
||||
|
||||
for _, indexAsset := range indexAssets {
|
||||
// it's an index file, do not pushed that.
|
||||
if strings.HasSuffix("/"+indexAsset, options.IndexName) {
|
||||
if strings.HasSuffix(prefix(indexAsset, "/"), options.IndexName) {
|
||||
continue
|
||||
}
|
||||
|
||||
// match using relative path (without the first '/' slash)
|
||||
// to keep consistency between the `PushTargets` behavior
|
||||
if regex.MatchString(indexAsset) {
|
||||
// println("Regex Matched: " + indexAsset)
|
||||
if err = pusher.Push(path.Join(r.RequestURI, indexAsset), nil); err != nil {
|
||||
if err = pusher.Push(path.Join(prefixURL, indexAsset), nil); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -595,7 +637,6 @@ func getFilenamesRecursively(fs http.FileSystem, f http.File, parent string) []s
|
|||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
filenames = append(filenames, getFilenamesRecursively(fs, ff, fullname)...)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user