parent
a89b9e7303
commit
832f5b9710
33 changed files with 900 additions and 1086 deletions
cmd/tstor
|
@ -5,6 +5,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
|
||||
"net"
|
||||
nethttp "net/http"
|
||||
|
@ -13,17 +14,13 @@ import (
|
|||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"git.kmsign.ru/royalcat/tstor/daemons/qbittorrent"
|
||||
"git.kmsign.ru/royalcat/tstor/daemons/ytdlp"
|
||||
"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/daemons"
|
||||
"git.kmsign.ru/royalcat/tstor/src/daemon"
|
||||
"git.kmsign.ru/royalcat/tstor/src/delivery"
|
||||
"git.kmsign.ru/royalcat/tstor/src/telemetry"
|
||||
"git.kmsign.ru/royalcat/tstor/src/vfs"
|
||||
"github.com/go-git/go-billy/v5/osfs"
|
||||
"github.com/urfave/cli/v2"
|
||||
|
||||
_ "git.kmsign.ru/royalcat/tstor/pkg/rlog"
|
||||
|
@ -64,7 +61,7 @@ func main() {
|
|||
}
|
||||
|
||||
func run(configPath string) error {
|
||||
conf, err := config.Load(configPath)
|
||||
conf, koanf, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error loading configuration: %w", err)
|
||||
}
|
||||
|
@ -79,6 +76,22 @@ func run(configPath string) error {
|
|||
|
||||
log := rlog.Component("run")
|
||||
|
||||
daemons := []daemon.Daemon{}
|
||||
|
||||
plugins, err := os.ReadDir(conf.DaemonsPluginsDir)
|
||||
for _, v := range plugins {
|
||||
if v.IsDir() {
|
||||
continue
|
||||
}
|
||||
path := filepath.Join(conf.DaemonsPluginsDir, v.Name())
|
||||
dm, err := daemon.LoadFromPlugin(ctx, path, koanf)
|
||||
if err != nil {
|
||||
log.Error(ctx, "error registering plugin daemon", rlog.Error(err))
|
||||
}
|
||||
|
||||
daemons = append(daemons, dm)
|
||||
}
|
||||
|
||||
// TODO make optional
|
||||
// err = syscall.Setpriority(syscall.PRIO_PGRP, 0, 19)
|
||||
// if err != nil {
|
||||
|
@ -89,31 +102,13 @@ func run(configPath string) error {
|
|||
return fmt.Errorf("error creating data folder: %w", err)
|
||||
}
|
||||
|
||||
sourceFs := osfs.New(conf.SourceDir, osfs.WithBoundOS())
|
||||
// tsrv, err := torrent.NewDaemon(sourceFs, conf.Sources.TorrentClient)
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("error creating service: %w", err)
|
||||
// }
|
||||
// sourceFs := vfs.NewCtxBillyFs("/", ctxbilly.WrapFileSystem(osfs.New(conf.SourceDir, osfs.WithBoundOS())))
|
||||
|
||||
err = os.MkdirAll("./ytdlp", 0744)
|
||||
hostedfs, err := daemon.NewHostedFS(vfs.NewOsFs(conf.SourceDir), daemons)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("error creating hosted filesystem: %w", err)
|
||||
}
|
||||
ytdlpsrv, err := ytdlp.NewService("./ytdlp")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
qtdaemon, err := qbittorrent.NewDaemon(conf.Sources.QBittorrent)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating qbittorrent daemon: %w", err)
|
||||
}
|
||||
|
||||
sfs := daemons.NewHostedFS(
|
||||
vfs.NewCtxBillyFs("/", ctxbilly.WrapFileSystem(sourceFs)),
|
||||
qtdaemon, ytdlpsrv,
|
||||
)
|
||||
sfs, err = vfs.WrapLogFS(sfs)
|
||||
hostedfs, err = vfs.WrapLogFS(hostedfs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -130,7 +125,7 @@ func run(configPath string) error {
|
|||
|
||||
if conf.Mounts.Fuse.Enabled {
|
||||
mh := fuse.NewHandler(conf.Mounts.Fuse.AllowOther, conf.Mounts.Fuse.Path)
|
||||
err := mh.Mount(sfs)
|
||||
err := mh.Mount(hostedfs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mount fuse error: %w", err)
|
||||
}
|
||||
|
@ -139,7 +134,7 @@ func run(configPath string) error {
|
|||
|
||||
if conf.Mounts.WebDAV.Enabled {
|
||||
go func() {
|
||||
if err := webdav.NewWebDAVServer(sfs, conf.Mounts.WebDAV.Port, conf.Mounts.WebDAV.User, conf.Mounts.WebDAV.Pass); err != nil {
|
||||
if err := webdav.NewWebDAVServer(hostedfs, conf.Mounts.WebDAV.Port, conf.Mounts.WebDAV.User, conf.Mounts.WebDAV.Pass); err != nil {
|
||||
log.Error(ctx, "error starting webDAV", rlog.Error(err))
|
||||
}
|
||||
|
||||
|
@ -148,7 +143,7 @@ func run(configPath string) error {
|
|||
}
|
||||
if conf.Mounts.HttpFs.Enabled {
|
||||
go func() {
|
||||
httpfs := httpfs.NewHTTPFS(sfs)
|
||||
httpfs := httpfs.NewHTTPFS(hostedfs)
|
||||
addr := fmt.Sprintf("0.0.0.0:%d", conf.Mounts.HttpFs.Port)
|
||||
err = nethttp.ListenAndServe(addr, nethttp.FileServer(httpfs))
|
||||
if err != nil {
|
||||
|
@ -177,7 +172,7 @@ func run(configPath string) error {
|
|||
return
|
||||
}
|
||||
log.Info(ctx, "starting NFS server", slog.String("address", listener.Addr().String()))
|
||||
handler, err := nfs.NewNFSv3Handler(sfs, conf.Mounts.NFS)
|
||||
handler, err := nfs.NewNFSv3Handler(hostedfs, conf.Mounts.NFS)
|
||||
if err != nil {
|
||||
log.Error(ctx, "failed to create NFS handler", rlog.Error(err))
|
||||
return
|
||||
|
@ -199,7 +194,7 @@ func run(configPath string) error {
|
|||
}()
|
||||
|
||||
go func() {
|
||||
err := delivery.Run(qtdaemon, sfs, conf)
|
||||
err := delivery.Run(conf.WebUi, hostedfs, daemons)
|
||||
if err != nil {
|
||||
log.Error(ctx, "error initializing HTTP server", rlog.Error(err))
|
||||
}
|
||||
|
@ -209,8 +204,14 @@ func run(configPath string) error {
|
|||
signal.Notify(sigChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sigChan
|
||||
|
||||
return errors.Join(
|
||||
// tsrv.Close(ctx),
|
||||
qtdaemon.Close(ctx),
|
||||
)
|
||||
errs := []error{}
|
||||
|
||||
for _, dm := range daemons {
|
||||
err := dm.Close(ctx)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue