70 lines
2.2 KiB
Go
70 lines
2.2 KiB
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/debridmediamanager/zurg/internal/config"
|
|
"github.com/debridmediamanager/zurg/internal/net"
|
|
"github.com/debridmediamanager/zurg/internal/torrent"
|
|
"github.com/debridmediamanager/zurg/internal/universal"
|
|
zurghttp "github.com/debridmediamanager/zurg/pkg/http"
|
|
"github.com/debridmediamanager/zurg/pkg/logutil"
|
|
"github.com/debridmediamanager/zurg/pkg/realdebrid"
|
|
"github.com/debridmediamanager/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)
|
|
}
|
|
}
|