From 180d92d5b7632467d61bc9540f60ca811a752746 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Wed, 14 Sep 2022 16:14:18 +0200 Subject: [PATCH] add docker and config for fly.io --- Dockerfile.daemon | 13 +++++++++++ cmd/cli/main.go | 2 +- cmd/daemon/service.go | 23 +++++-------------- fly.toml | 22 ++++++++++++++++++ internal/configuration/configuration.go | 24 +++++++++++++++++++- internal/configuration/configuration_test.go | 2 +- 6 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 Dockerfile.daemon create mode 100644 fly.toml diff --git a/Dockerfile.daemon b/Dockerfile.daemon new file mode 100644 index 0000000..f9b78e3 --- /dev/null +++ b/Dockerfile.daemon @@ -0,0 +1,13 @@ +FROM golang:1.18-alpine + +WORKDIR /app + +COPY go.mod ./ +COPY go.sum ./ +RUN go mod download + +ADD . . + +RUN go build -o /gte-daemon ./cmd/daemon/service.go + +CMD ["/gte-daemon"] diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 90f973e..b7276a0 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -18,7 +18,7 @@ func main() { fmt.Println(err, "could not open config file") os.Exit(1) } - config := configuration.New(configFile) + config := configuration.NewFromFile(configFile) cmd, err := command.Parse(os.Args[1:], config) if err != nil { diff --git a/cmd/daemon/service.go b/cmd/daemon/service.go index 6c1f5b6..8b0d895 100644 --- a/cmd/daemon/service.go +++ b/cmd/daemon/service.go @@ -1,7 +1,6 @@ package main import ( - "flag" "os" "os/signal" "time" @@ -18,21 +17,11 @@ import ( func main() { logger := log.New(os.Stdout) - configPath := flag.String("c", "~/.config/gte/gte.conf", "path to configuration file") - daysAhead := flag.Int("d", 6, "generate for this amount of days from now") - flag.Parse() - - logger.With(log.Fields{ - "config": *configPath, - "daysAhead": *daysAhead, - }).Info("started") - - configFile, err := os.Open(*configPath) - if err != nil { - logger.WithErr(err).Error("could not open config file") - os.Exit(1) - } - config := configuration.New(configFile) + config := configuration.NewFromEnvironment() + cfgCopy := *config + cfgCopy.IMAPPassword = "***" + cfgCopy.SMTPPassword = "***" + logger.WithField("config", cfgCopy).Info("started") msgStore := mstore.NewIMAP(config.IMAP()) mailSend := msend.NewSSLSMTP(config.SMTP()) @@ -40,7 +29,7 @@ func main() { disp := storage.NewDispatcher(mailSend) inboxProc := process.NewInbox(repo) - recurProc := process.NewRecur(repo, disp, *daysAhead) + recurProc := process.NewRecur(repo, disp, config.DaysAhead) go Run(inboxProc, recurProc, logger) diff --git a/fly.toml b/fly.toml new file mode 100644 index 0000000..68abfd7 --- /dev/null +++ b/fly.toml @@ -0,0 +1,22 @@ +# fly.toml file generated for gte-daemon on 2022-09-14T15:53:30+02:00 + +app = "gte-daemon" +kill_signal = "SIGINT" +kill_timeout = 5 +processes = [] + +[build] + dockerfile = "Dockerfile.daemon" + +[env] + IMAP_URL = "imap.fastmail.com:993" + IMAP_FOLDER_PREFIX = "GTE/" + SMTP_URL = "smtp.fastmail.com:465" + GTE_TO_NAME = "gte" + GTE_FROM_NAME = "gte" + GTE_DAYS_AHEAD = 6 + +[experimental] + allowed_public_ports = [] + auto_rollback = true + diff --git a/internal/configuration/configuration.go b/internal/configuration/configuration.go index 9274b59..df4c586 100644 --- a/internal/configuration/configuration.go +++ b/internal/configuration/configuration.go @@ -4,6 +4,8 @@ import ( "bufio" "errors" "io" + "os" + "strconv" "strings" "time" @@ -33,13 +35,14 @@ type Configuration struct { ToAddress string LocalDBPath string + DaysAhead int } type LocalConfiguration struct { MinSyncInterval time.Duration } -func New(src io.Reader) *Configuration { +func NewFromFile(src io.Reader) *Configuration { conf := &Configuration{} scanner := bufio.NewScanner(src) for scanner.Scan() { @@ -81,6 +84,25 @@ func New(src io.Reader) *Configuration { return conf } +func NewFromEnvironment() *Configuration { + days, _ := strconv.Atoi(os.Getenv("GTE_DAYS_AHEAD")) + + return &Configuration{ + IMAPURL: os.Getenv("IMAP_URL"), + IMAPUsername: os.Getenv("IMAP_USER"), + IMAPPassword: os.Getenv("IMAP_PASSWORD"), + IMAPFolderPrefix: os.Getenv("IMAP_FOLDER_PREFIX"), + SMTPURL: os.Getenv("SMTP_URL"), + SMTPUsername: os.Getenv("SMTP_USER"), + SMTPPassword: os.Getenv("SMTP_PASSWORD"), + ToName: os.Getenv("GTE_TO_NAME"), + ToAddress: os.Getenv("GTE_TO_ADDRESS"), + FromName: os.Getenv("GTE_FROM_NAME"), + FromAddress: os.Getenv("GTE_FROM_ADDRESS"), + DaysAhead: days, + } +} + func (c *Configuration) IMAP() *mstore.IMAPConfig { return &mstore.IMAPConfig{ IMAPURL: c.IMAPURL, diff --git a/internal/configuration/configuration_test.go b/internal/configuration/configuration_test.go index 539c6af..e651bc2 100644 --- a/internal/configuration/configuration_test.go +++ b/internal/configuration/configuration_test.go @@ -78,7 +78,7 @@ func TestNew(t *testing.T) { }, } { t.Run(tc.name, func(t *testing.T) { - test.Equals(t, tc.exp, configuration.New(strings.NewReader(tc.source))) + test.Equals(t, tc.exp, configuration.NewFromFile(strings.NewReader(tc.source))) }) } }