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
+16 -10
View File
@@ -7,45 +7,51 @@ import (
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool" "github.com/cs3org/reva/pkg/rgrpc/todo/pool"
"github.com/owncloud/ocis/notifications/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/log"
) )
// Channel defines the methods of a communication channel.
type Channel interface { 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 SendMessage(receiver, msg string) error
} }
func NewMailChanel(logger log.Logger) (Channel, error) { // NewMailChannel instantiates a new mail communication channel.
gc, err := pool.GetGatewayServiceClient("localhost:9142") func NewMailChannel(cfg config.Config, logger log.Logger) (Channel, error) {
gc, err := pool.GetGatewayServiceClient(cfg.Notifications.RevaGateway)
if err != nil { if err != nil {
logger.Error().Err(err).Msg("could not get gateway client") logger.Error().Err(err).Msg("could not get gateway client")
return nil, err return nil, err
} }
return Mail{ return Mail{
gatewayClient: gc, gatewayClient: gc,
conf: cfg,
}, nil }, nil
} }
// Mail is the communcation channel for email.
type Mail struct { type Mail struct {
gatewayClient gateway.GatewayAPIClient gatewayClient gateway.GatewayAPIClient
conf config.Config
} }
func (m Mail) SendMessage(receiver, msg string) error { func (m Mail) SendMessage(receiver, msg string) error {
smtpConf := m.conf.Notifications.SMTP
res, err := m.gatewayClient.Authenticate(context.Background(), &gateway.AuthenticateRequest{ res, err := m.gatewayClient.Authenticate(context.Background(), &gateway.AuthenticateRequest{
Type: "machine", Type: "machine",
ClientId: "userid:" + receiver, ClientId: "userid:" + receiver,
ClientSecret: "change-me-please", ClientSecret: m.conf.Notifications.MachineAuthSecret,
}) })
if err != nil { if err != nil {
return err return err
} }
from := "god"
password := "godisdead"
to := []string{res.User.Mail} to := []string{res.User.Mail}
host := "localhost"
port := "1025"
body := []byte(msg) body := []byte(msg)
auth := smtp.PlainAuth("", from, password, host) auth := smtp.PlainAuth("", smtpConf.Sender, smtpConf.Password, smtpConf.Host)
return smtp.SendMail(smtpConf.Host+":"+smtpConf.Port, auth, smtpConf.Sender, to, body)
return smtp.SendMail(host+":"+port, auth, from, to, body)
} }
+4 -5
View File
@@ -26,21 +26,20 @@ func Server(cfg *config.Config) *cli.Command {
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log) logger := logging.Configure(cfg.Service.Name, cfg.Log)
group := "notifications"
evs := []events.Unmarshaller{ evs := []events.Unmarshaller{
events.ShareCreated{}, 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 { if err != nil {
return err return err
} }
evts, err := events.Consume(client, group, evs...) evts, err := events.Consume(client, evtsCfg.ConsumerGroup, evs...)
if err != nil { if err != nil {
return err return err
} }
channel, err := channels.NewMailChanel(logger) channel, err := channels.NewMailChannel(*cfg, logger)
if err != nil { if err != nil {
return err return err
} }
+25
View File
@@ -15,5 +15,30 @@ type Config struct {
Log *Log `ocisConfig:"log"` Log *Log `ocisConfig:"log"`
Debug Debug `ocisConfig:"debug"` Debug Debug `ocisConfig:"debug"`
Notifications Notifications `ocisConfig:"notifications"`
Context context.Context 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"`
}
+15
View File
@@ -8,5 +8,20 @@ func DefaultConfig() *Config {
Service: Service{ Service: Service{
Name: "notifications", 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",
},
} }
} }