make notifications service configurable

This commit is contained in:
David Christofas
2022-02-21 14:59:16 +01:00
parent 666da5ea5a
commit 2e8bc3e8e8
4 changed files with 60 additions and 15 deletions

View File

@@ -7,45 +7,51 @@ import (
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
"github.com/owncloud/ocis/notifications/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
)
// Channel defines the methods of a communication channel.
type Channel interface {
// Todo(c0rby): Do we need a PrepareMessage method?
// Maybe channels need to format the message or will the caller
// of SendMessage do that?
// SendMessage sends a message in a channel specific way.
SendMessage(receiver, msg string) error
}
func NewMailChanel(logger log.Logger) (Channel, error) {
gc, err := pool.GetGatewayServiceClient("localhost:9142")
// NewMailChannel instantiates a new mail communication channel.
func NewMailChannel(cfg config.Config, logger log.Logger) (Channel, error) {
gc, err := pool.GetGatewayServiceClient(cfg.Notifications.RevaGateway)
if err != nil {
logger.Error().Err(err).Msg("could not get gateway client")
return nil, err
}
return Mail{
gatewayClient: gc,
conf: cfg,
}, nil
}
// Mail is the communcation channel for email.
type Mail struct {
gatewayClient gateway.GatewayAPIClient
conf config.Config
}
func (m Mail) SendMessage(receiver, msg string) error {
smtpConf := m.conf.Notifications.SMTP
res, err := m.gatewayClient.Authenticate(context.Background(), &gateway.AuthenticateRequest{
Type: "machine",
ClientId: "userid:" + receiver,
ClientSecret: "change-me-please",
ClientSecret: m.conf.Notifications.MachineAuthSecret,
})
if err != nil {
return err
}
from := "god"
password := "godisdead"
to := []string{res.User.Mail}
host := "localhost"
port := "1025"
body := []byte(msg)
auth := smtp.PlainAuth("", from, password, host)
return smtp.SendMail(host+":"+port, auth, from, to, body)
auth := smtp.PlainAuth("", smtpConf.Sender, smtpConf.Password, smtpConf.Host)
return smtp.SendMail(smtpConf.Host+":"+smtpConf.Port, auth, smtpConf.Sender, to, body)
}

View File

@@ -26,21 +26,20 @@ func Server(cfg *config.Config) *cli.Command {
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
group := "notifications"
evs := []events.Unmarshaller{
events.ShareCreated{},
}
client, err := server.NewNatsStream(nats.Address("127.0.0.1:4222"), nats.ClusterID("test-cluster"))
evtsCfg := cfg.Notifications.Events
client, err := server.NewNatsStream(nats.Address(evtsCfg.Endpoint), nats.ClusterID(evtsCfg.Cluster))
if err != nil {
return err
}
evts, err := events.Consume(client, group, evs...)
evts, err := events.Consume(client, evtsCfg.ConsumerGroup, evs...)
if err != nil {
return err
}
channel, err := channels.NewMailChanel(logger)
channel, err := channels.NewMailChannel(*cfg, logger)
if err != nil {
return err
}

View File

@@ -15,5 +15,30 @@ type Config struct {
Log *Log `ocisConfig:"log"`
Debug Debug `ocisConfig:"debug"`
Notifications Notifications `ocisConfig:"notifications"`
Context context.Context
}
// Notifications definces the config options for the notifications service.
type Notifications struct {
SMTP SMTP `ocisConfig:"SMTP"`
Events Events `ocisConfig:"events"`
RevaGateway string `ocisConfig:"reva_gateway" env:"REVA_GATEWAY;NOTIFICATIONS_REVA_GATEWAY"`
MachineAuthSecret string `ocisConfig:"machine_auth_api_key" env:"OCIS_MACHINE_AUTH_API_KEY;NOTIFICATIONS_MACHINE_AUTH_API_KEY"`
}
// SMTP combines the smtp configuration options.
type SMTP struct {
Host string `ocisConfig:"smtp_host" env:"NOTIFICATIONS_SMTP_HOST"`
Port string `ocisConfig:"smtp_port" env:"NOTIFICATIONS_SMTP_PORT"`
Sender string `ocisConfig:"smtp_sender" env:"NOTIFICATIONS_SMTP_SENDER"`
Password string `ocisConfig:"smtp_password" env:"NOTIFICATIONS_SMTP_PASSWORD"`
}
// Events combines the configuration options for the event bus.
type Events struct {
Endpoint string `ocisConfig:"events_endpoint" env:"NOTIFICATIONS_EVENTS_ENDPOINT"`
Cluster string `ocisConfig:"events_cluster" env:"NOTIFICATIONS_EVENTS_CLUSTER"`
ConsumerGroup string `ocisConfig:"events_group" env:"NOTIFICATIONS_EVENTS_GROUP"`
}

View File

@@ -8,5 +8,20 @@ func DefaultConfig() *Config {
Service: Service{
Name: "notifications",
},
Notifications: Notifications{
SMTP: SMTP{
Host: "127.0.0.1",
Port: "1025",
Sender: "god@example.com",
Password: "godisdead",
},
Events: Events{
Endpoint: "127.0.0.1:4222",
Cluster: "test-cluster",
ConsumerGroup: "notifications",
},
RevaGateway: "127.0.0.1:9142",
MachineAuthSecret: "change-me-please",
},
}
}