1
0

Use inotify fs watcher. Also shorten errors msg for loggers instantiation.

This commit is contained in:
Alexander Andreev 2022-02-08 00:26:59 +04:00
parent 9a4461ac4e
commit e45bb01f49
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F

View File

@ -5,6 +5,8 @@ import (
"dwelling-upload/internal/handlers" "dwelling-upload/internal/handlers"
"dwelling-upload/pkg/logging" "dwelling-upload/pkg/logging"
"dwelling-upload/pkg/server" "dwelling-upload/pkg/server"
"dwelling-upload/pkg/utils"
"dwelling-upload/pkg/watcher"
"flag" "flag"
"log" "log"
"net/http" "net/http"
@ -56,23 +58,41 @@ func main() {
logErr, err := logging.NewLogger(config.Log.Error, config.Log.ToStdout) logErr, err := logging.NewLogger(config.Log.Error, config.Log.ToStdout)
if err != nil { if err != nil {
log.Fatalln("failed to open error logger:", err) log.Fatalln("error logger:", err)
} }
defer logErr.Close() defer logErr.Close()
logUpload, err := logging.NewLogger(config.Log.Upload, config.Log.ToStdout) logUpload, err := logging.NewLogger(config.Log.Upload, config.Log.ToStdout)
if err != nil { if err != nil {
log.Fatalln("failed to open upload logger:", err) log.Fatalln("upload logger:", err)
} }
defer logUpload.Close() defer logUpload.Close()
logDownload, err := logging.NewLogger(config.Log.Download, config.Log.ToStdout) logDownload, err := logging.NewLogger(config.Log.Download, config.Log.ToStdout)
if err != nil { if err != nil {
log.Fatalln("failed to open download logger:", err) log.Fatalln("download logger:", err)
} }
defer logDownload.Close() defer logDownload.Close()
hand := handlers.NewUploadHandlers(config, logErr, logUpload, logDownload) watcha, err := watcher.NewFSWatcher()
if err != nil {
logErr.Fatalln(err)
}
defer watcha.Close()
if err := watcha.AddWatch(config.Uploads.Directory, watcher.CrDelMask); err != nil {
logErr.Fatalln(err)
}
uploadDirNotify := make(chan uint32)
uploadDirSize, err := utils.DirectorySize(config.Uploads.Directory)
if err != nil {
logErr.Fatalln(err)
}
watcha.WatchForMask(uploadDirNotify, watcher.CrDelMask)
hand := handlers.NewUploadHandlers(config, logErr, logUpload, logDownload, &uploadDirSize)
srv := server.NewHttpServer() srv := server.NewHttpServer()
srv.SetNotFoundHandler(hand.NotFound) srv.SetNotFoundHandler(hand.NotFound)
@ -88,9 +108,22 @@ func main() {
doneSignal := make(chan os.Signal, 1) doneSignal := make(chan os.Signal, 1)
signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-doneSignal for {
select {
case <-doneSignal:
if err := srv.Stop(); err != nil { if err := srv.Stop(); err != nil {
logErr.Fatalln(err) logErr.Fatalln(err)
} }
case <-uploadDirNotify:
sz, err := utils.DirectorySize(config.Uploads.Directory)
if err != nil {
logErr.Println(err)
}
if sz > 0 {
uploadDirSize = sz
}
}
}
} }