-
-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move templates from rakyll/statik to go:embed
There is no need for the rakyll/statik package starting with Go 1.16, which provides us with tools for embedding files without third-party libraries.
- Loading branch information
Showing
20 changed files
with
38 additions
and
127 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
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
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,38 +1,21 @@ | ||
package templates | ||
|
||
import ( | ||
"net/http" | ||
"embed" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
|
||
log "github.com/go-pkgz/lgr" | ||
"github.com/rakyll/statik/fs" | ||
) | ||
|
||
// FS stores link to statikFS if it exists | ||
type FS struct { | ||
statik http.FileSystem | ||
} | ||
|
||
// FileReader describes methods of filesystem | ||
type FileReader interface { | ||
ReadFile(path string) ([]byte, error) | ||
} | ||
|
||
// NewFS returns new FS instance, which will read from statik if it's available and from fs otherwise | ||
func NewFS() *FS { | ||
f := &FS{} | ||
if statikFS, err := fs.NewWithNamespace("templates"); err == nil { | ||
log.Printf("[INFO] templates will be read from statik") | ||
f.statik = statikFS | ||
} | ||
return f | ||
} | ||
//go:embed static | ||
var templateFS embed.FS | ||
|
||
// ReadFile depends on statik achieve exists | ||
func (f *FS) ReadFile(path string) ([]byte, error) { | ||
if f.statik != nil { | ||
return fs.ReadFile(f.statik, filepath.Join("/", path)) //nolint:gocritic // root folder is a requirement for statik | ||
// Read reads either template from disk if it exists, or from embedded template | ||
func Read(path string) ([]byte, error) { | ||
if _, err := os.Stat(filepath.Clean(path)); err == nil { | ||
return os.ReadFile(filepath.Clean(path)) | ||
} | ||
return os.ReadFile(filepath.Clean(path)) | ||
// remove "static/" prefix from path | ||
var contentFS, _ = fs.Sub(templateFS, "static") | ||
return fs.ReadFile(contentFS, filepath.Clean(path)) | ||
} |
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