86 lines
2.9 KiB
Go
86 lines
2.9 KiB
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
netHttp "net/http"
|
|
"os"
|
|
"time"
|
|
|
|
// _ "net/http/pprof" // Register pprof
|
|
|
|
"github.com/debridmediamanager/zurg/internal/config"
|
|
"github.com/debridmediamanager/zurg/internal/handlers"
|
|
"github.com/debridmediamanager/zurg/internal/torrent"
|
|
"github.com/debridmediamanager/zurg/internal/universal"
|
|
"github.com/debridmediamanager/zurg/internal/version"
|
|
"github.com/debridmediamanager/zurg/pkg/http"
|
|
"github.com/debridmediamanager/zurg/pkg/logutil"
|
|
"github.com/debridmediamanager/zurg/pkg/premium"
|
|
"github.com/debridmediamanager/zurg/pkg/realdebrid"
|
|
"github.com/debridmediamanager/zurg/pkg/utils"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/panjf2000/ants/v2"
|
|
)
|
|
|
|
func MainApp(configPath string) {
|
|
utils.EnsureDirExists("logs") // Ensure the logs directory exists
|
|
logPath := fmt.Sprintf("logs/zurg-%s.log", time.Now().Format(time.DateOnly))
|
|
log := logutil.NewLogger(logPath)
|
|
zurglog := log.Named("zurg")
|
|
|
|
zurglog.Debugf("PID: %d", os.Getpid())
|
|
zurglog.Debugf("Version: %s", version.GetVersion())
|
|
zurglog.Debugf("GitCommit: %s", version.GetGitCommit())
|
|
zurglog.Debugf("BuiltAt: %s", version.GetBuiltAt())
|
|
|
|
config, configErr := config.LoadZurgConfig(configPath, log.Named("config"))
|
|
if configErr != nil {
|
|
zurglog.Errorf("Config failed to load: %v", configErr)
|
|
os.Exit(1)
|
|
}
|
|
|
|
apiClient := http.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), config.GetRealDebridTimeout(), false, config, log.Named("httpclient"))
|
|
|
|
rd := realdebrid.NewRealDebrid(apiClient, log.Named("realdebrid"))
|
|
|
|
premium.MonitorPremiumStatus(rd, zurglog)
|
|
|
|
// extra 1 worker for the refresh job
|
|
workerPool, err := ants.NewPool(config.GetNumOfWorkers() + 1)
|
|
if err != nil {
|
|
zurglog.Errorf("Failed to create worker pool: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
defer workerPool.Release()
|
|
|
|
repairPool, err := ants.NewPool(1, ants.WithMaxBlockingTasks(1))
|
|
if err != nil {
|
|
zurglog.Errorf("Failed to create repair pool: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
defer repairPool.Release()
|
|
|
|
utils.EnsureDirExists("data") // Ensure the data directory exists
|
|
torrentMgr := torrent.NewTorrentManager(config, rd, workerPool, repairPool, log.Named("manager"))
|
|
|
|
downloadClient := http.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), 0, true, config, log.Named("dlclient"))
|
|
downloader := universal.NewDownloader(downloadClient)
|
|
|
|
router := chi.NewRouter()
|
|
handlers.AttachHandlers(router, downloader, torrentMgr, config, rd, log.Named("router"))
|
|
|
|
// go func() {
|
|
// if err := netHttp.ListenAndServe(":6060", nil); err != nil && err != netHttp.ErrServerClosed {
|
|
// zurglog.Errorf("Failed to start pprof: %v", err)
|
|
// os.Exit(1)
|
|
// }
|
|
// }()
|
|
|
|
addr := fmt.Sprintf("%s:%s", config.GetHost(), config.GetPort())
|
|
zurglog.Infof("Starting server on %s", addr)
|
|
if err := netHttp.ListenAndServe(addr, router); err != nil && err != netHttp.ErrServerClosed {
|
|
zurglog.Errorf("Failed to start server: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|