-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.go
More file actions
72 lines (61 loc) · 2.21 KB
/
options.go
File metadata and controls
72 lines (61 loc) · 2.21 KB
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
package lambada
// OutputMode represents the way the request's output will be handled.
// See the defined OutputMode constant to get details on available output modes and how they work.
type OutputMode int8
const (
// Fully manual mode. Neither Content-Type or binary mode will be automatically activated on the responses.
// Binary mode may be manually set on the response writer if needed.
Manual OutputMode = -1
// The Content-Type will automatically be set in the response, if not already provided.
// The binary mode will not be activated automatically and must be set manually when required.
//
// This is the default mode for backward compatibility reasons.
AutoContentType OutputMode = 0
// Fully automatic mode: The Content-Type will be set in the response, if not already provided.
// If the Content-Type indicated binary data, binary mode is also activated automatically.
// Binary mode can still be forcefully manually enabled, but cannot be forced to disabled.
Automatic OutputMode = 1
)
type options struct {
logger Logger
outputMode OutputMode
defaultBinary bool
}
// newOptions creates a new options and applies opts.
// Prior applying opts, the new options are initialized with the zero value for all fields except the loggers, which
// are all initialized with a NullLogger.
func newOptions(opts ...Option) *options {
o := &options{
logger: NullLogger{},
}
o.apply(opts...)
return o
}
// apply applies opts to o, in order.
func (o *options) apply(opts ...Option) {
for _, opt := range opts {
opt(o)
}
}
// An Option used to customize the handler behavior
type Option func(*options)
// WithOutputMode sets the handler's output mode.
// OutputMode may be one of Manual, AutoContentType or Automatic.
func WithOutputMode(outputMode OutputMode) Option {
return func(o *options) {
o.outputMode = outputMode
}
}
// WithLogger sets the handler's logger.
// The logger will be used to log requests, responses, errors, and panics.
func WithLogger(logger Logger) Option {
return func(o *options) {
o.logger = logger
}
}
// WithDefaultBinary enables or disable the default binary mode.
func WithDefaultBinary(defaultBinary bool) Option {
return func(o *options) {
o.defaultBinary = defaultBinary
}
}