-
Notifications
You must be signed in to change notification settings - Fork 27
/
service.go
67 lines (55 loc) · 1.81 KB
/
service.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
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
// Adapted for Orb project, modifications licensed under MPL v. 2.0:
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
package sinks
import (
"context"
"github.com/mainflux/mainflux"
mfsdk "github.com/mainflux/mainflux/pkg/sdk/go"
"github.com/ns1labs/orb/pkg/errors"
"github.com/ns1labs/orb/pkg/types"
"github.com/ns1labs/orb/sinks/backend/prometheus"
"go.uber.org/zap"
"time"
)
// PageMetadata contains page metadata that helps navigation
type PageMetadata struct {
Total uint64
Offset uint64 `json:"offset,omitempty"`
Limit uint64 `json:"limit,omitempty"`
Name string `json:"name,omitempty"`
Order string `json:"order,omitempty"`
Dir string `json:"dir,omitempty"`
Metadata types.Metadata `json:"metadata,omitempty"`
Tags types.Tags `json:"tags,omitempty"`
}
var _ SinkService = (*sinkService)(nil)
type sinkService struct {
logger *zap.Logger
// for AuthN/AuthZ
auth mainflux.AuthServiceClient
mfsdk mfsdk.SDK
// Sinks
sinkRepo SinkRepository
}
func (s sinkService) identify(token string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
res, err := s.auth.Identify(ctx, &mainflux.Token{Value: token})
if err != nil {
return "", errors.Wrap(errors.ErrUnauthorizedAccess, err)
}
return res.GetId(), nil
}
func NewSinkService(logger *zap.Logger, auth mainflux.AuthServiceClient, sinkRepo SinkRepository, mfsdk mfsdk.SDK) SinkService {
prometheus.Register()
return &sinkService{
logger: logger,
auth: auth,
sinkRepo: sinkRepo,
mfsdk: mfsdk,
}
}