Path: blob/main/components/proxy/plugins/logif/caddyfile.go
2500 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package logif56import (7"github.com/caddyserver/caddy/v2/caddyconfig"8"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"9"go.uber.org/zap/zapcore"10)1112// UnmarshalCaddyfile sets up the module form Caddyfile tokens.13//14// Syntax:15//16// if {17// "<expression>"18// } [<encoder>]19//20// The <expression> must be on a single line.21// Refer to `lang.Lang` for its syntax.22//23// The <encoder> can be one of `json`, `jsonselector`, `console`.24// In case no <encoder> is specified, one between `json` and `console` is set up depending25// on the current environment.26func (ce *ConditionalEncoder) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {27if d.Next() {28if d.Val() != moduleName {29return d.Errf("expecting %s (%T) subdirective", moduleID, ce)30}31var expression string32if !d.Args(&expression) {33return d.Errf("%s (%T) requires an expression", moduleID, ce)34}3536ce.Expr = expression37}3839if !d.Next() {40return nil41}4243// Delegate the parsing of the encoder to the encoder itself44nextDispenser := d.NewFromNextSegment()45if nextDispenser.Next() {46moduleName := nextDispenser.Val()47moduleID := "caddy.logging.encoders." + moduleName48mod, err := caddyfile.UnmarshalModule(nextDispenser, moduleID)49if err != nil {50return err51}52enc, ok := mod.(zapcore.Encoder)53if !ok {54return d.Errf("module %s (%T) is not a zapcore.Encoder", moduleID, mod)55}56ce.EncRaw = caddyconfig.JSONModuleObject(enc, "format", moduleName, nil)57ce.Formatter = moduleName58}5960return nil61}626364