Begin unit tests, relates to #35
This commit is contained in:
parent
da8f90d388
commit
be50af0a7a
11 changed files with 198 additions and 61 deletions
|
@ -8,11 +8,23 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// CachingEmbedFS is a wrapper around embed.FS that allows setting a ModTime, so that the
|
||||
// default static file server can send 304s back. It can be used like this:
|
||||
//
|
||||
// var (
|
||||
// //go:embed docs
|
||||
// docsStaticFs embed.FS
|
||||
// docsStaticCached = &util.CachingEmbedFS{ModTime: time.Now(), FS: docsStaticFs}
|
||||
// )
|
||||
//
|
||||
// http.FileServer(http.FS(docsStaticCached)).ServeHTTP(w, r)
|
||||
//
|
||||
type CachingEmbedFS struct {
|
||||
ModTime time.Time
|
||||
FS embed.FS
|
||||
}
|
||||
|
||||
// Open opens a file in the embedded filesystem and returns a fs.File with the static ModTime
|
||||
func (f CachingEmbedFS) Open(name string) (fs.File, error) {
|
||||
file, err := f.FS.Open(name)
|
||||
if err != nil {
|
||||
|
|
|
@ -15,6 +15,7 @@ var (
|
|||
random = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
)
|
||||
|
||||
// FileExists checks if a file exists, and returns true if it does
|
||||
func FileExists(filename string) bool {
|
||||
stat, _ := os.Stat(filename)
|
||||
return stat != nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue