-
Notifications
You must be signed in to change notification settings - Fork 7
/
analyticsservice.go
98 lines (84 loc) · 2.54 KB
/
analyticsservice.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package mockimpl
import (
"github.com/couchbaselabs/gocaves/mock"
"github.com/couchbaselabs/gocaves/mock/mockauth"
"github.com/couchbaselabs/gocaves/mock/mockimpl/servers"
)
// analyticsService represents a analytics service running somewhere in the cluster.
type analyticsService struct {
clusterNode *clusterNodeInst
server *servers.HTTPServer
tlsServer *servers.HTTPServer
}
type newAnalyticsServiceOptions struct {
}
func newAnalyticsService(parent *clusterNodeInst, opts newAnalyticsServiceOptions) (*analyticsService, error) {
svc := &analyticsService{
clusterNode: parent,
}
srv, err := servers.NewHTTPServer(servers.NewHTTPServiceOptions{
Name: "analytics",
Handlers: servers.HTTPServerHandlers{
NewRequestHandler: svc.handleNewRequest,
},
})
if err != nil {
return nil, err
}
svc.server = srv
if parent.HasFeature(mock.ClusterNodeFeatureTLS) {
tlsSrv, err := servers.NewHTTPServer(servers.NewHTTPServiceOptions{
Name: "analytics",
Handlers: servers.HTTPServerHandlers{
NewRequestHandler: svc.handleNewRequest,
},
TLSConfig: parent.cluster.tlsConfig,
})
if err != nil {
return nil, err
}
svc.tlsServer = tlsSrv
}
return svc, nil
}
// Node returns the node which owns this service.
func (s *analyticsService) Node() mock.ClusterNode {
return s.clusterNode
}
// Hostname returns the hostname where this service can be accessed.
func (s *analyticsService) Hostname() string {
return "127.0.0.1"
}
// ListenPort returns the port this service is listening on.
func (s *analyticsService) ListenPort() int {
if s.server == nil {
return -1
}
return s.server.ListenPort()
}
// ListenPortTLS returns the TLS port this service is listening on.
func (s *analyticsService) ListenPortTLS() int {
if s.tlsServer == nil {
return -1
}
return s.tlsServer.ListenPort()
}
func (s *analyticsService) handleNewRequest(req *mock.HTTPRequest) *mock.HTTPResponse {
return s.clusterNode.cluster.handleAnalyticsRequest(s, req)
}
// Close will shut down this service once it is no longer needed.
func (s *analyticsService) Close() error {
var errOut error
if s.server != nil {
errOut = s.server.Close()
}
if s.tlsServer != nil {
errOut = s.tlsServer.Close()
}
return errOut
}
// CheckAuthenticated verifies that the currently authenticated user has the specified permissions.
func (s *analyticsService) CheckAuthenticated(permission mockauth.Permission, bucket, scope, collection string,
req *mock.HTTPRequest) bool {
return checkHTTPAuthenticated(permission, bucket, scope, collection, req, s.Node().Cluster().Users())
}