Skip to content

Commit

Permalink
chasquid: Move the certificate loading logic in a separate function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
albertito committed Sep 17, 2023
1 parent b9e222f commit f51b449
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions chasquid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/".
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f51b449

Please sign in to comment.