gotoolkit service
This commit is contained in:
parent
14792a4234
commit
326457cca3
2 changed files with 58 additions and 11 deletions
3
go.mod
3
go.mod
|
@ -3,6 +3,9 @@ module git.nakama.town/fmartingr/smtp2shoutrrr
|
||||||
go 1.23.3
|
go 1.23.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
git.nakama.town/fmartingr/gotoolkit v0.0.0-00010101000000-000000000000
|
||||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
|
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
|
||||||
github.com/emersion/go-smtp v0.21.3
|
github.com/emersion/go-smtp v0.21.3
|
||||||
)
|
)
|
||||||
|
|
||||||
|
replace git.nakama.town/fmartingr/gotoolkit => ../gotoolkit
|
||||||
|
|
66
main.go
66
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
@ -8,10 +9,38 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.nakama.town/fmartingr/gotoolkit/model"
|
||||||
|
"git.nakama.town/fmartingr/gotoolkit/service"
|
||||||
"github.com/emersion/go-sasl"
|
"github.com/emersion/go-sasl"
|
||||||
"github.com/emersion/go-smtp"
|
"github.com/emersion/go-smtp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ model.Server = (*smtpServer)(nil)
|
||||||
|
|
||||||
|
type smtpServer struct {
|
||||||
|
backend *smtp.Server
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *smtpServer) IsEnabled() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *smtpServer) Start(_ context.Context) error {
|
||||||
|
slog.Info("Started SMTP server", slog.String("addr", s.backend.Addr))
|
||||||
|
return s.backend.ListenAndServe()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *smtpServer) Stop(ctx context.Context) error {
|
||||||
|
slog.Info("Stopping SMTP server")
|
||||||
|
return s.backend.Shutdown(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSMTPServer(backend *smtp.Server) model.Server {
|
||||||
|
return &smtpServer{
|
||||||
|
backend: backend,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The Backend implements SMTP server methods.
|
// The Backend implements SMTP server methods.
|
||||||
type Backend struct{}
|
type Backend struct{}
|
||||||
|
|
||||||
|
@ -79,18 +108,33 @@ func (s *Session) Logout() error {
|
||||||
func main() {
|
func main() {
|
||||||
be := &Backend{}
|
be := &Backend{}
|
||||||
|
|
||||||
s := smtp.NewServer(be)
|
smtpBackend := smtp.NewServer(be)
|
||||||
|
|
||||||
s.Addr = "localhost:11025"
|
smtpBackend.Addr = "localhost:11025"
|
||||||
s.Domain = "localhost"
|
smtpBackend.Domain = "localhost"
|
||||||
s.WriteTimeout = 10 * time.Second
|
smtpBackend.WriteTimeout = 10 * time.Second
|
||||||
s.ReadTimeout = 10 * time.Second
|
smtpBackend.ReadTimeout = 10 * time.Second
|
||||||
s.MaxMessageBytes = 1024 * 1024
|
smtpBackend.MaxMessageBytes = 1024 * 1024
|
||||||
s.MaxRecipients = 50
|
smtpBackend.MaxRecipients = 50
|
||||||
s.AllowInsecureAuth = true
|
smtpBackend.AllowInsecureAuth = true
|
||||||
|
|
||||||
log.Println("Starting server at", s.Addr)
|
ctx := context.Background()
|
||||||
if err := s.ListenAndServe(); err != nil {
|
smtpServer := NewSMTPServer(smtpBackend)
|
||||||
log.Fatal(err)
|
|
||||||
|
svc, err := service.NewService([]model.Server{
|
||||||
|
smtpServer,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Error creating service:", slog.String("err", err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := svc.Start(ctx); err != nil {
|
||||||
|
slog.Error("Error starting service:", slog.String("err", err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := svc.WaitStop(ctx); err != nil {
|
||||||
|
slog.Error("Error waiting for service interruption:", slog.String("err", err.Error()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue