-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
333 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ var directiveOrder = []string{ | |
|
||
"map", | ||
"vars", | ||
"fs", | ||
"root", | ||
"skip_log", | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package filesystems | ||
|
||
import ( | ||
"io/fs" | ||
"sync" | ||
) | ||
|
||
const ( | ||
DefaultFilesystemKey = "default" | ||
) | ||
|
||
var ( | ||
DefaultFilesystem = &wrapperFs{key: DefaultFilesystemKey, FS: OsFS{}} | ||
) | ||
|
||
// wrapperFs exists so can easily add to wrapperFs down the line | ||
type wrapperFs struct { | ||
key string | ||
fs.FS | ||
} | ||
|
||
// FilesystemMap stores a map of filesystems | ||
// the empty key will be overwritten to be the default key | ||
// it includes a default filesystem, based off the os fs | ||
type FilesystemMap struct { | ||
m sync.Map | ||
} | ||
|
||
// note that the first invocation of key cannot be called in a racy context. | ||
func (f *FilesystemMap) key(k string) string { | ||
if k == "" { | ||
k = DefaultFilesystemKey | ||
} | ||
return k | ||
} | ||
|
||
// Register will add the filesystem with key to later be retrieved | ||
// A call with a nil fs will call unregister, ensuring that a call to Default() will never be nil | ||
func (f *FilesystemMap) Register(k string, v fs.FS) { | ||
k = f.key(k) | ||
if v == nil { | ||
f.Unregister(k) | ||
return | ||
} | ||
f.m.Store(k, &wrapperFs{key: k, FS: v}) | ||
} | ||
|
||
// Unregister will remove the filesystem with key from the filesystem map | ||
// if the key is the default key, it will set the default to the osFS instead of deleting it | ||
// modules should call this on cleanup to be safe | ||
func (f *FilesystemMap) Unregister(k string) { | ||
k = f.key(k) | ||
if k == DefaultFilesystemKey { | ||
f.m.Store(k, DefaultFilesystem) | ||
} else { | ||
f.m.Delete(k) | ||
} | ||
} | ||
|
||
// Get will get a filesystem with a given key | ||
func (f *FilesystemMap) Get(k string) (v fs.FS, ok bool) { | ||
k = f.key(k) | ||
c, ok := f.m.Load(k) | ||
if !ok { | ||
if k == DefaultFilesystemKey { | ||
f.m.Store(k, DefaultFilesystem) | ||
return DefaultFilesystem, true | ||
} | ||
return nil, ok | ||
} | ||
return c.(fs.FS), false | ||
} | ||
|
||
// Default will get the default filesystem in the filesystem map | ||
func (f *FilesystemMap) Default() fs.FS { | ||
val, _ := f.Get(DefaultFilesystemKey) | ||
return val | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package filesystems | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// OsFS is a simple fs.FS implementation that uses the local | ||
// file system. (We do not use os.DirFS because we do our own | ||
// rooting or path prefixing without being constrained to a single | ||
// root folder. The standard os.DirFS implementation is problematic | ||
// since roots can be dynamic in our application.) | ||
// | ||
// OsFS also implements fs.StatFS, fs.GlobFS, fs.ReadDirFS, and fs.ReadFileFS. | ||
type OsFS struct{} | ||
|
||
func (OsFS) Open(name string) (fs.File, error) { return os.Open(name) } | ||
func (OsFS) Stat(name string) (fs.FileInfo, error) { return os.Stat(name) } | ||
func (OsFS) Glob(pattern string) ([]string, error) { return filepath.Glob(pattern) } | ||
func (OsFS) ReadDir(name string) ([]fs.DirEntry, error) { return os.ReadDir(name) } | ||
func (OsFS) ReadFile(name string) ([]byte, error) { return os.ReadFile(name) } | ||
|
||
var ( | ||
_ fs.StatFS = (*OsFS)(nil) | ||
_ fs.GlobFS = (*OsFS)(nil) | ||
_ fs.ReadDirFS = (*OsFS)(nil) | ||
_ fs.ReadFileFS = (*OsFS)(nil) | ||
) |
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package caddyfs | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/fs" | ||
|
||
"github.com/caddyserver/caddy/v2" | ||
"github.com/caddyserver/caddy/v2/caddyconfig" | ||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" | ||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func init() { | ||
caddy.RegisterModule(Filesystems{}) | ||
httpcaddyfile.RegisterGlobalOption("filesystem", parseFilesystems) | ||
} | ||
|
||
type moduleEntry struct { | ||
Key string `json:"name,omitempty"` | ||
FileSystemRaw json.RawMessage `json:"file_system,omitempty" caddy:"namespace=caddy.fs inline_key=backend"` | ||
fileSystem fs.FS | ||
} | ||
|
||
// Filesystems loads caddy.fs modules into the global filesystem map | ||
type Filesystems struct { | ||
Filesystems []*moduleEntry `json:"filesystems"` | ||
|
||
defers []func() | ||
} | ||
|
||
func parseFilesystems(d *caddyfile.Dispenser, existingVal any) (any, error) { | ||
p := &Filesystems{} | ||
current, ok := existingVal.(*Filesystems) | ||
if ok { | ||
p = current | ||
} | ||
x := &moduleEntry{} | ||
err := x.UnmarshalCaddyfile(d) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p.Filesystems = append(p.Filesystems, x) | ||
return p, nil | ||
} | ||
|
||
// CaddyModule returns the Caddy module information. | ||
func (Filesystems) CaddyModule() caddy.ModuleInfo { | ||
return caddy.ModuleInfo{ | ||
ID: "caddy.filesystems", | ||
New: func() caddy.Module { return new(Filesystems) }, | ||
} | ||
} | ||
|
||
func (xs *Filesystems) Start() error { return nil } | ||
func (xs *Filesystems) Stop() error { return nil } | ||
|
||
func (xs *Filesystems) Provision(ctx caddy.Context) error { | ||
// load the filesystem module | ||
for _, f := range xs.Filesystems { | ||
if len(f.FileSystemRaw) > 0 { | ||
mod, err := ctx.LoadModule(f, "FileSystemRaw") | ||
if err != nil { | ||
return fmt.Errorf("loading file system module: %v", err) | ||
} | ||
f.fileSystem = mod.(fs.FS) | ||
} | ||
// register that module | ||
ctx.Logger().Debug("registering fs", zap.String("fs", f.Key)) | ||
ctx.Filesystems().Register(f.Key, f.fileSystem) | ||
// remember to unregister the module when we are done | ||
xs.defers = append(xs.defers, func() { | ||
ctx.Filesystems().Unregister(f.Key) | ||
}) | ||
} | ||
return nil | ||
|
||
} | ||
|
||
func (f *Filesystems) Cleanup() error { | ||
for _, v := range f.defers { | ||
v() | ||
} | ||
return nil | ||
} | ||
|
||
func (f *moduleEntry) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { | ||
for d.Next() { | ||
// key required for now | ||
if !d.Args(&f.Key) { | ||
return d.ArgErr() | ||
} | ||
// get the module json | ||
if !d.NextArg() { | ||
return d.ArgErr() | ||
} | ||
name := d.Val() | ||
modID := "caddy.fs." + name | ||
unm, err := caddyfile.UnmarshalModule(d, modID) | ||
if err != nil { | ||
return err | ||
} | ||
fsys, ok := unm.(fs.FS) | ||
if !ok { | ||
return d.Errf("module %s (%T) is not a supported file system implementation (requires fs.FS)", modID, unm) | ||
} | ||
f.FileSystemRaw = caddyconfig.JSONModuleObject(fsys, "backend", name, nil) | ||
} | ||
return nil | ||
} |
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.