add email rotation regarding a limit
This commit is contained in:
parent
fcb56f9a7f
commit
730e9f8bde
@ -90,6 +90,7 @@ Les valeurs des variables d'environnement surchargent les valeurs présentes dan
|
||||
| `FAKESMTP_SMTP_MAXRECIPIENTS` | `smtp.maxRecipients` |
|
||||
| `FAKESMTP_SMTP_ALLOWINSECUREAUTH` | `smtp.allowInsecureAuth` |
|
||||
| `FAKESMTP_SMTP_DEBUG` | `smtp.debug` |
|
||||
| `FAKESMTP_DATA_MAX_EMAIL` | `data.maxEmail` |
|
||||
| `FAKESMTP_DATA_PATH` | `data.path` |
|
||||
| `FAKESMTP_RELAY_ENABLED` | `relay.enabled` |
|
||||
| `FAKESMTP_RELAY_ADDRESS` | `relay.address` |
|
||||
|
@ -43,6 +43,11 @@ func getServiceContainer(conf *config.Config) (*service.Container, error) {
|
||||
cqrs.CommandHandlerFunc(command.HandleStoreEmail),
|
||||
)
|
||||
|
||||
bus.RegisterCommand(
|
||||
cqrs.MatchCommandRequest(&command.RotateEmailRequest{}),
|
||||
cqrs.CommandHandlerFunc(command.HandleRotateEmail),
|
||||
)
|
||||
|
||||
bus.RegisterCommand(
|
||||
cqrs.MatchCommandRequest(&command.ClearInboxRequest{}),
|
||||
cqrs.CommandHandlerFunc(command.HandleClearInbox),
|
||||
|
@ -92,6 +92,18 @@ func (s *Session) Data(r io.Reader) error {
|
||||
}
|
||||
}
|
||||
|
||||
if conf.Data.MaxEmail > 0 {
|
||||
cmd := &command.RotateEmailRequest{
|
||||
MaxEmail: conf.Data.MaxEmail,
|
||||
}
|
||||
|
||||
if _, err := bus.Exec(s.ctx, cmd); err != nil {
|
||||
logger.Error(s.ctx, "could not exec command", logger.E(err))
|
||||
|
||||
return errors.Wrapf(err, "could not exec '%T' command", cmd)
|
||||
}
|
||||
}
|
||||
|
||||
cmd := &command.StoreEmailRequest{
|
||||
Envelope: env,
|
||||
}
|
||||
|
44
internal/command/rotate_email.go
Normal file
44
internal/command/rotate_email.go
Normal file
@ -0,0 +1,44 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"forge.cadoles.com/Cadoles/fake-smtp/internal/model"
|
||||
"forge.cadoles.com/Cadoles/fake-smtp/internal/storm"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/cqrs"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
)
|
||||
|
||||
type RotateEmailRequest struct {
|
||||
MaxEmail int
|
||||
}
|
||||
|
||||
func HandleRotateEmail(ctx context.Context, cmd cqrs.Command) error {
|
||||
req, ok := cmd.Request().(*RotateEmailRequest)
|
||||
if !ok {
|
||||
return cqrs.ErrUnexpectedRequest
|
||||
}
|
||||
|
||||
ctn, err := container.From(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not retrieve service container")
|
||||
}
|
||||
|
||||
db, err := storm.From(ctn)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not retrieve storm service")
|
||||
}
|
||||
|
||||
emailcount, err := db.Select().Count(&model.Email{})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not count emails")
|
||||
}
|
||||
|
||||
if emailcount >= req.MaxEmail {
|
||||
var intdiff = emailcount - (req.MaxEmail - 1)
|
||||
db.Select().OrderBy("ID").Limit(intdiff).Delete(&model.Email{})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -51,7 +51,8 @@ type RelayConfig struct {
|
||||
}
|
||||
|
||||
type DataConfig struct {
|
||||
Path string `yaml:"path" env:"FAKESMTP_DATA_PATH"`
|
||||
MaxEmail int `yaml:"maxEmail" env:"FAKESMTP_DATA_MAX_EMAIL"`
|
||||
Path string `yaml:"path" env:"FAKESMTP_DATA_PATH"`
|
||||
}
|
||||
|
||||
// NewFromFile retrieves the configuration from the given file
|
||||
@ -103,7 +104,8 @@ func NewDefault() *Config {
|
||||
Debug: true,
|
||||
},
|
||||
Data: DataConfig{
|
||||
Path: "fakesmtp.db",
|
||||
MaxEmail: 0,
|
||||
Path: "fakesmtp.db",
|
||||
},
|
||||
Relay: RelayConfig{
|
||||
Enabled: false,
|
||||
|
Loading…
x
Reference in New Issue
Block a user