Improve folder umount
Signed-off-by: Antonio Navarro Perez <antnavper@gmail.com>
This commit is contained in:
parent
11d501cd51
commit
256b1bd205
1 changed files with 39 additions and 20 deletions
|
@ -19,7 +19,8 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/shurcooL/httpfs/html/vfstemplate"
|
"github.com/shurcooL/httpfs/html/vfstemplate"
|
||||||
log "github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
ginlogrus "github.com/toorop/gin-logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -31,28 +32,33 @@ func main() {
|
||||||
configPath = os.Args[1]
|
configPath = os.Args[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SetFormatter(&log.TextFormatter{})
|
log := logrus.New()
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetFormatter(&logrus.TextFormatter{})
|
||||||
|
log.SetLevel(logrus.InfoLevel)
|
||||||
|
|
||||||
f, err := ioutil.ReadFile(configPath)
|
f, err := ioutil.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Fatal("error reading configuration file")
|
log.WithError(err).Error("error reading configuration file")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conf := &config.Root{}
|
conf := &config.Root{}
|
||||||
if err := yaml.Unmarshal(f, conf); err != nil {
|
if err := yaml.Unmarshal(f, conf); err != nil {
|
||||||
log.WithError(err).Fatal("error parsing configuration file")
|
log.WithError(err).Error("error parsing configuration file")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conf = config.AddDefaults(conf)
|
conf = config.AddDefaults(conf)
|
||||||
|
|
||||||
if err := os.MkdirAll(conf.MetadataFolder, 0770); err != nil {
|
if err := os.MkdirAll(conf.MetadataFolder, 0770); err != nil {
|
||||||
log.WithError(err).Fatal("error creating metadata folder")
|
log.WithError(err).Error("error creating metadata folder")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fc, err := filecache.NewCache(conf.MetadataFolder)
|
fc, err := filecache.NewCache(conf.MetadataFolder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Fatal("error creating cache")
|
log.WithError(err).Error("error creating cache")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fc.SetCapacity(conf.MaxCacheSize * 1024 * 1024)
|
fc.SetCapacity(conf.MaxCacheSize * 1024 * 1024)
|
||||||
|
@ -67,39 +73,41 @@ func main() {
|
||||||
|
|
||||||
c, err := torrent.NewClient(torrentCfg)
|
c, err := torrent.NewClient(torrentCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Fatal("error initializing torrent client")
|
log.WithError(err).Error("error initializing torrent client")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ss := stats.NewTorrent()
|
ss := stats.NewTorrent()
|
||||||
mountService := mount.NewHandler(c, ss)
|
mountService := mount.NewHandler(c, ss)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
tryClose(log, c, mountService)
|
||||||
|
}()
|
||||||
|
|
||||||
sigChan := make(chan os.Signal)
|
sigChan := make(chan os.Signal)
|
||||||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-sigChan
|
<-sigChan
|
||||||
log.Info("closing torrent client...")
|
tryClose(log, c, mountService)
|
||||||
c.Close()
|
|
||||||
log.Info("unmounting fuse filesystem...")
|
|
||||||
mountService.Close()
|
|
||||||
|
|
||||||
log.Info("exiting")
|
|
||||||
os.Exit(1)
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for _, mp := range conf.MountPoints {
|
for _, mp := range conf.MountPoints {
|
||||||
if err := mountService.Mount(mp); err != nil {
|
if err := mountService.Mount(mp); err != nil {
|
||||||
log.WithError(err).WithField("path", mp).Fatal("error mounting folder")
|
log.WithError(err).WithField("path", mp.Path).Error("error mounting folder")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.New()
|
||||||
|
|
||||||
|
r.Use(ginlogrus.Logger(log), gin.Recovery())
|
||||||
|
|
||||||
assets := distribyted.NewBinaryFileSystem(distribyted.HttpFS, "/assets")
|
assets := distribyted.NewBinaryFileSystem(distribyted.HttpFS, "/assets")
|
||||||
r.Use(static.Serve("/assets", assets))
|
r.Use(static.Serve("/assets", assets))
|
||||||
|
|
||||||
t, err := vfstemplate.ParseGlob(distribyted.HttpFS, nil, "/templates/*")
|
t, err := vfstemplate.ParseGlob(distribyted.HttpFS, nil, "/templates/*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Fatal("error parsing html template")
|
log.WithError(err).Error("error parsing html template")
|
||||||
}
|
}
|
||||||
|
|
||||||
r.SetHTMLTemplate(t)
|
r.SetHTMLTemplate(t)
|
||||||
|
@ -130,6 +138,17 @@ func main() {
|
||||||
|
|
||||||
//TODO add port from configuration
|
//TODO add port from configuration
|
||||||
if err := r.Run(":4444"); err != nil {
|
if err := r.Run(":4444"); err != nil {
|
||||||
log.WithError(err).Fatal("error initializing server")
|
log.WithError(err).Error("error initializing server")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tryClose(log *logrus.Logger, c *torrent.Client, mountService *mount.Handler) {
|
||||||
|
log.Info("closing torrent client...")
|
||||||
|
c.Close()
|
||||||
|
log.Info("unmounting fuse filesystem...")
|
||||||
|
mountService.Close()
|
||||||
|
|
||||||
|
log.Info("exiting")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue