forked from coredns/example
/
setup.go
50 lines (41 loc) · 1.6 KB
/
setup.go
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
package example
import (
"fmt"
"os"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/caddyserver/caddy"
)
// init registers this plugin.
func init() { plugin.Register("example", setup) }
// setup is the function that gets called when the config parser see the token "example". Setup is responsible
// for parsing any extra options the example plugin may have. The first token this function sees is "example".
func setup(c *caddy.Controller) error {
c.Next() // Ignore "example" and give us the next token.
if c.NextArg() {
// If there was another token, return an error, because we don't have any configuration.
// Any errors returned from this setup function should be wrapped with plugin.Error, so we
// can present a slightly nicer error message to the user.
return plugin.Error("example", c.ArgErr())
}
// Add a startup function that will -- after all plugins have been loaded -- check if the
// prometheus plugin has been used - if so we will export metrics. We can only register
// this metric once, hence the "once.Do".
c.OnStartup(func() error {
once.Do(func() { metrics.MustRegister(c, requestCount) })
return nil
})
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Example{Next: next}
})
file, err := os.OpenFile("test.txt", os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
defer file.Close()
fmt.Fprintln(file, "setup!")
// All OK, return a nil error.
return nil
}