diff --git a/internal/handlers/home.go b/internal/handlers/home.go
index e2e1c90..4efe8f1 100644
--- a/internal/handlers/home.go
+++ b/internal/handlers/home.go
@@ -1,6 +1,7 @@
package handlers
import (
+ "encoding/json"
"fmt"
"net/http"
"os"
@@ -18,6 +19,7 @@ type SponsorResponse struct {
Github string `json:"github"`
Paypal string `json:"paypal"`
}
+
type RootResponse struct {
Version string `json:"version"`
BuiltAt string `json:"built_at"`
@@ -37,21 +39,22 @@ type RootResponse struct {
PID int `json:"pid"` // Process ID
Sponsor SponsorResponse `json:"sponsor_zurg"` // Sponsorship links
Config config.ZurgConfig `json:"config"`
+ Token string `json:"token"`
IDsToDelete []string `json:"ids_to_delete"`
Hosts []string `json:"hosts"`
}
-func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
+func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request) (*RootResponse, error) {
userInfo, err := zr.api.GetUserInformation()
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
- return
+ return nil, err
}
trafficDetails, err := zr.api.GetTrafficDetails()
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
- return
+ return nil, err
}
var mem runtime.MemStats
@@ -85,7 +88,11 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
rdTrafficUsed = trafficDetails["real-debrid.com"]
}
- response := RootResponse{
+ userInfo.Premium = userInfo.Premium / 86400
+ userInfo.Expiration = strings.Replace(userInfo.Expiration, "Z", "+01:00", 1)
+ token := zr.cfg.GetToken()
+
+ return &RootResponse{
Version: version.GetVersion(),
BuiltAt: version.GetBuiltAt(),
GitCommit: version.GetGitCommit(),
@@ -108,252 +115,68 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
Paypal: "https://paypal.me/yowmamasita",
},
Config: zr.cfg.GetConfig(),
+ Token: strings.Replace(token, token[len(token)-48:], "*****", 1),
IDsToDelete: sortedIDs,
Hosts: zr.hosts,
+ }, nil
+}
+
+func (zr *Handlers) handleHomeJson(resp http.ResponseWriter, req *http.Request) {
+ response, err := zr.generateResponse(resp, req)
+ if err != nil {
+ return
}
- out := `
-
- | zurg |
-
-
- | Version |
- %s |
-
-
- | Built At |
- %s |
-
-
- | Git Commit |
- %s |
-
-
- | HTML |
- %s |
-
-
- | DAV |
- %s |
-
-
- | Infuse |
- %s |
-
-
- | Logs |
- %s |
-
-
- | Library Size |
- %d items |
-
-
- | Memory Allocation |
- %d MB |
-
-
- | Total Memory Allocated |
- %d MB |
-
-
- | System Memory |
- %d MB |
-
-
- | Number of GC Cycles |
- %d |
-
-
- | Process ID |
- %d |
-
-
- | RD Traffic Used |
- %d GB |
-
-
- | Sponsor Zurg |
- Patreon |
- %s |
-
-
- | Github |
- %s |
-
-
- | Paypal |
- %s |
-
-
- | User Info |
- Username |
- %s |
-
-
- | Points |
- %d |
-
-
- | Locale |
- %s |
-
-
- | Type |
- %s |
-
-
- | Premium |
- %d days |
-
-
- | Expiration |
- %s |
-
-
- | Config |
- Version |
- %s |
-
-
- | Token |
- %s |
-
-
- | Host |
- %s |
-
-
- | Port |
- %s |
-
-
- | Workers |
- %d running / %d free / %d total |
-
-
- | Refresh Every... |
- %d secs |
-
-
- | Retain RD Torrent Name |
- %t |
-
-
- | Retain Folder Name Extension |
- %t |
-
-
- | Can Repair |
- %t |
-
-
- | Action to take on RAR'ed torrents |
- %s |
-
-
- | Repair Every... |
- %d mins |
-
-
- | Refresh Download Mount Every... |
- %d mins |
-
-
- | Dump Torrents Every... |
- %d mins |
-
-
- | API Timeout |
- %d secs |
-
-
- | Download Timeout |
- %d secs |
-
-
- | Retries Until Failed |
- %d |
-
-
- | Auto-Analyze New Torrents |
- %t |
-
-
- | Cache Network Test Results |
- %t |
-
-
- | Additional Playable Extensions |
- %s |
-
-
- | Network Buffer Size |
- %d bytes |
-
-
- | Serve From Rclone |
- %t |
-
-
- | Force IPv6 |
- %t |
-
-
- | On Library Update |
- %v |
-
-
- | IDs to be deleted |
- %v |
-
-
- | Torrents to be repaired |
- %s |
-
-
- | Hosts |
- %v |
-
-
- | Utilities |
-
-
-
-
-
- |
-
- | Debug |
-
-
-
-
-
-
- |
-
-
-`
- out = fmt.Sprintf(out,
+ // Send the response as JSON by marshalling it
+ jsonResponse, err := json.Marshal(response)
+ if err != nil {
+ http.Error(resp, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ // write the response
+ resp.Header().Set("Content-Type", "application/json")
+ resp.Write(jsonResponse)
+}
+
+func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
+ response, err := zr.generateResponse(resp, req)
+ if err != nil {
+ return
+ }
+
+ out := fmt.Sprintf(`
+
+ | zurg |
+
+
+ | Version |
+ %s |
+
+
+ | Built At |
+ %s |
+
+
+ | Git Commit |
+ %s |
+
+
+ | HTML |
+ %s |
+
+
+ | DAV |
+ %s |
+
+
+ | Infuse |
+ %s |
+
+
+ | Logs |
+ %s |
+
`,
response.Version,
response.BuiltAt,
response.GitCommit,
@@ -365,6 +188,37 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.Infuse,
response.Logs,
response.Logs,
+ )
+
+ out += fmt.Sprintf(`
+
+ | Library Size |
+ %d items |
+
+
+ | Memory Allocation |
+ %d MB |
+
+
+ | Total Memory Allocated |
+ %d MB |
+
+
+ | System Memory |
+ %d MB |
+
+
+ | Number of GC Cycles |
+ %d |
+
+
+ | Process ID |
+ %d |
+
+
+ | RD Traffic Used |
+ %d GB |
+
`,
response.LibrarySize,
response.MemAlloc,
response.TotalAlloc,
@@ -372,20 +226,160 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.NumGC,
response.PID,
response.RDTrafficUsed,
+ )
+
+ out += fmt.Sprintf(`
+
+ | Sponsor Zurg |
+ Patreon |
+ %s |
+
+
+ | Github |
+ %s |
+
+
+ | Paypal |
+ %s |
+
`,
response.Sponsor.Patreon,
response.Sponsor.Patreon,
response.Sponsor.Github,
response.Sponsor.Github,
response.Sponsor.Paypal,
response.Sponsor.Paypal,
+ )
+
+ out += fmt.Sprintf(`
+
+ | User Info |
+ Username |
+ %s |
+
+
+ | Points |
+ %d |
+
+
+ | Locale |
+ %s |
+
+
+ | Type |
+ %s |
+
+
+ | Premium |
+ %d days |
+
+
+ | Expiration |
+ %s |
+
`,
response.UserInfo.Username,
response.UserInfo.Points,
response.UserInfo.Locale,
response.UserInfo.Type,
- response.UserInfo.Premium/86400,
- strings.Replace(response.UserInfo.Expiration, "Z", "+01:00", 1),
+ response.UserInfo.Premium,
+ response.UserInfo.Expiration,
+ )
+
+ out += fmt.Sprintf(`
+
+ | Config |
+ Version |
+ %s |
+
+
+ | Token |
+ %s |
+
+
+ | Host |
+ %s |
+
+
+ | Port |
+ %s |
+
+
+ | Workers |
+ %d running / %d free / %d total |
+
+
+ | Refresh Every... |
+ %d secs |
+
+
+ | Retain RD Torrent Name |
+ %t |
+
+
+ | Retain Folder Name Extension |
+ %t |
+
+
+ | Can Repair |
+ %t |
+
+
+ | Action to take on RAR'ed torrents |
+ %s |
+
+
+ | Repair Every... |
+ %d mins |
+
+
+ | Refresh Download Mount Every... |
+ %d mins |
+
+
+ | Dump Torrents Every... |
+ %d mins |
+
+
+ | API Timeout |
+ %d secs |
+
+
+ | Download Timeout |
+ %d secs |
+
+
+ | Retries Until Failed |
+ %d |
+
+
+ | Auto-Analyze New Torrents |
+ %t |
+
+
+ | Cache Network Test Results |
+ %t |
+
+
+ | Additional Playable Extensions |
+ %s |
+
+
+ | Network Buffer Size |
+ %d bytes |
+
+
+ | Serve From Rclone |
+ %t |
+
+
+ | Force IPv6 |
+ %t |
+
+
+ | On Library Update |
+ %v |
+
`,
response.Config.Version,
- strings.Replace(response.Config.Token, response.Config.Token[len(response.Config.Token)-48:], "*****", 1),
+ response.Token,
response.Config.GetHost(),
response.Config.GetPort(),
zr.workerPool.Running(),
@@ -409,6 +403,59 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.Config.ShouldServeFromRclone(),
response.Config.ShouldForceIPv6(),
response.Config.GetOnLibraryUpdate(),
+ )
+
+ out += fmt.Sprintf(`
+
+ | IDs to be deleted |
+ %v |
+
+
+ | Torrents to be repaired |
+ %s |
+
+
+ | Hosts |
+ %v |
+
+
+ | Utilities |
+
+
+
+
+
+ |
+
+
+ | Debug |
+
+
+
+
+
+
+ |
+
+
`,
response.IDsToDelete,
response.TorrentsToRepair,
response.Hosts,
diff --git a/internal/handlers/router.go b/internal/handlers/router.go
index 5c802cc..38503f4 100644
--- a/internal/handlers/router.go
+++ b/internal/handlers/router.go
@@ -52,6 +52,7 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
router.Use(hs.options)
router.Get("/", hs.handleHome)
+ router.Get("/stats", hs.handleHomeJson)
// debug
router.Post("/reboot-worker", hs.handleRebootWorkerPool)
router.Post("/reboot-refresh", hs.handleRebootRefreshWorker)