mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-22 17:10:10 +00:00
Harvey Tindall
72cf3e2240
"make all" will build with internal data, whereas "make debug"/"make all-external" will make an external "data/" directory.
33 lines
666 B
Go
33 lines
666 B
Go
package main
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// Since the gin-static middleware uses a version of http.Filesystem with an extra Exists() func, we extend it here.
|
|
|
|
type httpFS struct {
|
|
hfs http.FileSystem // Created by converting fs.FS using http.FS()
|
|
fs fs.FS
|
|
}
|
|
|
|
func (f httpFS) Open(name string) (http.File, error) {
|
|
return f.hfs.Open("data/web" + name)
|
|
}
|
|
|
|
func (f httpFS) Exists(prefix string, filepath string) bool {
|
|
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
|
|
stats, err := fs.Stat(f.fs, "data/web/"+p)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if stats.IsDir() {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
return false
|
|
}
|