From f97458b7c0db329be077810fd2ec0d432562510f Mon Sep 17 00:00:00 2001 From: ItsNotGoodName Date: Mon, 3 Jan 2022 01:47:09 -0800 Subject: [PATCH] feat: add int signal and fix logging --- cmd/server.go | 19 ++++++++++++++++++- config/config.go | 2 +- right/repository/database.go | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index d80f48c7..fb1315cf 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -22,6 +22,10 @@ THE SOFTWARE. package cmd import ( + "log" + "os" + "os/signal" + "github.com/ItsNotGoodName/smtpbridge/app" "github.com/ItsNotGoodName/smtpbridge/config" "github.com/ItsNotGoodName/smtpbridge/core" @@ -86,7 +90,20 @@ var serverCmd = &cobra.Command{ // Init and start smtp server smtpBackend := smtp.NewBackend(app) smtpServer := smtp.New(serverConfig, smtpBackend) - smtpServer.Start() + go smtpServer.Start() + + // Wait for interrupt + stop := make(chan os.Signal, 1) + signal.Notify(stop, os.Interrupt) + <-stop + + // Close database + err := db.Close() + if err != nil { + log.Println("error closing database:", err) + } + + log.Println("server stopped") }, } diff --git a/config/config.go b/config/config.go index 30498dfa..8c43b097 100644 --- a/config/config.go +++ b/config/config.go @@ -94,7 +94,7 @@ func New() *Config { func (c *Config) Load() { if err := viper.ReadInConfig(); err != nil { if _, ok := err.(viper.ConfigParseError); ok { - log.Fatalln("config.Load:", err) + log.Fatalln("config.Config.Load:", err) } } diff --git a/right/repository/database.go b/right/repository/database.go index 8e1237a0..0b6f56ee 100644 --- a/right/repository/database.go +++ b/right/repository/database.go @@ -17,7 +17,7 @@ type Database struct { func NewDatabase(cfg *config.Config) Database { db, err := storm.Open(cfg.DB.DB) if err != nil { - log.Fatalln("repository.NewStorm: could not open database:", err) + log.Fatalln("repository.NewDatabase: could not open database:", err) } attachment := NewAttachment(cfg, db)