Newer
Older
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
package gatcaddyfile
import (
"encoding/json"
"fmt"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"gfx.cafe/gfx/pggat/lib/util/maps"
)
const (
Discoverer = "pggat.handlers.discovery.discoverers"
Handler = "pggat.handlers"
Matcher = "pggat.matchers"
Pooler = "pggat.poolers"
SSLServer = "pggat.ssl.servers"
SSLClient = "pggat.ssl.clients"
)
var unmarshallers maps.TwoKey[string, string, Unmarshaller]
func RegisterDirective(namespace, directive string, unmarshaller Unmarshaller) {
if _, ok := unmarshallers.Load(namespace, directive); ok {
panic(fmt.Sprintf(`directive "%s" already exists`, directive))
}
unmarshallers.Store(namespace, directive, unmarshaller)
}
func LookupDirective(namespace, directive string) (Unmarshaller, bool) {
return unmarshallers.Load(namespace, directive)
}
func UnmarshalDirectiveJSONModuleObject(
d *caddyfile.Dispenser,
namespace string,
inlineKey string,
warnings *[]caddyconfig.Warning,
) (json.RawMessage, error) {
unmarshaller, ok := LookupDirective(namespace, d.Val())
if !ok {
return nil, d.Errf(`unknown directive in %s: "%s"`, namespace, d.Val())
}
return unmarshaller.JSONModuleObject(
d,
namespace,
inlineKey,
warnings,
)
}