Merge pull request #2631 from kobergj/go-embed

Go embed
This commit is contained in:
kobergj
2021-10-20 11:31:31 +02:00
committed by GitHub
36 changed files with 173 additions and 3818 deletions

View File

@@ -32,7 +32,6 @@ include ../.make/generate.mk
.PHONY: ci-go-generate
ci-go-generate: protobuf # CI runs ci-node-generate automatically before this target
@go generate ./...
.PHONY: ci-node-generate
ci-node-generate: yarn-build

8
accounts/accounts.go Normal file
View File

@@ -0,0 +1,8 @@
package accounts
import (
"embed"
)
//go:embed assets/*
var Assets embed.FS

View File

@@ -1,66 +0,0 @@
package assets
import (
"net/http"
"os"
"path"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
// Fake the import to make the dep tree happy.
_ "golang.org/x/net/context"
// Fake the import to make the dep tree happy.
_ "golang.org/x/net/webdav"
)
//go:generate make -C ../.. embed.yml
// assets gets initialized by New and provides the handler.
type assets struct {
logger log.Logger
config *config.Config
}
// Open just implements the HTTP filesystem interface.
func (a assets) Open(original string) (http.File, error) {
if a.config.Asset.Path != "" {
if stat, err := os.Stat(a.config.Asset.Path); err == nil && stat.IsDir() {
custom := path.Join(
a.config.Asset.Path,
original,
)
if _, err := os.Stat(custom); !os.IsNotExist(err) {
f, err := os.Open(custom)
if err != nil {
return nil, err
}
return f, nil
}
} else {
a.logger.Warn().
Str("path", a.config.Asset.Path).
Msg("Assets directory doesn't exist")
}
}
return FS.OpenFile(
CTX,
original,
os.O_RDONLY,
0644,
)
}
// New returns a new http filesystem to serve assets.
func New(opts ...Option) http.FileSystem {
options := newOptions(opts...)
return assets{
config: options.Config,
}
}

File diff suppressed because one or more lines are too long

View File

@@ -1,17 +0,0 @@
---
pkg: "assets"
dest: "."
output: "embed.go"
fmt: true
noprefix: true
compression:
compress: true
custom:
- files:
- "../../assets/"
base: "../../assets/"
prefix: ""
...

View File

@@ -1,10 +1,20 @@
package assets
import (
"net/http"
"github.com/owncloud/ocis/accounts"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/assetsfs"
"github.com/owncloud/ocis/ocis-pkg/log"
)
// New returns a new http filesystem to serve assets.
func New(opts ...Option) http.FileSystem {
options := newOptions(opts...)
return assetsfs.New(accounts.Assets, options.Config.Asset.Path, options.Logger)
}
// Option defines a single option function.
type Option func(o *Options)

View File

@@ -0,0 +1,6 @@
Enhancement: use go-embed insted of fileb0x
go-embed delivers already the funtionality we need but with less code. We decided to use it instead of 3rd party fileb0x
https://github.com/owncloud/ocis/issues/1199
https://github.com/owncloud/ocis/pull/2631

View File

@@ -22,11 +22,10 @@ docs-generate: config-docs-generate
include ../.make/generate.mk
.PHONY: ci-go-generate
ci-go-generate: pull-assets # CI runs ci-node-generate automatically before this target
@go generate ./...
ci-go-generate: # CI runs ci-node-generate automatically before this target
.PHONY: ci-node-generate
ci-node-generate:
ci-node-generate: pull-assets
.PHONY: pull-assets
pull-assets:

View File

@@ -0,0 +1,8 @@
package graphexplorer
import (
"embed"
)
//go:embed assets/*
var Assets embed.FS

View File

@@ -1,37 +0,0 @@
package assets
import (
"net/http"
"os"
"github.com/owncloud/ocis/graph-explorer/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
)
//go:generate make -C ../.. embed.yml
// assets gets initialized by New and provides the handler.
type assets struct {
logger log.Logger
config *config.Config
}
// Open just implements the HTTP filesystem interface.
func (a assets) Open(original string) (http.File, error) {
return FS.OpenFile(
CTX,
original,
os.O_RDONLY,
0644,
)
}
// New returns a new http filesystem to serve assets.
func New(opts ...Option) http.FileSystem {
options := newOptions(opts...)
return assets{
logger: options.Logger,
config: options.Config,
}
}

View File

@@ -1,9 +0,0 @@
package assets
import (
// Fake the import to make the dep tree happy.
_ "golang.org/x/net/context"
// Fake the import to make the dep tree happy.
_ "golang.org/x/net/webdav"
)

File diff suppressed because one or more lines are too long

View File

@@ -1,16 +0,0 @@
---
pkg: "assets"
dest: "."
output: "embed.go"
fmt: true
noprefix: true
compression:
compress: true
custom:
- files:
- "../../assets/"
base: "../../assets/"
prefix: ""
...

View File

@@ -1,10 +1,20 @@
package assets
import (
"net/http"
graphexplorer "github.com/owncloud/ocis/graph-explorer"
"github.com/owncloud/ocis/graph-explorer/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/assetsfs"
"github.com/owncloud/ocis/ocis-pkg/log"
)
// New returns a new http filesystem to serve assets.
func New(opts ...Option) http.FileSystem {
options := newOptions(opts...)
return assetsfs.New(graphexplorer.Assets, "", options.Logger)
}
// Option defines a single option function.
type Option func(o *Options)

8
idp/idp.go Normal file
View File

@@ -0,0 +1,8 @@
package idp
import (
"embed"
)
//go:embed assets/*
var Assets embed.FS

View File

@@ -1,61 +0,0 @@
package assets
import (
"net/http"
"os"
"path"
"github.com/owncloud/ocis/idp/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
)
//go:generate make -C ../.. embed.yml
// assets gets initialized by New and provides the handler.
type assets struct {
logger log.Logger
config *config.Config
}
// Open just implements the HTTP filesystem interface.
func (a assets) Open(original string) (http.File, error) {
if a.config.Asset.Path != "" {
if stat, err := os.Stat(a.config.Asset.Path); err == nil && stat.IsDir() {
custom := path.Join(
a.config.Asset.Path,
original,
)
if _, err := os.Stat(custom); !os.IsNotExist(err) {
f, err := os.Open(custom)
if err != nil {
return nil, err
}
return f, nil
}
} else {
a.logger.Warn().
Str("path", a.config.Asset.Path).
Msg("Assets directory doesn't exist")
}
}
return FS.OpenFile(
CTX,
original,
os.O_RDONLY,
0644,
)
}
// New returns a new http filesystem to serve assets.
func New(opts ...Option) http.FileSystem {
options := newOptions(opts...)
return assets{
logger: options.Logger,
config: options.Config,
}
}

View File

@@ -1,9 +0,0 @@
package assets
import (
// Fake the import to make the dep tree happy.
_ "golang.org/x/net/context"
// Fake the import to make the dep tree happy.
_ "golang.org/x/net/webdav"
)

File diff suppressed because one or more lines are too long

View File

@@ -1,20 +0,0 @@
---
pkg: "assets"
dest: "."
output: "embed.go"
fmt: true
noprefix: true
compression:
compress: true
custom:
- files:
- "../../assets/"
base: "../../assets/"
prefix: ""
exclude:
- "asset-manifest.json"
- "precache-manifest.*.js"
...

View File

@@ -1,10 +1,20 @@
package assets
import (
"net/http"
"github.com/owncloud/ocis/idp"
"github.com/owncloud/ocis/idp/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/assetsfs"
"github.com/owncloud/ocis/ocis-pkg/log"
)
// New returns a new http filesystem to serve assets.
func New(opts ...Option) http.FileSystem {
options := newOptions(opts...)
return assetsfs.New(idp.Assets, options.Config.Asset.Path, options.Logger)
}
// Option defines a single option function.
type Option func(o *Options)

View File

@@ -0,0 +1,65 @@
package assetsfs
import (
"embed"
"fmt"
"io/fs"
"net/http"
"os"
"path"
"github.com/owncloud/ocis/ocis-pkg/log"
)
// FileSystem customized to load assets
type FileSystem struct {
fs http.FileSystem
assetPath string
log log.Logger
}
// Open checks if assetPath is set and tries to load from there. Falls back to fs if that is not possible
func (f *FileSystem) Open(original string) (http.File, error) {
if f.assetPath != "" {
file, err := read(f.assetPath, original)
if err == nil {
return file, nil
}
f.log.Warn().
Str("path", f.assetPath).
Str("filename", original).
Str("error", err.Error()).
Msg("error reading from assetPath")
}
return f.fs.Open(original)
}
// New initializes a new FileSystem. Quits on error
func New(embedFS embed.FS, assetPath string, logger log.Logger) *FileSystem {
f, err := fs.Sub(embedFS, "assets")
if err != nil {
fmt.Println("Cannot load subtree fs:", err.Error())
os.Exit(1)
}
return &FileSystem{
fs: http.FS(f),
assetPath: assetPath,
log: logger,
}
}
// tries to read file from disk or errors
func read(assetPath string, fileName string) (http.File, error) {
if stat, err := os.Stat(assetPath); err != nil || !stat.IsDir() {
return nil, fmt.Errorf("can't load asset path: %s", err)
}
p := path.Join(assetPath, fileName)
if _, err := os.Stat(p); err != nil {
return nil, err
}
return os.Open(p)
}

View File

@@ -31,4 +31,8 @@ ci-go-generate: # CI runs ci-node-generate automatically before this target
@go generate ./...
.PHONY: ci-node-generate
ci-node-generate:
ci-node-generate: # no need to recreate go code, just add assets
@make -C ../accounts ci-node-generate
@make -C ../graph-explorer ci-node-generate
@make -C ../settings ci-node-generate
@make -C ../web ci-node-generate

View File

@@ -26,4 +26,5 @@ ci-go-generate: # CI runs ci-node-generate automatically before this target
@go generate ./...
.PHONY: ci-node-generate
ci-node-generate:
ci-node-generate:
@make -C ../accounts ci-node-generate

View File

@@ -33,7 +33,6 @@ include ../.make/generate.mk
.PHONY: ci-go-generate
ci-go-generate: protobuf # CI runs ci-node-generate automatically before this target
@go generate ./...
.PHONY: ci-node-generate
ci-node-generate: yarn-build

View File

@@ -1,66 +0,0 @@
package assets
import (
"net/http"
"os"
"path/filepath"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/settings/pkg/config"
// Fake the import to make the dep tree happy.
_ "golang.org/x/net/context"
// Fake the import to make the dep tree happy.
_ "golang.org/x/net/webdav"
)
//go:generate make -C ../.. embed.yml
// assets gets initialized by New and provides the handler.
type assets struct {
logger log.Logger
config *config.Config
}
// Open just implements the HTTP filesystem interface.
func (a assets) Open(original string) (http.File, error) {
if a.config.Asset.Path != "" {
if stat, err := os.Stat(a.config.Asset.Path); err == nil && stat.IsDir() {
custom := filepath.Join(
a.config.Asset.Path,
original,
)
if _, err := os.Stat(custom); !os.IsNotExist(err) {
f, err := os.Open(custom)
if err != nil {
return nil, err
}
return f, nil
}
} else {
a.logger.Warn().
Str("path", a.config.Asset.Path).
Msg("Assets directory doesn't exist")
}
}
return FS.OpenFile(
CTX,
original,
os.O_RDONLY,
0644,
)
}
// New returns a new http filesystem to serve assets.
func New(opts ...Option) http.FileSystem {
options := newOptions(opts...)
return assets{
config: options.Config,
}
}

File diff suppressed because one or more lines are too long

View File

@@ -1,17 +0,0 @@
---
pkg: "assets"
dest: "."
output: "embed.go"
fmt: true
noprefix: true
compression:
compress: true
custom:
- files:
- "../../assets/"
base: "../../assets/"
prefix: ""
...

View File

@@ -1,10 +1,20 @@
package assets
import (
"net/http"
"github.com/owncloud/ocis/ocis-pkg/assetsfs"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/settings"
"github.com/owncloud/ocis/settings/pkg/config"
)
// New returns a new http filesystem to serve assets.
func New(opts ...Option) http.FileSystem {
options := newOptions(opts...)
return assetsfs.New(settings.Assets, options.Config.Asset.Path, options.Logger)
}
// Option defines a single option function.
type Option func(o *Options)

8
settings/settings.go Normal file
View File

@@ -0,0 +1,8 @@
package settings
import (
"embed"
)
//go:embed assets/*
var Assets embed.FS

View File

@@ -22,11 +22,10 @@ docs-generate: config-docs-generate
include ../.make/generate.mk
.PHONY: ci-go-generate
ci-go-generate: pull-assets # CI runs ci-node-generate automatically before this target
@go generate ./...
ci-go-generate: # CI runs ci-node-generate automatically before this target
.PHONY: ci-node-generate
ci-node-generate:
ci-node-generate: pull-assets
WEB_ASSETS_VERSION = v4.3.0

View File

@@ -1,61 +0,0 @@
package assets
import (
"net/http"
"os"
"path"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/web/pkg/config"
)
//go:generate make -C ../.. embed.yml
// assets gets initialized by New and provides the handler.
type assets struct {
logger log.Logger
config *config.Config
}
// Open just implements the HTTP filesystem interface.
func (a assets) Open(original string) (http.File, error) {
if a.config.Asset.Path != "" {
if stat, err := os.Stat(a.config.Asset.Path); err == nil && stat.IsDir() {
custom := path.Join(
a.config.Asset.Path,
original,
)
if _, err := os.Stat(custom); !os.IsNotExist(err) {
f, err := os.Open(custom)
if err != nil {
return nil, err
}
return f, nil
}
} else {
a.logger.Fatal().
Str("path", a.config.Asset.Path).
Msg("assets directory doesn't exist")
}
}
return FS.OpenFile(
CTX,
original,
os.O_RDONLY,
0644,
)
}
// New returns a new http filesystem to serve assets.
func New(opts ...Option) http.FileSystem {
options := newOptions(opts...)
return assets{
logger: options.Logger,
config: options.Config,
}
}

View File

@@ -1,9 +0,0 @@
package assets
import (
// Fake the import to make the dep tree happy.
_ "golang.org/x/net/context"
// Fake the import to make the dep tree happy.
_ "golang.org/x/net/webdav"
)

File diff suppressed because one or more lines are too long

View File

@@ -1,23 +0,0 @@
---
pkg: "assets"
dest: "."
output: "embed.go"
fmt: true
noprefix: true
compression:
compress: true
custom:
- files:
- "../../assets/"
base: "../../assets/"
prefix: ""
exclude:
- "appinfo/**"
- "LICENSE/**"
- "CHANGELOG.md"
- "README.md"
- "config.json"
...

View File

@@ -1,10 +1,20 @@
package assets
import (
"net/http"
"github.com/owncloud/ocis/ocis-pkg/assetsfs"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/web"
"github.com/owncloud/ocis/web/pkg/config"
)
// New returns a new http filesystem to serve assets.
func New(opts ...Option) http.FileSystem {
options := newOptions(opts...)
return assetsfs.New(web.Assets, options.Config.Asset.Path, options.Logger)
}
// Option defines a single option function.
type Option func(o *Options)

9
web/web.go Normal file
View File

@@ -0,0 +1,9 @@
package web
import (
"embed"
)
//go:embed assets/*
//go:embed assets/js/*
var Assets embed.FS