diff --git a/internal/router/root.go b/internal/router/root.go new file mode 100644 index 0000000..ed2636e --- /dev/null +++ b/internal/router/root.go @@ -0,0 +1,288 @@ +package router + +import ( + "fmt" + "net/http" + "os" + "runtime" + "strings" + + "github.com/debridmediamanager/zurg/internal/config" + "github.com/debridmediamanager/zurg/internal/version" + "github.com/debridmediamanager/zurg/pkg/realdebrid" + "github.com/julienschmidt/httprouter" +) + +type SponsorResponse struct { + Patreon string `json:"patreon"` + Github string `json:"github"` + Paypal string `json:"paypal"` +} +type RootResponse struct { + Version string `json:"version"` + BuiltAt string `json:"built_at"` + GitCommit string `json:"git_commit"` + Dav string `json:"dav"` + Html string `json:"html"` + Logs string `json:"logs"` + UserInfo *realdebrid.User `json:"user_info"` // Replace UserInfoType with the actual type + MemAlloc uint64 `json:"mem_alloc"` // Memory allocation in MB + TotalAlloc uint64 `json:"total_alloc"` // Total memory allocated in MB + Sys uint64 `json:"sys"` // System memory in MB + NumGC uint32 `json:"num_gc"` // Number of completed GC cycles + PID int `json:"pid"` // Process ID + Sponsor SponsorResponse `json:"sponsor_zurg"` // Sponsorship links + Config config.ZurgConfig `json:"config"` +} + +func (zr *ZurgRouter) rootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { + userInfo, err := zr.api.GetUserInformation() + if err != nil { + http.Error(resp, err.Error(), http.StatusInternalServerError) + return + } + + var mem runtime.MemStats + runtime.ReadMemStats(&mem) + + response := RootResponse{ + Version: version.GetVersion(), + BuiltAt: version.GetBuiltAt(), + GitCommit: version.GetGitCommit(), + Dav: fmt.Sprintf("//%s/dav/", req.Host), + Html: fmt.Sprintf("//%s/http/", req.Host), + Logs: fmt.Sprintf("//%s/logs/", req.Host), + UserInfo: userInfo, + MemAlloc: bToMb(mem.Alloc), + TotalAlloc: bToMb(mem.TotalAlloc), + Sys: bToMb(mem.Sys), + NumGC: mem.NumGC, + PID: os.Getpid(), + Sponsor: SponsorResponse{ + Patreon: "https://www.patreon.com/debridmediamanager", + Github: "https://github.com/sponsors/debridmediamanager", + Paypal: "https://paypal.me/yowmamasita", + }, + Config: zr.cfg.GetConfig(), + } + + out := ` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
zurg
Version%s
Built At%s
Git Commit%s
DAV%s
HTML%s
Logs%s
Memory Allocation%d MB
Total Memory Allocated%d MB
System Memory%d MB
Number of GC Cycles%d
Process ID%d
Sponsor ZurgPatreon%s
Github%s
Paypal%s
User InfoUsername%s
Points%d
Locale%s
Type%s
Premium%d seconds
Expiration%s
ConfigVersion%s
Token%s
Host%s
Port%s
Number of Workers%d
Refresh Every Seconds%d
Retain RD Torrent Name%t
Retain Folder Name Extension%t
Can Repair%t
Delete Rar Files%t
RealDebrid Timeout%d
Use Download Cache%t
Rate Limit Sleep Seconds%d
Retries Until Failed%d
Preferred Hosts%s
Network Buffer Size%d
Serve From Rclone%t
Verify Download Link%t
Force IPv6%t
On Library Update%s
+` + out = fmt.Sprintf(out, + response.Version, + response.BuiltAt, + response.GitCommit, + response.Dav, + response.Dav, + response.Html, + response.Html, + response.Logs, + response.Logs, + response.MemAlloc, + response.TotalAlloc, + response.Sys, + response.NumGC, + response.PID, + response.Sponsor.Patreon, + response.Sponsor.Patreon, + response.Sponsor.Github, + response.Sponsor.Github, + response.Sponsor.Paypal, + response.Sponsor.Paypal, + response.UserInfo.Username, + response.UserInfo.Points, + response.UserInfo.Locale, + response.UserInfo.Type, + response.UserInfo.Premium, + response.UserInfo.Expiration, + response.Config.Version, + strings.Replace(response.Config.Token, response.Config.Token[len(response.Config.Token)-48:], "*****", 1), + response.Config.Host, + response.Config.Port, + response.Config.NumOfWorkers, + response.Config.RefreshEverySeconds, + response.Config.RetainRDTorrentName, + response.Config.RetainFolderNameExtension, + response.Config.CanRepair, + response.Config.DeleteRarFiles, + response.Config.RealDebridTimeout, + response.Config.UseDownloadCache, + response.Config.RateLimitSleepSeconds, + response.Config.RetriesUntilFailed, + strings.Join(response.Config.PreferredHosts, ", "), + response.Config.NetworkBufferSize, + response.Config.ServeFromRclone, + response.Config.VerifyDownloadLink, + response.Config.ForceIPv6, + response.Config.OnLibraryUpdate, + ) + + fmt.Fprint(resp, out) +} diff --git a/internal/router/router.go b/internal/router/router.go index 478848c..24e8c9c 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -3,17 +3,13 @@ package router import ( "fmt" "net/http" - "os" "path/filepath" - "runtime" - "strings" "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/internal/dav" intHttp "github.com/debridmediamanager/zurg/internal/http" "github.com/debridmediamanager/zurg/internal/torrent" "github.com/debridmediamanager/zurg/internal/universal" - "github.com/debridmediamanager/zurg/internal/version" "github.com/debridmediamanager/zurg/pkg/logutil" "github.com/debridmediamanager/zurg/pkg/realdebrid" "github.com/julienschmidt/httprouter" @@ -239,280 +235,6 @@ func (zr *ZurgRouter) headFileHandler(resp http.ResponseWriter, req *http.Reques universal.HandleHeadRequest(directory, torrentName, fileName, resp, req, zr.torMgr, zr.log) } -type SponsorResponse struct { - Patreon string `json:"patreon"` - Github string `json:"github"` - Paypal string `json:"paypal"` -} -type RootResponse struct { - Version string `json:"version"` - BuiltAt string `json:"built_at"` - GitCommit string `json:"git_commit"` - Dav string `json:"dav"` - Html string `json:"html"` - Logs string `json:"logs"` - UserInfo *realdebrid.User `json:"user_info"` // Replace UserInfoType with the actual type - MemAlloc uint64 `json:"mem_alloc"` // Memory allocation in MB - TotalAlloc uint64 `json:"total_alloc"` // Total memory allocated in MB - Sys uint64 `json:"sys"` // System memory in MB - NumGC uint32 `json:"num_gc"` // Number of completed GC cycles - PID int `json:"pid"` // Process ID - Sponsor SponsorResponse `json:"sponsor_zurg"` // Sponsorship links - Config config.ZurgConfig `json:"config"` -} - -func (zr *ZurgRouter) rootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - userInfo, err := zr.api.GetUserInformation() - if err != nil { - http.Error(resp, err.Error(), http.StatusInternalServerError) - return - } - - var mem runtime.MemStats - runtime.ReadMemStats(&mem) - - response := RootResponse{ - Version: version.GetVersion(), - BuiltAt: version.GetBuiltAt(), - GitCommit: version.GetGitCommit(), - Dav: fmt.Sprintf("//%s/dav/", req.Host), - Html: fmt.Sprintf("//%s/http/", req.Host), - Logs: fmt.Sprintf("//%s/logs/", req.Host), - UserInfo: userInfo, - MemAlloc: bToMb(mem.Alloc), - TotalAlloc: bToMb(mem.TotalAlloc), - Sys: bToMb(mem.Sys), - NumGC: mem.NumGC, - PID: os.Getpid(), - Sponsor: SponsorResponse{ - Patreon: "https://www.patreon.com/debridmediamanager", - Github: "https://github.com/sponsors/debridmediamanager", - Paypal: "https://paypal.me/yowmamasita", - }, - Config: zr.cfg.GetConfig(), - } - - out := ` - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
zurg
Version%s
Built At%s
Git Commit%s
DAV%s
HTML%s
Logs%s
Memory Allocation%d MB
Total Memory Allocated%d MB
System Memory%d MB
Number of GC Cycles%d
Process ID%d
Sponsor ZurgPatreon%s
Github%s
Paypal%s
User InfoUsername%s
Points%d
Locale%s
Type%s
Premium%d seconds
Expiration%s
ConfigVersion%s
Token%s
Host%s
Port%s
Number of Workers%d
Refresh Every Seconds%d
Retain RD Torrent Name%t
Retain Folder Name Extension%t
Can Repair%t
Delete Rar Files%t
RealDebrid Timeout%d
Use Download Cache%t
Rate Limit Sleep Seconds%d
Retries Until Failed%d
Preferred Hosts%s
Network Buffer Size%d
Serve From Rclone%t
Verify Download Link%t
Force IPv6%t
On Library Update%s
-` - out = fmt.Sprintf(out, - response.Version, - response.BuiltAt, - response.GitCommit, - response.Dav, - response.Dav, - response.Html, - response.Html, - response.Logs, - response.Logs, - response.MemAlloc, - response.TotalAlloc, - response.Sys, - response.NumGC, - response.PID, - response.Sponsor.Patreon, - response.Sponsor.Patreon, - response.Sponsor.Github, - response.Sponsor.Github, - response.Sponsor.Paypal, - response.Sponsor.Paypal, - response.UserInfo.Username, - response.UserInfo.Points, - response.UserInfo.Locale, - response.UserInfo.Type, - response.UserInfo.Premium, - response.UserInfo.Expiration, - response.Config.Version, - strings.Replace(response.Config.Token, response.Config.Token[len(response.Config.Token)-48:], "*****", 1), - response.Config.Host, - response.Config.Port, - response.Config.NumOfWorkers, - response.Config.RefreshEverySeconds, - response.Config.RetainRDTorrentName, - response.Config.RetainFolderNameExtension, - response.Config.CanRepair, - response.Config.DeleteRarFiles, - response.Config.RealDebridTimeout, - response.Config.UseDownloadCache, - response.Config.RateLimitSleepSeconds, - response.Config.RetriesUntilFailed, - strings.Join(response.Config.PreferredHosts, ", "), - response.Config.NetworkBufferSize, - response.Config.ServeFromRclone, - response.Config.VerifyDownloadLink, - response.Config.ForceIPv6, - response.Config.OnLibraryUpdate, - ) - - fmt.Fprint(resp, out) -} - func (zr *ZurgRouter) logsHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { logs, err := zr.log.GetLogsFromFile() if err != nil {