• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cshum / imagor / 4142091150

10 Feb 2023 08:24AM UTC coverage: 90.999% (-0.2%) from 91.179%
4142091150

Pull #282

github

Adrian Shum
refactor interfaces
Pull Request #282: feat(server): Add optional Prometheus metrics server

67 of 67 new or added lines in 4 files covered. (100.0%)

5530 of 6077 relevant lines covered (91.0%)

1.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.22
/server/option.go
1
package server
2

3
import (
4
        "net/http"
5
        "time"
6

7
        "github.com/rs/cors"
8
        "go.uber.org/zap"
9
)
10

11
// Option Server option
12
type Option func(s *Server)
13

14
// WithAddr with server address with port option
15
func WithAddr(addr string) Option {
1✔
16
        return func(s *Server) {
2✔
17
                s.Addr = addr
1✔
18
        }
1✔
19
}
20

21
// WithAddress with server address option
22
func WithAddress(address string) Option {
1✔
23
        return func(s *Server) {
2✔
24
                s.Address = address
1✔
25
        }
1✔
26
}
27

28
// WithPort with port option
29
func WithPort(port int) Option {
1✔
30
        return func(s *Server) {
2✔
31
                s.Port = port
1✔
32
        }
1✔
33
}
34

35
// WithLogger with logger option
36
func WithLogger(logger *zap.Logger) Option {
1✔
37
        return func(s *Server) {
2✔
38
                if logger != nil {
2✔
39
                        s.Logger = logger
1✔
40
                }
1✔
41
        }
42
}
43

44
// WithMiddleware with HTTP middleware option
45
func WithMiddleware(middleware func(http.Handler) http.Handler) Option {
1✔
46
        return func(s *Server) {
2✔
47
                if middleware != nil {
2✔
48
                        s.Handler = middleware(s.Handler)
1✔
49
                }
1✔
50
        }
51
}
52

53
// WithPathPrefix with path prefix option
54
func WithPathPrefix(prefix string) Option {
1✔
55
        return func(s *Server) {
2✔
56
                s.PathPrefix = prefix
1✔
57
        }
1✔
58
}
59

60
// WithCORS with CORS option
61
func WithCORS(enabled bool) Option {
1✔
62
        return func(s *Server) {
2✔
63
                if enabled {
2✔
64
                        s.Handler = cors.Default().Handler(s.Handler)
1✔
65
                }
1✔
66
        }
67
}
68

69
// WithDebug with debug option
70
func WithDebug(debug bool) Option {
1✔
71
        return func(s *Server) {
2✔
72
                s.Debug = debug
1✔
73
        }
1✔
74
}
75

76
// WithStartupTimeout with server startup timeout option
77
func WithStartupTimeout(timeout time.Duration) Option {
1✔
78
        return func(s *Server) {
2✔
79
                if timeout > 0 {
2✔
80
                        s.StartupTimeout = timeout
1✔
81
                }
1✔
82
        }
83
}
84

85
// WithShutdownTimeout with server shutdown timeout option
86
func WithShutdownTimeout(timeout time.Duration) Option {
1✔
87
        return func(s *Server) {
2✔
88
                if timeout > 0 {
2✔
89
                        s.ShutdownTimeout = timeout
1✔
90
                }
1✔
91
        }
92
}
93

94
// WithStripQueryString with strip query string option
95
func WithStripQueryString(enabled bool) Option {
1✔
96
        return func(s *Server) {
2✔
97
                if enabled {
2✔
98
                        s.Handler = stripQueryStringHandler(s.Handler)
1✔
99
                }
1✔
100
        }
101
}
102

103
// WithAccessLog with server access log option
104
func WithAccessLog(enabled bool) Option {
1✔
105
        return func(s *Server) {
2✔
106
                if enabled {
2✔
107
                        s.Handler = s.accessLogHandler(s.Handler)
1✔
108
                }
1✔
109
        }
110
}
111

112
// WithMetrics with server metrics option
113
func WithMetrics(metrics Metrics) Option {
×
114
        return func(s *Server) {
×
115
                s.Metrics = metrics
×
116
        }
×
117
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc