-
-
Notifications
You must be signed in to change notification settings - Fork 388
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
21 changed files
with
39 additions
and
137 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
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,24 @@ | ||
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) | ||
} | ||
//go:embed static | ||
var templateFS embed.FS | ||
|
||
// 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 | ||
// 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 f | ||
} | ||
|
||
// 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 | ||
// remove "static/" prefix from path | ||
var contentFS, err = fs.Sub(templateFS, "static") | ||
if err != nil { | ||
return nil, err | ||
} | ||
return os.ReadFile(filepath.Clean(path)) | ||
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
Oops, something went wrong.