Skip to content

Commit

Permalink
feat: add int signal and fix logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Jan 3, 2022
1 parent 21dd94e commit f97458b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
19 changes: 18 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
},
}

Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion right/repository/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f97458b

Please sign in to comment.