Skip to content

Commit 384387d

Browse files
committedSep 14, 2023
feat: orphan delete on event and refactor
1 parent eb386b9 commit 384387d

File tree

6 files changed

+65
-41
lines changed

6 files changed

+65
-41
lines changed
 

‎cmd/smtpbridge/main.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/ItsNotGoodName/smtpbridge/internal/database"
1616
"github.com/ItsNotGoodName/smtpbridge/internal/file"
1717
"github.com/ItsNotGoodName/smtpbridge/internal/mailman"
18-
"github.com/ItsNotGoodName/smtpbridge/internal/models"
1918
"github.com/ItsNotGoodName/smtpbridge/internal/repo"
2019
"github.com/ItsNotGoodName/smtpbridge/migrations"
2120
"github.com/ItsNotGoodName/smtpbridge/pkg/secret"
@@ -38,7 +37,11 @@ func main() {
3837
flags := config.WithFlagSet(flag.NewFlagSet(os.Args[0], flag.ExitOnError))
3938

4039
app := lieut.NewSingleCommandApp(
41-
lieut.AppInfo{Name: "smtpbridge", Version: build.Current.Version, Summary: "Bridge email to other messaging services."},
40+
lieut.AppInfo{
41+
Name: "smtpbridge",
42+
Version: build.Current.Version,
43+
Summary: "Bridge email to other messaging services.",
44+
},
4245
run(flags),
4346
flags,
4447
os.Stdout,
@@ -94,16 +97,9 @@ func run(flags *flag.FlagSet) lieut.Executor {
9497
}
9598

9699
// App
97-
webFileStore := app.NewWebFileStore("apple-touch-icon.png", fmt.Sprintf("http://127.0.0.1:%d", cfg.HTTPPort))
98-
app := app.New(db, fileStore, bus, cfg.Config, cfg.EndpointFactory, webFileStore)
99-
100-
// TODO: move this somewhere else
101-
{
102-
release := bus.OnEnvelopeCreated(func(ctx context.Context, evt models.EventEnvelopeCreated) error {
103-
return app.MailmanEnqueue(ctx, evt.ID)
104-
})
105-
defer release()
106-
}
100+
webTestFileStore := app.NewWebTestFileStore("apple-touch-icon.png", fmt.Sprintf("http://127.0.0.1:%d", cfg.HTTPPort))
101+
app, release := app.New(db, fileStore, bus, cfg.Config, cfg.EndpointFactory, webTestFileStore)
102+
defer release()
107103

108104
// Supervisor
109105
super := suture.New("root", suture.Spec{

‎config/config.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
)
2626

2727
const (
28-
TimeFormat12H = "12h"
29-
TimeFormat24H = "24h"
28+
timeFormat12H = "12h"
29+
timeFormat24H = "24h"
3030
)
3131

3232
type Config struct {
@@ -106,7 +106,7 @@ var RawDefault = struct {
106106
HTTPPort uint16 `koanf:"http.port"`
107107
// IMAPPort uint16 `koanf:"imap.port"`
108108
}{
109-
TimeFormat: TimeFormat12H,
109+
TimeFormat: timeFormat12H,
110110
SMTPMaxPayloadSize: "25 MB",
111111
DataDirectory: "smtpbridge_data",
112112
PythonExecutable: "python3",
@@ -115,15 +115,17 @@ var RawDefault = struct {
115115
// IMAPPort: 10143,
116116
}
117117

118+
// flagFlatKeys are remaps of CLI flag keys to work with koanf's parsing.
118119
var flagFlatKeys map[string]string = map[string]string{
119120
"time-format": "time_format",
120121
"data-directory": "data_directory",
121122
"python-executable": "python_executable",
122123
}
123124

125+
// WithFlagSet includes CLI flags with the config reading.
124126
func WithFlagSet(flags *flag.FlagSet) *flag.FlagSet {
125127
flags.String("config", "", flagUsageString("", "Path to config file."))
126-
flags.String("time-format", "", flagUsageString(TimeFormat12H, fmt.Sprintf("Format for time display (%s/%s).", TimeFormat12H, TimeFormat24H)))
128+
flags.String("time-format", "", flagUsageString(timeFormat12H, fmt.Sprintf("Format for time display (%s/%s).", timeFormat12H, timeFormat24H)))
127129
flags.String("data-directory", "", flagUsageString(RawDefault.DataDirectory, "Path to data directory."))
128130
flags.String("python-executable", "", flagUsageString(RawDefault.PythonExecutable, "Python executable."))
129131
flags.Bool("debug", false, flagUsageBool(false, "Run in debug mode."))
@@ -290,9 +292,9 @@ func (p Parser) Parse(raw Raw) (Config, error) {
290292

291293
var timeHourFormat string
292294
switch raw.TimeFormat {
293-
case TimeFormat12H:
295+
case timeFormat12H:
294296
timeHourFormat = helpers.TimeHourFormat12
295-
case TimeFormat24H:
297+
case timeFormat24H:
296298
timeHourFormat = helpers.TimeHourFormat24
297299
default:
298300
return Config{}, fmt.Errorf("invalid time format: %s", raw.TimeFormat)

‎internal/app/app.go

+32-17
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ type FileStore interface {
3030
}
3131

3232
type App struct {
33-
db database.Querier
34-
fileStore FileStore
35-
bus core.Bus
36-
config *models.Config
37-
endpointFactory endpoint.Factory
38-
webFileStore WebFileStore
33+
db database.Querier
34+
fileStore FileStore
35+
bus core.Bus
36+
config *models.Config
37+
endpointFactory endpoint.Factory
38+
webTestFileStore WebTestFileStore
3939
}
4040

4141
func (a App) RuleEndpointsGet(ctx context.Context, id int64) (models.RuleEndpoints, error) {
@@ -103,16 +103,29 @@ func New(
103103
bus core.Bus,
104104
config *models.Config,
105105
endpointFactory endpoint.Factory,
106-
webFileStore WebFileStore,
107-
) App {
108-
return App{
109-
db: db,
110-
fileStore: fileStore,
111-
bus: bus,
112-
config: config,
113-
endpointFactory: endpointFactory,
114-
webFileStore: webFileStore,
106+
webTestFileStore WebTestFileStore,
107+
) (App, func()) {
108+
a := App{
109+
db: db,
110+
fileStore: fileStore,
111+
bus: bus,
112+
config: config,
113+
endpointFactory: endpointFactory,
114+
webTestFileStore: webTestFileStore,
115115
}
116+
117+
return a, a.init()
118+
}
119+
120+
func (a App) init() func() {
121+
return closers(
122+
a.bus.OnEnvelopeCreated(func(ctx context.Context, evt models.EventEnvelopeCreated) error {
123+
return a.MailmanEnqueue(ctx, evt.ID)
124+
}),
125+
a.bus.OnEnvelopeDeleted(func(ctx context.Context, evt models.EventEnvelopeDeleted) error {
126+
return a.AttachmentOrphanDelete(ctx, a.Tracer(trace.SourceApp))
127+
}),
128+
)
116129
}
117130

118131
var ErrorLogin = fmt.Errorf("login invalid")
@@ -187,6 +200,8 @@ func (a App) EnvelopeDelete(ctx context.Context, id int64) error {
187200
return err
188201
}
189202

203+
a.bus.EnvelopeDeleted(ctx)
204+
190205
return nil
191206
}
192207

@@ -301,7 +316,7 @@ func (a App) EndpointTest(ctx context.Context, id int64) error {
301316
return err
302317
}
303318

304-
file, err := a.webFileStore.File()
319+
file, err := a.webTestFileStore.File()
305320
if err != nil {
306321
return err
307322
}
@@ -318,7 +333,7 @@ func (a App) EndpointTest(ctx context.Context, id int64) error {
318333
Text: "Test Body",
319334
}), datt.Attachment)
320335

321-
return end.Send(ctx, a.webFileStore, env)
336+
return end.Send(ctx, a.webTestFileStore, env)
322337
}
323338

324339
func (a App) Tracer(source string) trace.Tracer {

‎internal/app/utils.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package app
2+
3+
func closers(closers ...func()) func() {
4+
return func() {
5+
for _, v := range closers {
6+
v()
7+
}
8+
}
9+
}

‎internal/app/web.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,28 @@ import (
1010
"github.com/ItsNotGoodName/smtpbridge/web"
1111
)
1212

13-
type WebFileStore struct {
13+
// WebTestFileStore is used for testing endpoints.
14+
type WebTestFileStore struct {
1415
name string
1516
url string
1617
}
1718

18-
func NewWebFileStore(name, url string) WebFileStore {
19-
return WebFileStore{
19+
func NewWebTestFileStore(name, url string) WebTestFileStore {
20+
return WebTestFileStore{
2021
name: name,
2122
url: url,
2223
}
2324
}
2425

25-
func (w WebFileStore) File() (fs.File, error) {
26+
func (w WebTestFileStore) File() (fs.File, error) {
2627
return web.FS.Open(w.name)
2728
}
2829

29-
func (w WebFileStore) Reader(ctx context.Context, att models.Attachment) (io.ReadCloser, error) {
30+
func (w WebTestFileStore) Reader(ctx context.Context, att models.Attachment) (io.ReadCloser, error) {
3031
return w.File()
3132
}
3233

33-
func (w WebFileStore) Path(ctx context.Context, att models.Attachment) (string, error) {
34+
func (w WebTestFileStore) Path(ctx context.Context, att models.Attachment) (string, error) {
3435
if w.url == "" {
3536
return "", fmt.Errorf("app: url is empty")
3637
}

‎internal/trace/trace.go

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const (
7676
SourceSMTP = "smtp"
7777
SourceMailman = "mailman"
7878
SourceCron = "cron"
79+
SourceApp = "app"
7980
)
8081

8182
const (

0 commit comments

Comments
 (0)
Please sign in to comment.