summaryrefslogtreecommitdiffhomepage
path: root/modules/caddyfs/filesystem.go
blob: b2fdcf7a2742db612b3f1d9b59d01b444be5a7ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package caddyfs

import (
	"encoding/json"
	"fmt"
	"io/fs"

	"go.uber.org/zap"

	"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"
)

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.Logger().Debug("unregistering fs", zap.String("fs", f.Key))
			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
}