76 lines
2.1 KiB
Go
76 lines
2.1 KiB
Go
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/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"
|
|
|
|
_ "net/http/pprof"
|
|
)
|
|
|
|
func main() {
|
|
// special commands
|
|
if len(os.Args) > 1 {
|
|
switch os.Args[1] {
|
|
case "version":
|
|
version.Show()
|
|
case "networktest":
|
|
realdebrid.RunTest()
|
|
}
|
|
os.Exit(0)
|
|
}
|
|
|
|
// normal startup
|
|
log := logutil.NewLogger()
|
|
zurglog := log.Named("zurg")
|
|
|
|
config, configErr := config.LoadZurgConfig("./config.yml", 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()
|
|
|
|
torrentMgr := torrent.NewTorrentManager(config, rd, p, log.Named("manager"))
|
|
|
|
downloadClient := zurghttp.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), 0, config, log.Named("dlclient"))
|
|
getfile := universal.NewGetFile(downloadClient)
|
|
|
|
go func() {
|
|
http.ListenAndServe("localhost:6060", nil)
|
|
}()
|
|
|
|
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)
|
|
}
|
|
}
|