Refactor structure, use cobra
This commit is contained in:
@@ -2,79 +2,45 @@ package main
|
||||
|
||||
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"
|
||||
"github.com/debridmediamanager.com/zurg/internal/version"
|
||||
"github.com/dgraph-io/ristretto"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
|
||||
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/internal"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// special commands
|
||||
if len(os.Args) > 1 {
|
||||
switch os.Args[1] {
|
||||
case "version":
|
||||
version.Show()
|
||||
case "networktest":
|
||||
realdebrid.RunTest()
|
||||
}
|
||||
os.Exit(0)
|
||||
var configPath string // Variable to hold the config path
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "zurg",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
internal.MainApp(configPath) // Pass the configPath to MainApp
|
||||
},
|
||||
}
|
||||
|
||||
// normal startup
|
||||
log := logutil.NewLogger()
|
||||
zurglog := log.Named("zurg")
|
||||
// Adding a flag for the config path with a default value
|
||||
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "./config.yml", "config file path")
|
||||
|
||||
config, configErr := config.LoadZurgConfig("./config.yml", log.Named("config"))
|
||||
if configErr != nil {
|
||||
zurglog.Errorf("Config failed to load: %v", configErr)
|
||||
os.Exit(1)
|
||||
var versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Prints zurg's current version",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
internal.ShowVersion()
|
||||
},
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
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)
|
||||
var networkTestCmd = &cobra.Command{
|
||||
Use: "network-test",
|
||||
Short: "Run a network test",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
internal.NetworkTest()
|
||||
},
|
||||
}
|
||||
|
||||
torrentMgr := torrent.NewTorrentManager(config, rd, p, cache, log.Named("manager"))
|
||||
rootCmd.AddCommand(versionCmd, networkTestCmd)
|
||||
|
||||
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)
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user