mirror of
https://github.com/caddyserver/caddy.git
synced 2026-01-17 01:30:34 +00:00
events: Refactor; move Event into core, so core can emit events (#6930)
* events: Refactor; move Event into core, so core can emit events Requires some slight trickery to invert dependencies. We can't have the caddy package import the caddyevents package, because caddyevents imports caddy. Interface to the rescue! Also add two new events, experimentally: started, and stopping. At the request of a sponsor. Also rename "Filesystems" to "FileSystems" to match Go convention (unrelated to events, was just bugging me when I noticed it). * Coupla bug fixes * lol whoops
This commit is contained in:
@@ -7,10 +7,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultFilesystemKey = "default"
|
||||
DefaultFileSystemKey = "default"
|
||||
)
|
||||
|
||||
var DefaultFilesystem = &wrapperFs{key: DefaultFilesystemKey, FS: OsFS{}}
|
||||
var DefaultFileSystem = &wrapperFs{key: DefaultFileSystemKey, FS: OsFS{}}
|
||||
|
||||
// wrapperFs exists so can easily add to wrapperFs down the line
|
||||
type wrapperFs struct {
|
||||
@@ -18,24 +18,24 @@ type wrapperFs struct {
|
||||
fs.FS
|
||||
}
|
||||
|
||||
// FilesystemMap stores a map of filesystems
|
||||
// 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 {
|
||||
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 {
|
||||
func (f *FileSystemMap) key(k string) string {
|
||||
if k == "" {
|
||||
k = DefaultFilesystemKey
|
||||
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) {
|
||||
func (f *FileSystemMap) Register(k string, v fs.FS) {
|
||||
k = f.key(k)
|
||||
if v == nil {
|
||||
f.Unregister(k)
|
||||
@@ -47,23 +47,23 @@ func (f *FilesystemMap) Register(k string, v fs.FS) {
|
||||
// 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) {
|
||||
func (f *FileSystemMap) Unregister(k string) {
|
||||
k = f.key(k)
|
||||
if k == DefaultFilesystemKey {
|
||||
f.m.Store(k, DefaultFilesystem)
|
||||
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) {
|
||||
func (f *FileSystemMap) Get(k string) (v fs.FS, ok bool) {
|
||||
k = f.key(k)
|
||||
c, ok := f.m.Load(strings.TrimSpace(k))
|
||||
if !ok {
|
||||
if k == DefaultFilesystemKey {
|
||||
f.m.Store(k, DefaultFilesystem)
|
||||
return DefaultFilesystem, true
|
||||
if k == DefaultFileSystemKey {
|
||||
f.m.Store(k, DefaultFileSystem)
|
||||
return DefaultFileSystem, true
|
||||
}
|
||||
return nil, ok
|
||||
}
|
||||
@@ -71,7 +71,7 @@ func (f *FilesystemMap) Get(k string) (v fs.FS, ok bool) {
|
||||
}
|
||||
|
||||
// Default will get the default filesystem in the filesystem map
|
||||
func (f *FilesystemMap) Default() fs.FS {
|
||||
val, _ := f.Get(DefaultFilesystemKey)
|
||||
func (f *FileSystemMap) Default() fs.FS {
|
||||
val, _ := f.Get(DefaultFileSystemKey)
|
||||
return val
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user