Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
logging: Implement net writer (#2884)
* Implement UDP writer * Implement Net Writer * Utilize Caddy's address parsing functions * A couple little fixes (see #2884)
Abdelmalek Ihdene committed
Dec 15, 2019 at 14:58 UTC
c3bcd967bd3be85c41ac5f630d496f0dc0d18115
4 files changed
+93
-4
admin.go
+6
@@ -711,6 +711,12 @@ traverseLoop:
711
return fmt.Errorf("unrecognized method %s", method)
712
}
713
} else {
714
+ // if we are "PUTting" a new resource, the key(s) in its path
715
+ // might not exist yet; that's OK but we need to make them as
716
+ // we go, while we still have a pointer from the level above
717
+ if v[part] == nil && method == http.MethodPut {
718
+ v[part] = make(map[string]interface{})
719
+ }
720
ptr = v[part]
721
}
722
caddy.go
+1
-1
@@ -134,7 +134,7 @@ func changeConfig(method, path string, input []byte, forceReload bool) error {
134
135
// if nothing changed, no need to do a whole reload unless the client forces it
136
if !forceReload && bytes.Equal(rawCfgJSON, newCfg) {
137
- Log().Named("admin.api.change_config").Info("config is unchanged")
137
+ Log().Named("admin.api").Info("config is unchanged")
138
return nil
139
}
140
logging.go
+4
-3
@@ -225,10 +225,11 @@ func (logging *Logging) openWriter(opener WriterOpener) (io.WriteCloser, bool, e
225
w, err := opener.OpenWriter()
226
return writerDestructor{w}, err
227
})
228
- if err == nil {
229
- logging.writerKeys = append(logging.writerKeys, key)
228
+ if err != nil {
229
+ return nil, false, err
230
}
231
- return writer.(io.WriteCloser), !loaded, err
231
+ logging.writerKeys = append(logging.writerKeys, key)
232
+ return writer.(io.WriteCloser), !loaded, nil
233
}
234
235
// WriterOpener is a module that can open a log writer.
modules/logging/netwriter.go
new
+82
@@ -0,0 +1,82 @@
1
+// Copyright 2015 Matthew Holt and The Caddy Authors
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License");
4
+// you may not use this file except in compliance with the License.
5
+// You may obtain a copy of the License at
6
+//
7
+// http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS,
11
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+// See the License for the specific language governing permissions and
13
+// limitations under the License.
14
+
15
+package logging
16
+
17
+import (
18
+ "fmt"
19
+ "io"
20
+ "net"
21
+
22
+ "github.com/caddyserver/caddy/v2"
23
+)
24
+
25
+func init() {
26
+ caddy.RegisterModule(NetWriter{})
27
+}
28
+
29
+// NetWriter implements a log writer that outputs to a network socket.
30
+type NetWriter struct {
31
+ Address string `json:"address,omitempty"`
32
+
33
+ addr caddy.ParsedAddress
34
+}
35
+
36
+// CaddyModule returns the Caddy module information.
37
+func (NetWriter) CaddyModule() caddy.ModuleInfo {
38
+ return caddy.ModuleInfo{
39
+ ID: "caddy.logging.writers.net",
40
+ New: func() caddy.Module { return new(NetWriter) },
41
+ }
42
+}
43
+
44
+// Provision sets up the module.
45
+func (nw *NetWriter) Provision(ctx caddy.Context) error {
46
+ repl := caddy.NewReplacer()
47
+ address, err := repl.ReplaceOrErr(nw.Address, true, true)
48
+ if err != nil {
49
+ return fmt.Errorf("invalid host in address: %v", err)
50
+ }
51
+
52
+ nw.addr, err = caddy.ParseNetworkAddress(address)
53
+ if err != nil {
54
+ return fmt.Errorf("parsing network address '%s': %v", address, err)
55
+ }
56
+
57
+ if nw.addr.PortRangeSize() != 1 {
58
+ return fmt.Errorf("multiple ports not supported")
59
+ }
60
+
61
+ return nil
62
+}
63
+
64
+func (nw NetWriter) String() string {
65
+ return nw.addr.String()
66
+}
67
+
68
+// WriterKey returns a unique key representing this nw.
69
+func (nw NetWriter) WriterKey() string {
70
+ return nw.addr.String()
71
+}
72
+
73
+// OpenWriter opens a new network connection.
74
+func (nw NetWriter) OpenWriter() (io.WriteCloser, error) {
75
+ return net.Dial(nw.addr.Network, nw.addr.JoinHostPort(0))
76
+}
77
+
78
+// Interface guards
79
+var (
80
+ _ caddy.Provisioner = (*NetWriter)(nil)
81
+ _ caddy.WriterOpener = (*NetWriter)(nil)
82
+)