forked from phalaaxx/milter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.go
73 lines (59 loc) · 1.63 KB
/
options.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package milter
import (
"log"
"net"
)
// An Option configures a Server using the functional options paradigm
// popularized by Rob Pike.
type Option interface {
apply(*Server)
}
// An ListenerOption configures a Server using the functional options paradigm
// popularized by Rob Pike.
type ListenerOption interface {
lapply(*Server)
}
type optionFunc func(*Server)
func (f optionFunc) apply(Server *Server) { f(Server) }
type loptionFunc func(*Server)
func (f loptionFunc) lapply(Server *Server) { f(Server) }
// WithLogger adds an Logger
func WithLogger(l CustomLogger) Option {
return optionFunc(func(server *Server) {
server.logger = l
})
}
// WithListener adds an Listener
func WithListener(listener net.Listener) ListenerOption {
return loptionFunc(func(server *Server) {
server.listener = listener
})
}
// WithTCPListener e.g. "127.0.0.1:12349"
func WithTCPListener(address string) ListenerOption {
protocol := "tcp"
// bind to listening address
socket, err := net.Listen(protocol, address)
if err != nil {
log.Fatal(err)
}
return WithListener(socket)
}
// WithUnixSocket e.g. "/var/spool/postfix/var/run/milter/milter.sock"
// make sure that the file does not exist!
func WithUnixSocket(file string) ListenerOption {
protocol := "unix"
// bind to listening address
socket, err := net.Listen(protocol, file)
if err != nil {
log.Fatal(err)
}
return WithListener(socket)
}
// WithPanicHandler Adds the error panic handler
// Multiple panic handlers are supported
func WithPanicHandler(handler func(error)) Option {
return optionFunc(func(server *Server) {
server.errHandlers = append(server.errHandlers, handler)
})
}