2022-04-21 15:14:49 -03:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2024-09-24 18:31:58 -03:00
|
|
|
"fmt"
|
2022-04-21 15:14:49 -03:00
|
|
|
"net/http"
|
2024-09-24 18:31:58 -03:00
|
|
|
"os"
|
2025-07-29 12:34:37 -03:00
|
|
|
"strconv"
|
|
|
|
|
"time"
|
2022-04-21 15:14:49 -03:00
|
|
|
|
2023-09-23 17:02:55 +00:00
|
|
|
handler "github.com/felipemarinho97/torrent-indexer/api"
|
|
|
|
|
"github.com/felipemarinho97/torrent-indexer/cache"
|
2025-07-29 12:34:37 -03:00
|
|
|
"github.com/felipemarinho97/torrent-indexer/magnet"
|
2024-03-10 14:00:07 +00:00
|
|
|
"github.com/felipemarinho97/torrent-indexer/monitoring"
|
2024-12-13 11:54:55 -03:00
|
|
|
"github.com/felipemarinho97/torrent-indexer/public"
|
2024-09-24 18:31:58 -03:00
|
|
|
"github.com/felipemarinho97/torrent-indexer/requester"
|
2024-12-13 11:54:55 -03:00
|
|
|
meilisearch "github.com/felipemarinho97/torrent-indexer/search"
|
2024-03-10 14:00:07 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2024-12-11 16:01:09 -03:00
|
|
|
|
|
|
|
|
str2duration "github.com/xhit/go-str2duration/v2"
|
2022-04-21 15:14:49 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2023-09-23 17:02:55 +00:00
|
|
|
redis := cache.NewRedis()
|
2024-12-13 11:54:55 -03:00
|
|
|
searchIndex := meilisearch.NewSearchIndexer(os.Getenv("MEILISEARCH_ADDRESS"), os.Getenv("MEILISEARCH_KEY"), "torrents")
|
2025-07-29 12:34:37 -03:00
|
|
|
var magnetMetadataAPI *magnet.MetadataClient
|
|
|
|
|
if os.Getenv("MAGNET_METADATA_API_ENABLED") == "true" {
|
|
|
|
|
timeout := 10 * time.Second
|
|
|
|
|
if v := os.Getenv("MAGNET_METADATA_API_TIMEOUT_SECONDS"); v != "" {
|
|
|
|
|
if t, err := strconv.Atoi(v); err == nil {
|
|
|
|
|
timeout = time.Duration(t) * time.Second
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
magnetMetadataAPI = magnet.NewClient(os.Getenv("MAGNET_METADATA_API_ADDRESS"), timeout, redis)
|
|
|
|
|
}
|
2024-03-10 14:00:07 +00:00
|
|
|
metrics := monitoring.NewMetrics()
|
|
|
|
|
metrics.Register()
|
2024-09-24 18:31:58 -03:00
|
|
|
|
|
|
|
|
flaresolverr := requester.NewFlareSolverr(os.Getenv("FLARESOLVERR_ADDRESS"), 60000)
|
|
|
|
|
req := requester.NewRequester(flaresolverr, redis)
|
2024-12-11 16:01:09 -03:00
|
|
|
|
|
|
|
|
// get shot-lived and long-lived cache expiration from env
|
|
|
|
|
shortLivedCacheExpiration, err := str2duration.ParseDuration(os.Getenv("SHORT_LIVED_CACHE_EXPIRATION"))
|
|
|
|
|
if err == nil {
|
|
|
|
|
fmt.Printf("Setting short-lived cache expiration to %s\n", shortLivedCacheExpiration)
|
|
|
|
|
req.SetShortLivedCacheExpiration(shortLivedCacheExpiration)
|
|
|
|
|
}
|
|
|
|
|
longLivedCacheExpiration, err := str2duration.ParseDuration(os.Getenv("LONG_LIVED_CACHE_EXPIRATION"))
|
|
|
|
|
if err == nil {
|
|
|
|
|
fmt.Printf("Setting long-lived cache expiration to %s\n", longLivedCacheExpiration)
|
|
|
|
|
redis.SetDefaultExpiration(longLivedCacheExpiration)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-29 12:34:37 -03:00
|
|
|
indexers := handler.NewIndexers(redis, metrics, req, searchIndex, magnetMetadataAPI)
|
2024-12-13 11:54:55 -03:00
|
|
|
search := handler.NewMeilisearchHandler(searchIndex)
|
2023-09-23 17:02:55 +00:00
|
|
|
|
2024-03-10 14:00:07 +00:00
|
|
|
indexerMux := http.NewServeMux()
|
|
|
|
|
metricsMux := http.NewServeMux()
|
2022-04-21 15:14:49 -03:00
|
|
|
|
2024-03-10 14:00:07 +00:00
|
|
|
indexerMux.HandleFunc("/", handler.HandlerIndex)
|
|
|
|
|
indexerMux.HandleFunc("/indexers/bludv", indexers.HandlerBluDVIndexer)
|
2025-07-22 14:57:05 -03:00
|
|
|
indexerMux.HandleFunc("/indexers/comando_torrents", indexers.HandlerComandoIndexer)
|
2025-07-16 15:32:27 -03:00
|
|
|
indexerMux.HandleFunc("/indexers/comandohds", indexers.HandlerComandoHDsIndexer)
|
2025-07-22 14:57:05 -03:00
|
|
|
indexerMux.HandleFunc("/indexers/rede_torrent", indexers.HandlerRedeTorrentIndexer)
|
2025-07-16 15:25:17 -03:00
|
|
|
indexerMux.HandleFunc("/indexers/starck-filmes", indexers.HandlerStarckFilmesIndexer)
|
2025-07-22 14:57:05 -03:00
|
|
|
indexerMux.HandleFunc("/indexers/torrent-dos-filmes", indexers.HandlerTorrentDosFilmesIndexer)
|
2024-06-18 12:38:08 -03:00
|
|
|
indexerMux.HandleFunc("/indexers/manual", indexers.HandlerManualIndexer)
|
2024-12-13 11:54:55 -03:00
|
|
|
indexerMux.HandleFunc("/search", search.SearchTorrentHandler)
|
2025-07-23 18:01:44 -03:00
|
|
|
indexerMux.HandleFunc("/search/health", search.HealthHandler)
|
|
|
|
|
indexerMux.HandleFunc("/search/stats", search.StatsHandler)
|
2024-12-13 11:54:55 -03:00
|
|
|
indexerMux.Handle("/ui/", http.StripPrefix("/ui/", http.FileServer(http.FS(public.UIFiles))))
|
2024-03-10 14:00:07 +00:00
|
|
|
|
|
|
|
|
metricsMux.Handle("/metrics", promhttp.Handler())
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
err := http.ListenAndServe(":8081", metricsMux)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
2024-12-11 16:01:09 -03:00
|
|
|
|
|
|
|
|
port := os.Getenv("PORT")
|
|
|
|
|
if port == "" {
|
2025-07-23 21:15:17 +00:00
|
|
|
port = "7006"
|
2024-12-11 16:01:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Printf("Server listening on :%s\n", port)
|
|
|
|
|
err = http.ListenAndServe(":"+port, indexerMux)
|
2022-04-21 15:14:49 -03:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|