Get rid of ugly statik

go1.16 introduced the embed package, it's the more graceful solution for embedding file into binary.
https://golang.org/pkg/embed/
This commit is contained in:
bobo liu 2021-02-24 01:33:32 +08:00 committed by bobo liu
parent a62a9431b1
commit f773f85f8a
19 changed files with 37 additions and 59 deletions

View File

@ -14,22 +14,15 @@
package assets package assets
//go:generate statik -src=./frps/static -dest=./frps
//go:generate statik -src=./frpc/static -dest=./frpc
//go:generate go fmt ./frps/statik/statik.go
//go:generate go fmt ./frpc/statik/statik.go
import ( import (
"io/ioutil" "io/fs"
"net/http" "net/http"
"os"
"path"
"github.com/rakyll/statik/fs"
) )
var ( var (
// store static files in memory by statik // read-only filesystem created by "embed" for embedded files
content fs.FS
FileSystem http.FileSystem FileSystem http.FileSystem
// if prefix is not empty, we get file content from disk // if prefix is not empty, we get file content from disk
@ -44,34 +37,11 @@ func Load(path string) (err error) {
FileSystem = http.Dir(prefixPath) FileSystem = http.Dir(prefixPath)
return nil return nil
} else { } else {
FileSystem, err = fs.New() FileSystem = http.FS(content)
} }
return err return err
} }
func ReadFile(file string) (content string, err error) { func Register(fs fs.FS) {
if prefixPath == "" { content = fs
file, err := FileSystem.Open(path.Join("/", file))
if err != nil {
return content, err
}
defer file.Close()
buf, err := ioutil.ReadAll(file)
if err != nil {
return content, err
}
content = string(buf)
} else {
file, err := os.Open(path.Join(prefixPath, file))
if err != nil {
return content, err
}
defer file.Close()
buf, err := ioutil.ReadAll(file)
if err != nil {
return content, err
}
content = string(buf)
}
return content, err
} }

14
assets/frpc/embed.go Normal file
View File

@ -0,0 +1,14 @@
package frpc
import (
"embed"
"github.com/fatedier/frp/assets"
)
//go:embed *
var content embed.FS
func init() {
assets.Register(content)
}

View File

Before

Width:  |  Height:  |  Size: 9.4 KiB

After

Width:  |  Height:  |  Size: 9.4 KiB

File diff suppressed because one or more lines are too long

14
assets/frps/embed.go Normal file
View File

@ -0,0 +1,14 @@
package frpc
import (
"embed"
"github.com/fatedier/frp/assets"
)
//go:embed *
var content embed.FS
func init() {
assets.Register(content)
}

View File

Before

Width:  |  Height:  |  Size: 9.4 KiB

After

Width:  |  Height:  |  Size: 9.4 KiB

File diff suppressed because one or more lines are too long

View File

@ -18,7 +18,7 @@ import (
"math/rand" "math/rand"
"time" "time"
_ "github.com/fatedier/frp/assets/frpc/statik" _ "github.com/fatedier/frp/assets/frpc"
"github.com/fatedier/frp/cmd/frpc/sub" "github.com/fatedier/frp/cmd/frpc/sub"
"github.com/fatedier/golib/crypto" "github.com/fatedier/golib/crypto"

View File

@ -20,7 +20,7 @@ import (
"github.com/fatedier/golib/crypto" "github.com/fatedier/golib/crypto"
_ "github.com/fatedier/frp/assets/frps/statik" _ "github.com/fatedier/frp/assets/frps"
_ "github.com/fatedier/frp/pkg/metrics" _ "github.com/fatedier/frp/pkg/metrics"
) )