Refactor structure, use cobra
This commit is contained in:
69
internal/app.go
Normal file
69
internal/app.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
"github.com/debridmediamanager.com/zurg/internal/net"
|
||||
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
||||
"github.com/debridmediamanager.com/zurg/internal/universal"
|
||||
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/logutil"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/utils"
|
||||
"github.com/dgraph-io/ristretto"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
)
|
||||
|
||||
func MainApp(configPath string) {
|
||||
log := logutil.NewLogger()
|
||||
zurglog := log.Named("zurg")
|
||||
|
||||
config, configErr := config.LoadZurgConfig(configPath, log.Named("config"))
|
||||
if configErr != nil {
|
||||
zurglog.Errorf("Config failed to load: %v", configErr)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
apiClient := zurghttp.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), config.GetRealDebridTimeout(), config, log.Named("httpclient"))
|
||||
|
||||
rd := realdebrid.NewRealDebrid(apiClient, log.Named("realdebrid"))
|
||||
|
||||
p, err := ants.NewPool(config.GetNumOfWorkers())
|
||||
if err != nil {
|
||||
zurglog.Errorf("Failed to create worker pool: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer p.Release()
|
||||
|
||||
utils.EnsureDirExists("data")
|
||||
|
||||
cache, err := ristretto.NewCache(&ristretto.Config{
|
||||
NumCounters: 1e5, // 200,000 to track frequency for 100,000 items.
|
||||
MaxCost: 100 << 20, // maximum cost of cache (100MB).
|
||||
BufferItems: 1 << 10, // number of keys per Get buffer, can be adjusted.
|
||||
})
|
||||
if err != nil {
|
||||
zurglog.Errorf("Failed to create cache: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
torrentMgr := torrent.NewTorrentManager(config, rd, p, cache, log.Named("manager"))
|
||||
|
||||
downloadClient := zurghttp.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), 0, config, log.Named("dlclient"))
|
||||
getfile := universal.NewGetFile(downloadClient)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
net.Router(mux, getfile, config, torrentMgr, log.Named("net"))
|
||||
|
||||
addr := fmt.Sprintf("%s:%s", config.GetHost(), config.GetPort())
|
||||
server := &http.Server{Addr: addr, Handler: mux}
|
||||
|
||||
zurglog.Infof("Starting server on %s", addr)
|
||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
zurglog.Errorf("Failed to start server: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
7
internal/network.go
Normal file
7
internal/network.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package internal
|
||||
|
||||
import "github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||
|
||||
func NetworkTest() {
|
||||
realdebrid.RunTest()
|
||||
}
|
||||
@@ -40,7 +40,6 @@ type TorrentManager struct {
|
||||
antsPool *ants.Pool
|
||||
unrestrictPool *ants.Pool
|
||||
log *zap.SugaredLogger
|
||||
mu *sync.Mutex
|
||||
}
|
||||
|
||||
// NewTorrentManager creates a new torrent manager
|
||||
@@ -55,7 +54,6 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p
|
||||
requiredVersion: "18.11.2023",
|
||||
antsPool: p,
|
||||
log: log,
|
||||
mu: &sync.Mutex{},
|
||||
}
|
||||
|
||||
unrestrictPool, err := ants.NewPool(t.Config.GetUnrestrictWorkers())
|
||||
@@ -65,8 +63,6 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p
|
||||
t.unrestrictPool = unrestrictPool
|
||||
}
|
||||
|
||||
ensureDir(DATA_DIR)
|
||||
|
||||
// create internal directories
|
||||
t.DirectoryMap.Set(INT_ALL, cmap.New[*Torrent]()) // key is AccessKey
|
||||
t.DirectoryMap.Set(INT_INFO_CACHE, cmap.New[*Torrent]()) // key is Torrent ID
|
||||
|
||||
@@ -2,7 +2,6 @@ package torrent
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func getFileIDs(files []File) []string {
|
||||
@@ -14,15 +13,3 @@ func getFileIDs(files []File) []string {
|
||||
}
|
||||
return fileIDs
|
||||
}
|
||||
|
||||
func ensureDir(dirName string) error {
|
||||
if _, err := os.Stat(dirName); os.IsNotExist(err) {
|
||||
err := os.Mkdir(dirName, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package version
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -11,7 +11,7 @@ var (
|
||||
Version string = "dev"
|
||||
)
|
||||
|
||||
func Show() {
|
||||
func ShowVersion() {
|
||||
fmt.Printf("zurg\nBuilt At: %s\nGo Version: %s\nCommit: %s\nVersion: %s\n",
|
||||
BuiltAt, GoVersion, GitCommit, Version)
|
||||
}
|
||||
Reference in New Issue
Block a user