/
service.go
115 lines (101 loc) · 2.87 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package sessrv
import (
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/lab259/http"
)
// SESServiceConfiguration is the configuration for the `SESService`
type SESServiceConfiguration struct {
Endpoint string `yaml:"endpoint"`
Region string `yaml:"region"`
Key string `yaml:"key"`
Secret string `yaml:"secret"`
}
type credentialsFromStruct struct {
credentials *SESServiceConfiguration
}
func NewCredentialsFromStruct(credentials *SESServiceConfiguration) *credentialsFromStruct {
return &credentialsFromStruct{
credentials: credentials,
}
}
func (c *credentialsFromStruct) Retrieve() (credentials.Value, error) {
return credentials.Value{
AccessKeyID: c.credentials.Key,
SecretAccessKey: c.credentials.Secret,
}, nil
}
func (*credentialsFromStruct) IsExpired() bool {
return false
}
// SESService is the service which manages a service queue on the AWS.
type SESService struct {
running bool
awsSES *ses.SES
Configuration SESServiceConfiguration
}
// LoadConfiguration returns a not implemented error.
//
// This happens because this method should be implemented on the struct that
// will use this implementation.
func (service *SESService) LoadConfiguration() (interface{}, error) {
return nil, errors.New("not implemented")
}
// ApplyConfiguration applies a given configuration to the service.
func (service *SESService) ApplyConfiguration(configuration interface{}) error {
switch c := configuration.(type) {
case SESServiceConfiguration:
service.Configuration = c
return nil
case *SESServiceConfiguration:
service.Configuration = *c
return nil
}
return http.ErrWrongConfigurationInformed
}
// Restart stops and then starts the service again.
func (service *SESService) Restart() error {
if service.running {
err := service.Stop()
if err != nil {
return err
}
}
return service.Start()
}
// Start initializes the aws client with the configuration.
func (service *SESService) Start() error {
if !service.running {
conf := aws.Config{
Credentials: credentials.NewCredentials(NewCredentialsFromStruct(&service.Configuration)),
Region: aws.String(service.Configuration.Region),
}
sess, err := session.NewSessionWithOptions(session.Options{
Config: conf,
})
if err != nil {
return err
}
service.awsSES = ses.New(sess)
service.running = true
}
return nil
}
// Stop erases the aws client reference.
func (service *SESService) Stop() error {
if service.running {
service.awsSES = nil
service.running = false
}
return nil
}
// RunWithSQS runs a handler passing the reference of a `sqs.SQS` client.
func (service *SESService) RunWithSES(handler func(client *ses.SES) error) error {
if service.running {
return handler(service.awsSES)
}
return http.ErrServiceNotRunning
}