chore: Refactor code to use SourceUpdater struct for managing sources
This commit is contained in:
parent
fa1fdcfc63
commit
99cdd5471e
12 changed files with 328 additions and 263 deletions
cmd/tstor
|
@ -12,18 +12,17 @@ import (
|
|||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.kmsign.ru/royalcat/tstor/pkg/ctxbilly"
|
||||
wnfs "git.kmsign.ru/royalcat/tstor/pkg/go-nfs"
|
||||
"git.kmsign.ru/royalcat/tstor/pkg/rlog"
|
||||
"git.kmsign.ru/royalcat/tstor/src/config"
|
||||
"git.kmsign.ru/royalcat/tstor/src/delivery"
|
||||
"git.kmsign.ru/royalcat/tstor/src/host"
|
||||
"git.kmsign.ru/royalcat/tstor/src/host/datastorage"
|
||||
"git.kmsign.ru/royalcat/tstor/src/host/service"
|
||||
"git.kmsign.ru/royalcat/tstor/src/host/store"
|
||||
"git.kmsign.ru/royalcat/tstor/src/host/vfs"
|
||||
"git.kmsign.ru/royalcat/tstor/src/telemetry"
|
||||
"github.com/go-git/go-billy/v5/osfs"
|
||||
"github.com/urfave/cli/v2"
|
||||
|
||||
_ "git.kmsign.ru/royalcat/tstor/pkg/rlog"
|
||||
|
@ -86,93 +85,22 @@ func run(configPath string) error {
|
|||
log.Error(ctx, "set priority failed", rlog.Error(err))
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(conf.TorrentClient.MetadataFolder, 0744); err != nil {
|
||||
return fmt.Errorf("error creating metadata folder: %w", err)
|
||||
if err := os.MkdirAll(conf.SourceDir, 0744); err != nil {
|
||||
return fmt.Errorf("error creating data folder: %w", err)
|
||||
}
|
||||
|
||||
fis, err := store.NewFileItemStore(filepath.Join(conf.TorrentClient.MetadataFolder, "items"), 2*time.Hour)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error starting item store: %w", err)
|
||||
}
|
||||
defer fis.Close()
|
||||
|
||||
id, err := store.GetOrCreatePeerID(filepath.Join(conf.TorrentClient.MetadataFolder, "ID"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating node ID: %w", err)
|
||||
}
|
||||
|
||||
st, _, err := datastorage.Setup(conf.TorrentClient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer st.Close()
|
||||
|
||||
excludedFilesStore, err := store.NewFileMappings(conf.TorrentClient.MetadataFolder, st)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
infoBytesStore, err := store.NewInfoBytes(conf.TorrentClient.MetadataFolder)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c, err := store.NewClient(st, fis, &conf.TorrentClient, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error starting torrent client: %w", err)
|
||||
}
|
||||
c.AddDhtNodes(conf.TorrentClient.DHTNodes)
|
||||
defer c.Close()
|
||||
|
||||
ts, err := service.NewService(
|
||||
conf.SourceDir, conf.TorrentClient,
|
||||
c, st, excludedFilesStore, infoBytesStore,
|
||||
)
|
||||
sourceFs := osfs.New(conf.SourceDir, osfs.WithBoundOS())
|
||||
srv, err := service.New(sourceFs, conf.TorrentClient)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating service: %w", err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(conf.SourceDir, 0744); err != nil {
|
||||
return fmt.Errorf("error creating data folder: %w", err)
|
||||
}
|
||||
sfs := host.NewTorrentStorage(conf.SourceDir, ts)
|
||||
sfs := host.NewTorrentStorage(
|
||||
vfs.NewCtxBillyFs("/", ctxbilly.WrapFileSystem(sourceFs)),
|
||||
srv,
|
||||
)
|
||||
sfs = vfs.WrapLogFS(sfs)
|
||||
|
||||
// TODO make separate function
|
||||
// {
|
||||
// if st, ok := st.(storage.FileStorageDeleter); ok {
|
||||
// log.Info().Msg("listing files")
|
||||
// files, err := listFilesRecursive(conf.SourceDir)
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("error listing files: %w", err)
|
||||
// }
|
||||
|
||||
// torrentFiles := []string{}
|
||||
// for _, v := range files {
|
||||
// if strings.HasSuffix(v, ".torrent") {
|
||||
// torrentFiles = append(torrentFiles, v)
|
||||
// }
|
||||
// }
|
||||
|
||||
// log.Info().Int("count", len(torrentFiles)).Msg("loading torrent files")
|
||||
// torrentList := []*torrent.Torrent{}
|
||||
// for _, tf := range torrentFiles {
|
||||
// t, err := c.AddTorrentFromFile(tf)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// <-t.GotInfo()
|
||||
// torrentList = append(torrentList, t)
|
||||
// }
|
||||
// log.Info().Msg("staring cleanup")
|
||||
// err = st.Cleanup(torrentList)
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("cleanup error: %w", err)
|
||||
// }
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
if conf.Mounts.Fuse.Enabled {
|
||||
mh := fuse.NewHandler(conf.Mounts.Fuse.AllowOther, conf.Mounts.Fuse.Path)
|
||||
err := mh.Mount(sfs)
|
||||
|
@ -246,7 +174,7 @@ func run(configPath string) error {
|
|||
go func() {
|
||||
logFilename := filepath.Join(conf.Log.Path, "logs")
|
||||
|
||||
err := delivery.New(nil, service.NewStats(), ts, sfs, logFilename, conf)
|
||||
err := delivery.New(nil, service.NewStats(), srv, sfs, logFilename, conf)
|
||||
if err != nil {
|
||||
log.Error(ctx, "error initializing HTTP server", rlog.Error(err))
|
||||
}
|
||||
|
@ -256,5 +184,5 @@ func run(configPath string) error {
|
|||
signal.Notify(sigChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sigChan
|
||||
|
||||
return ts.Close()
|
||||
return srv.Close(ctx)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue