Skip to content

Commit

Permalink
feat(config): change email config via http configuration endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fwiedmann committed Jul 23, 2022
1 parent 8f6795e commit cb8a502
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 80 deletions.
48 changes: 32 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,42 @@ Configuration:
"username": "",
"password": "",
"from_email_address": ""
},
"email_receiver_config": {
"alert_subject": "",
"resolve_subject": "",
"receivers": [
}
}
```

In order to configure your e-mail alert receiver you have to send the following to the dynamic e-mail config endpoint

Example data:

```json
[
{
"name": "Family",
"alert_subject": "ALERT",
"resolve_subject": "✅ Resolve",
"alert_template_message": "Hi {{ .Name }},\n ... ",
"resolve_template_message": "Hi {{ .Name }},\n ... ",
"addresses": [
{
"name": "",
"alert_template_message": "",
"resolve_template_message": "",
"addresses": [
{
"email": "",
"name": "",
"surname": ""
}
]
"email": "felix@example.com",
"name": "Felix",
"surname": "Wiedmann"
}
]
}
}
]
```

Create or Update the current config:

```bash
curl -X PUT <IP of your gocrazy host>:8080/email-config -d @config.json
```

Get current config:
```bash
curl -X GET <IP of your gocrazy host>:8080/email-config
```

## Circuit
Expand Down
19 changes: 17 additions & 2 deletions cmd/icof-cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import (
"github.com/fwiedmann/icof/pkg/gocrazy-permanent-data"
"github.com/fwiedmann/icof/pkg/notifier"
pi "github.com/fwiedmann/icof/pkg/raspberry-pi"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"gopkg.in/gomail.v2"
"net/http"
"time"
)

const httpServerPort = ":8080"

func main() {
pinAlert, err := pi.NewGpioPin(
pi.PinAlertConfig{
Expand All @@ -28,22 +32,33 @@ func main() {
if err != nil {
panic(err)
}

logger := setupLogger()

dynamicEmailConfig, err := gocrazy_permanent_data.NewDynamicEmailConfig(logger)
if err != nil {
panic(err)
}

emailNotifier, err := notifier.NewEmailClient(
&notifier.EmailClientConfig{
Sender: gomail.NewDialer(c.EmailClientConfig.Host, c.EmailClientConfig.Port, c.EmailClientConfig.Username, c.EmailClientConfig.Password),
FromEmailAddress: c.EmailClientConfig.FromEmailAddress,
},
c,
dynamicEmailConfig,
logger,
)

if err != nil {
panic(err)
}

go func() {
router := mux.NewRouter()
router.HandleFunc("/email-config", dynamicEmailConfig.GetConfigHandler()).Methods(http.MethodGet)
router.HandleFunc("/email-config", dynamicEmailConfig.CreatOrUpdateConfig()).Methods(http.MethodPut)
panic(http.ListenAndServe(httpServerPort, router))
}()

panic(icof.Run(context.Background(), icof.Config{
Observer: pinAlert,
Notifiers: []icof.Notifier{emailNotifier},
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ go 1.17
require (
github.com/go-playground/validator/v10 v10.10.0
github.com/golang/mock v1.6.0
github.com/gorilla/mux v1.8.0
github.com/sirupsen/logrus v1.8.1
go.etcd.io/bbolt v1.3.6
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
periph.io/x/conn/v3 v3.6.10
periph.io/x/host/v3 v3.7.2
Expand All @@ -14,10 +16,14 @@ require (
require (
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/gokrazy/firmware v0.0.0-20220723040627-bc9f0ac355ae // indirect
github.com/gokrazy/gokrazy v0.0.0-20220619152026-e92d116fc1be // indirect
github.com/gokrazy/kernel v0.0.0-20220723043759-49aa44e74501 // indirect
github.com/gokrazy/rpi-eeprom v0.0.0-20220723040153-8c98dac71b8d // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/sys v0.0.0-20220222172238-00053529121e // indirect
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
)
31 changes: 31 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/beevik/ntp v0.3.0 h1:xzVrPrE4ziasFXgBVBZJDP0Wg/KpMwk2KHJ4Ba8GrDw=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -10,10 +11,24 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/gokrazy/firmware v0.0.0-20220723040627-bc9f0ac355ae h1:rGSbxI4VikunVnAimfXOivvUEqpCPCCzqwXCzAoeJ+A=
github.com/gokrazy/firmware v0.0.0-20220723040627-bc9f0ac355ae/go.mod h1:nbEte1HMYHF4is+rAadA57SWBONRn7N3kXmafjkusFY=
github.com/gokrazy/gokrazy v0.0.0-20220619152026-e92d116fc1be h1:Zc3Bj/9n/mMIhbdbfVUU+iGDnm3YKAo5JupPH7tSm+s=
github.com/gokrazy/gokrazy v0.0.0-20220619152026-e92d116fc1be/go.mod h1:pgBUUZOVv30VhSGOqT5ygjRRE+GSPPWMUFyvyV4tNiI=
github.com/gokrazy/internal v0.0.0-20220129150711-9ed298107648 h1:kBuLicM0xJw3xEe4607WlnzGL+qSwPqdyh5/LUiCdq0=
github.com/gokrazy/kernel v0.0.0-20220723043759-49aa44e74501 h1:wjOApDvKcZaTpdmD1SSIYL0x03yLNLOSXlVLjOPn2c4=
github.com/gokrazy/kernel v0.0.0-20220723043759-49aa44e74501/go.mod h1:9bunbYi8kxZow8AQJl4SiPmglzemYA/f3zrLgKrTmVI=
github.com/gokrazy/rpi-eeprom v0.0.0-20220723040153-8c98dac71b8d h1:TzL50aM8/cwPM+5MoL2TtkN2Dq1+6WvxXLXhq1Rg8p4=
github.com/gokrazy/rpi-eeprom v0.0.0-20220723040153-8c98dac71b8d/go.mod h1:vabxV1M+i6S3rGuWoFieHxCJW3jlob3rqe0KV82j+0o=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
github.com/google/renameio/v2 v2.0.0 h1:UifI23ZTGY8Tt29JbYFiuyIU3eX+RNFtUwefq9qAhxg=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/josharian/native v1.0.0 h1:Ts/E8zCSEsG17dUqv7joXJFybuMLjQfWE04tsBODTxk=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
Expand All @@ -22,19 +37,27 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/mdlayher/packet v1.0.0 h1:InhZJbdShQYt6XV2GPj5XHxChzOfhJJOMbvnGAmOfQ8=
github.com/mdlayher/socket v0.2.3 h1:XZA2X2TjdOwNoNPVPclRCURoX/hokBY8nkTmRZFEheM=
github.com/mdlayher/watchdog v0.0.0-20201005150459-8bdc4f41966b h1:7tUBfsEEBWfFeHOB7CUfoOamak+Gx/BlirfXyPk1WjI=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rtr7/dhcp4 v0.0.0-20220302171438-18c84d089b46 h1:3psQveH4RUiv5yc3p7kRySilf1nSXLQhAvJFwg4fgnE=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 h1:gga7acRE695APm9hlsSMoOoE65U4/TcqNj90mc69Rlg=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU=
go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
Expand All @@ -44,18 +67,25 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220222172238-00053529121e h1:AGLQ2aegkB2Y9RY8YdQk+7MDCW9da7YmizIwNIt8NtQ=
golang.org/x/sys v0.0.0-20220222172238-00053529121e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down Expand Up @@ -83,3 +113,4 @@ periph.io/x/conn/v3 v3.6.10/go.mod h1:UqWNaPMosWmNCwtufoTSTTYhB2wXWsMRAJyo1PlxO4
periph.io/x/d2xx v0.0.4/go.mod h1:38Euaaj+s6l0faIRHh32a+PrjXvxFTFkPBEQI0TKg34=
periph.io/x/host/v3 v3.7.2 h1:rCAUxkzy2xrzh18HP2AoVwTL/fEKqmcJ1icsZQGM58Q=
periph.io/x/host/v3 v3.7.2/go.mod h1:nHMlzkPwmnHyP9Tn0I8FV+e0N3K7TjFXLZkIWzAicog=
rsc.io/goversion v1.2.0 h1:SPn+NLTiAG7w30IRK/DKp1BjvpWabYgxlLp/+kx5J8w=
53 changes: 1 addition & 52 deletions pkg/gocrazy-permanent-data/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package gocrazy_permanent_data

import (
"context"
"encoding/json"
"github.com/fwiedmann/icof/pkg/notifier"
"io/ioutil"
)

Expand All @@ -28,8 +26,7 @@ func New() (StartUpConfig, error) {

// StartUpConfig holds all required properties for icof to start
type StartUpConfig struct {
EmailClientConfig EmailClientConfig `json:"email_config"`
EmailReceiverConfig EmailReceiverConfig `json:"email_receiver_config"`
EmailClientConfig EmailClientConfig `json:"email_config"`
}

type EmailClientConfig struct {
Expand All @@ -39,51 +36,3 @@ type EmailClientConfig struct {
Password string `json:"password"`
FromEmailAddress string `json:"from_email_address"`
}

type EmailReceiverConfig struct {
AlertSubject string `json:"alert_subject"`
ResolveSubject string `json:"resolve_subject"`
Receivers []EmailReceiver `json:"receivers"`
}

type EmailReceiver struct {
Name string `json:"name"`
AlertTemplateMessage string `json:"alert_template_message"`
ResolveTemplateMessage string `json:"resolve_template_message"`
Addresses []EmailAddress `json:"addresses"`
}

type EmailAddress struct {
Email string `json:"email"`
Name string `json:"name"`
Surname string `json:"surname"`
}

// GetEmailReceivers implements the notifier.EmailReceiverRepository
func (c StartUpConfig) GetEmailReceivers(ctx context.Context) ([]notifier.EmailReceiver, error) {
config, err := New()
if err != nil {
return nil, err
}

receivers := make([]notifier.EmailReceiver, 0)
for _, receiver := range config.EmailReceiverConfig.Receivers {
addresses := make([]notifier.Address, 0)
for _, address := range receiver.Addresses {
addresses = append(addresses, notifier.Address{
Email: address.Email,
Name: address.Name,
Surname: address.Surname,
})
}
receivers = append(receivers, notifier.EmailReceiver{
Name: receiver.Name,
AlertSubject: config.EmailReceiverConfig.AlertSubject,
ResolveSubject: config.EmailReceiverConfig.ResolveSubject,
AlertTemplateMessage: receiver.AlertTemplateMessage,
ResolveTemplateMessage: receiver.ResolveTemplateMessage,
Addresses: addresses,
})
}
return receivers, nil
}
Loading

0 comments on commit cb8a502

Please sign in to comment.