-
Notifications
You must be signed in to change notification settings - Fork 1
/
external.go
92 lines (73 loc) · 2.9 KB
/
external.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
package external
import (
"fmt"
dphttp "github.com/ONSdigital/dp-net/v2/http"
"net/http"
"github.com/ONSdigital/dp-api-clients-go/v2/dataset"
"github.com/ONSdigital/dp-api-clients-go/v2/filter"
"github.com/ONSdigital/dp-api-clients-go/v2/image"
"github.com/ONSdigital/dp-api-clients-go/v2/files"
"github.com/ONSdigital/dp-download-service/config"
"github.com/ONSdigital/dp-download-service/content"
"github.com/ONSdigital/dp-download-service/downloads"
"github.com/ONSdigital/dp-download-service/service"
"github.com/ONSdigital/dp-healthcheck/healthcheck"
s3client "github.com/ONSdigital/dp-s3"
vault "github.com/ONSdigital/dp-vault"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
)
// External implements the service.Dependencies interface for actual external services.
type External struct{}
var _ service.Dependencies = &External{}
func (*External) DatasetClient(datasetAPIURL string) downloads.DatasetClient {
return dataset.NewAPIClient(datasetAPIURL)
}
func (*External) FilterClient(filterAPIURL string) downloads.FilterClient {
return filter.New(filterAPIURL)
}
func (*External) ImageClient(imageAPIURL string) downloads.ImageClient {
return image.NewAPIClient(imageAPIURL)
}
func (*External) VaultClient(cfg *config.Config) (content.VaultClient, error) {
return vault.CreateClient(cfg.VaultToken, cfg.VaultAddress, 3)
}
func (*External) FilesClient(cfg *config.Config) downloads.FilesClient {
return files.NewAPIClient(cfg.FilesApiURL, cfg.ServiceAuthToken)
}
// S3Client obtains a new S3 client, or a local storage client if a non-empty LocalObjectStore is provided
func (*External) S3Client(cfg *config.Config) (content.S3Client, error) {
if cfg.LocalObjectStore != "" {
s3Config := &aws.Config{
Credentials: credentials.NewStaticCredentials(cfg.MinioAccessKey, cfg.MinioSecretKey, ""),
Endpoint: aws.String(cfg.LocalObjectStore),
Region: aws.String(cfg.AwsRegion),
DisableSSL: aws.Bool(true),
S3ForcePathStyle: aws.Bool(true),
}
s, err := session.NewSession(s3Config)
if err != nil {
return nil, fmt.Errorf("could not create s3 session: %w", err)
}
return s3client.NewClientWithSession(cfg.BucketName, s), nil
}
s3, err := s3client.NewClient(cfg.AwsRegion, cfg.BucketName)
if err != nil {
return nil, fmt.Errorf("could not create s3 client: %w", err)
}
return s3, nil
}
func (*External) HealthCheck(cfg *config.Config, buildTime, gitCommit, version string) (service.HealthChecker, error) {
versionInfo, err := healthcheck.NewVersionInfo(buildTime, gitCommit, version)
if err != nil {
return nil, err
}
hc := healthcheck.New(versionInfo, cfg.HealthCheckCriticalTimeout, cfg.HealthCheckInterval)
return &hc, nil
}
func (*External) HttpServer(cfg *config.Config, r http.Handler) service.HTTPServer {
s := dphttp.NewServer(cfg.BindAddr, r)
s.HandleOSSignals = false
return s
}