From f51b449c6937eec2bddcd3936f790006736d7bc7 Mon Sep 17 00:00:00 2001 From: Alberto Bertogli Date: Sun, 17 Sep 2023 10:58:25 +0100 Subject: [PATCH] chasquid: Move the certificate loading logic in a separate function This patch moves the top-level certificate loading logic out of main and into a separate function. This is only for readability and consistency with how we handle domains (which have a similar structure already). There are no logic changes. --- chasquid.go | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/chasquid.go b/chasquid.go index c1ade7d..6e4f1ec 100644 --- a/chasquid.go +++ b/chasquid.go @@ -99,25 +99,7 @@ func main() { name := info.Name() dir := filepath.Join("certs/", name) - log.Infof(" %s", name) - - // Ignore directories that don't have both keys. - // We warn about this because it can be hard to debug otherwise. - certPath := filepath.Join(dir, "fullchain.pem") - if _, err := os.Stat(certPath); err != nil { - log.Infof(" skipping: %v", err) - continue - } - keyPath := filepath.Join(dir, "privkey.pem") - if _, err := os.Stat(keyPath); err != nil { - log.Infof(" skipping: %v", err) - continue - } - - err := s.AddCerts(certPath, keyPath) - if err != nil { - log.Fatalf(" %v", err) - } + loadCert(name, dir, s) } // Load domains from "domains/". @@ -265,6 +247,29 @@ func signalHandler(dinfo *domaininfo.DB, srv *smtpsrv.Server) { } } +// Helper to load a single certificate configuration into the server. +func loadCert(name, dir string, s *smtpsrv.Server) { + log.Infof(" %s", name) + + // Ignore directories that don't have both keys. + // We warn about this because it can be hard to debug otherwise. + certPath := filepath.Join(dir, "fullchain.pem") + if _, err := os.Stat(certPath); err != nil { + log.Infof(" skipping: %v", err) + return + } + keyPath := filepath.Join(dir, "privkey.pem") + if _, err := os.Stat(keyPath); err != nil { + log.Infof(" skipping: %v", err) + return + } + + err := s.AddCerts(certPath, keyPath) + if err != nil { + log.Fatalf(" %v", err) + } +} + // Helper to load a single domain configuration into the server. func loadDomain(name, dir string, s *smtpsrv.Server) { log.Infof(" %s", name)