split watcher and http server from sender + add golanci config

This commit is contained in:
2023-09-10 21:59:57 +02:00
parent d831c3e0aa
commit 3c4b740849
8 changed files with 401 additions and 126 deletions
+17 -10
View File
@@ -20,30 +20,29 @@ type Email struct {
}
func FromJSON(path string) (Email, error) {
var mail Email
var email Email
content, err := os.ReadFile(path)
if err != nil {
return mail, fmt.Errorf("%w, unable to read the file: %s", err, path)
return email, fmt.Errorf("%w, unable to read the file: %s", err, path)
}
if err := json.Unmarshal(content, &mail); err != nil {
return mail, err
if err := json.Unmarshal(content, &email); err != nil {
return email, err
}
validate = validator.New(validator.WithRequiredStructEnabled())
if err := validate.Struct(mail); err != nil {
return mail, err
if err := email.Validate(); err != nil {
return email, err
}
return mail, nil
return email, nil
}
func (e Email) GetReceivers() []string {
func (e *Email) GetReceivers() []string {
return strings.Split(e.Receivers, ",")
}
func (e Email) Generate() []byte {
func (e *Email) Generate() []byte {
mail := fmt.Sprintf(
"To: %s\nFrom: %s\nContent-Type: text/html;charset=utf-8\nSubject: %s\n\n%s",
e.Receivers,
@@ -53,3 +52,11 @@ func (e Email) Generate() []byte {
)
return []byte(mail)
}
func (e *Email) Validate() error {
validate = validator.New(validator.WithRequiredStructEnabled())
if err := validate.Struct(e); err != nil {
return err
}
return nil
}
-1
View File
@@ -56,5 +56,4 @@ func TestFromJson(t *testing.T) {
assert.Contains(t, err.Error(), "validation for 'Sender'")
})
}