Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/proxy/plugins/logif/caddyfile.go
2500 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package logif
6
7
import (
8
"github.com/caddyserver/caddy/v2/caddyconfig"
9
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
10
"go.uber.org/zap/zapcore"
11
)
12
13
// UnmarshalCaddyfile sets up the module form Caddyfile tokens.
14
//
15
// Syntax:
16
//
17
// if {
18
// "<expression>"
19
// } [<encoder>]
20
//
21
// The <expression> must be on a single line.
22
// Refer to `lang.Lang` for its syntax.
23
//
24
// The <encoder> can be one of `json`, `jsonselector`, `console`.
25
// In case no <encoder> is specified, one between `json` and `console` is set up depending
26
// on the current environment.
27
func (ce *ConditionalEncoder) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
28
if d.Next() {
29
if d.Val() != moduleName {
30
return d.Errf("expecting %s (%T) subdirective", moduleID, ce)
31
}
32
var expression string
33
if !d.Args(&expression) {
34
return d.Errf("%s (%T) requires an expression", moduleID, ce)
35
}
36
37
ce.Expr = expression
38
}
39
40
if !d.Next() {
41
return nil
42
}
43
44
// Delegate the parsing of the encoder to the encoder itself
45
nextDispenser := d.NewFromNextSegment()
46
if nextDispenser.Next() {
47
moduleName := nextDispenser.Val()
48
moduleID := "caddy.logging.encoders." + moduleName
49
mod, err := caddyfile.UnmarshalModule(nextDispenser, moduleID)
50
if err != nil {
51
return err
52
}
53
enc, ok := mod.(zapcore.Encoder)
54
if !ok {
55
return d.Errf("module %s (%T) is not a zapcore.Encoder", moduleID, mod)
56
}
57
ce.EncRaw = caddyconfig.JSONModuleObject(enc, "format", moduleName, nil)
58
ce.Formatter = moduleName
59
}
60
61
return nil
62
}
63
64