Add a json handler for home

This commit is contained in:
Ben Adrian Sarmiento
2024-06-24 18:41:27 +02:00
parent 08867d8fea
commit ec25c1ef17
2 changed files with 297 additions and 249 deletions

View File

@@ -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 := `<table border="1px">
<tr>
<th colspan="3">zurg</th>
</tr>
<tr>
<td>Version</td>
<td colspan="2">%s</td>
</tr>
<tr>
<td>Built At</td>
<td colspan="2">%s</td>
</tr>
<tr>
<td>Git Commit</td>
<td colspan="2">%s</td>
</tr>
<tr>
<td>HTML</td>
<td colspan="2"><a href="%s">%s</a></td>
</tr>
<tr>
<td>DAV</td>
<td colspan="2"><a href="%s">%s</a></td>
</tr>
<tr>
<td>Infuse</td>
<td colspan="2"><a href="%s">%s</a></td>
</tr>
<tr>
<td>Logs</td>
<td colspan="2"><a href="%s">%s</a></td>
</tr>
<tr>
<td>Library Size</td>
<td colspan="2">%d items</td>
</tr>
<tr>
<td>Memory Allocation</td>
<td colspan="2">%d MB</td>
</tr>
<tr>
<td>Total Memory Allocated</td>
<td colspan="2">%d MB</td>
</tr>
<tr>
<td>System Memory</td>
<td colspan="2">%d MB</td>
</tr>
<tr>
<td>Number of GC Cycles</td>
<td colspan="2">%d</td>
</tr>
<tr>
<td>Process ID</td>
<td colspan="2">%d</td>
</tr>
<tr>
<td>RD Traffic Used</td>
<td colspan="2">%d GB</td>
</tr>
<tr>
<td rowspan="3">Sponsor Zurg</td>
<td>Patreon</td>
<td><a href="%s">%s</a></td>
</tr>
<tr>
<td>Github</td>
<td><a href="%s">%s</a></td>
</tr>
<tr>
<td>Paypal</td>
<td><a href="%s">%s</a></td>
</tr>
<tr>
<td rowspan="6">User Info</td>
<td>Username</td>
<td>%s</td>
</tr>
<tr>
<td>Points</td>
<td>%d</td>
</tr>
<tr>
<td>Locale</td>
<td>%s</td>
</tr>
<tr>
<td>Type</td>
<td>%s</td>
</tr>
<tr>
<td>Premium</td>
<td>%d days</td>
</tr>
<tr>
<td>Expiration</td>
<td>%s</td>
</tr>
<tr>
<td rowspan="23">Config</td>
<td>Version</td>
<td>%s</td>
</tr>
<tr>
<td>Token</td>
<td>%s</td>
</tr>
<tr>
<td>Host</td>
<td>%s</td>
</tr>
<tr>
<td>Port</td>
<td>%s</td>
</tr>
<tr>
<td>Workers</td>
<td>%d running / %d free / %d total</td>
</tr>
<tr>
<td>Refresh Every...</td>
<td>%d secs</td>
</tr>
<tr>
<td>Retain RD Torrent Name</td>
<td>%t</td>
</tr>
<tr>
<td>Retain Folder Name Extension</td>
<td>%t</td>
</tr>
<tr>
<td>Can Repair</td>
<td>%t</td>
</tr>
<tr>
<td>Action to take on RAR'ed torrents</td>
<td>%s</td>
</tr>
<tr>
<td>Repair Every...</td>
<td>%d mins</td>
</tr>
<tr>
<td>Refresh Download Mount Every...</td>
<td>%d mins</td>
</tr>
<tr>
<td>Dump Torrents Every...</td>
<td>%d mins</td>
</tr>
<tr>
<td>API Timeout</td>
<td>%d secs</td>
</tr>
<tr>
<td>Download Timeout</td>
<td>%d secs</td>
</tr>
<tr>
<td>Retries Until Failed</td>
<td>%d</td>
</tr>
<tr>
<td>Auto-Analyze New Torrents</td>
<td>%t</td>
</tr>
<tr>
<td>Cache Network Test Results</td>
<td>%t</td>
</tr>
<tr>
<td>Additional Playable Extensions</td>
<td>%s</td>
</tr>
<tr>
<td>Network Buffer Size</td>
<td>%d bytes</td>
</tr>
<tr>
<td>Serve From Rclone</td>
<td>%t</td>
</tr>
<tr>
<td>Force IPv6</td>
<td>%t</td>
</tr>
<tr>
<td>On Library Update</td>
<td>%v</td>
</tr>
<tr>
<td>IDs to be deleted</td>
<td colspan="2">%v</td>
</tr>
<tr>
<td>Torrents to be repaired</td>
<td colspan="2">%s</td>
</tr>
<tr>
<td>Hosts</td>
<td colspan="2">%v</td>
</tr>
<tr>
<td>Utilities</td>
<td colspan="2">
<form method="post" action="/torrents/dump">
<input type="submit" value="Dump torrents" /> Copy all your zurgtorrent files to your dump folder
</form>
<form method="post" action="/torrents/analyze">
<input type="submit" value="Analyze torrents" /> Required to use media_info_* filters
</form>
<form method="post" action="/torrents/repair">
<input type="submit" value="Repair torrents" /> Trigger repair of all torrents
</form>
<form method="post" action="/torrents/reset-repair-state">
<input type="submit" value="Reset repair state" /> Reset repair state of all torrents so they can be repaired again
</form>
</td>
<tr>
<td>Debug</td>
<td colspan="2">
<form method="get" action="/logs/upload">
<input type="submit" value="Upload logs" /> Share the link to get help
</form>
<form method="post" action="/reboot-worker">
<input type="submit" value="Reboot worker pool" />
</form>
<form method="post" action="/reboot-refresh">
<input type="submit" value="Reboot refresh worker" />
</form>
<form method="post" action="/reboot-repair">
<input type="submit" value="Reboot repair worker" />
</form>
<form method="post" action="/downloads/remount">
<input type="submit" value="Remount downloads" />
</form>
</td>
</tr>
</table>
`
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(`<table border="1px">
<tr>
<th colspan="3">zurg</th>
</tr>
<tr>
<td>Version</td>
<td colspan="2">%s</td>
</tr>
<tr>
<td>Built At</td>
<td colspan="2">%s</td>
</tr>
<tr>
<td>Git Commit</td>
<td colspan="2">%s</td>
</tr>
<tr>
<td>HTML</td>
<td colspan="2"><a href="%s">%s</a></td>
</tr>
<tr>
<td>DAV</td>
<td colspan="2"><a href="%s">%s</a></td>
</tr>
<tr>
<td>Infuse</td>
<td colspan="2"><a href="%s">%s</a></td>
</tr>
<tr>
<td>Logs</td>
<td colspan="2"><a href="%s">%s</a></td>
</tr>`,
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(`
<tr>
<td>Library Size</td>
<td colspan="2">%d items</td>
</tr>
<tr>
<td>Memory Allocation</td>
<td colspan="2">%d MB</td>
</tr>
<tr>
<td>Total Memory Allocated</td>
<td colspan="2">%d MB</td>
</tr>
<tr>
<td>System Memory</td>
<td colspan="2">%d MB</td>
</tr>
<tr>
<td>Number of GC Cycles</td>
<td colspan="2">%d</td>
</tr>
<tr>
<td>Process ID</td>
<td colspan="2">%d</td>
</tr>
<tr>
<td>RD Traffic Used</td>
<td colspan="2">%d GB</td>
</tr>`,
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(`
<tr>
<td rowspan="3">Sponsor Zurg</td>
<td>Patreon</td>
<td><a href="%s">%s</a></td>
</tr>
<tr>
<td>Github</td>
<td><a href="%s">%s</a></td>
</tr>
<tr>
<td>Paypal</td>
<td><a href="%s">%s</a></td>
</tr>`,
response.Sponsor.Patreon,
response.Sponsor.Patreon,
response.Sponsor.Github,
response.Sponsor.Github,
response.Sponsor.Paypal,
response.Sponsor.Paypal,
)
out += fmt.Sprintf(`
<tr>
<td rowspan="6">User Info</td>
<td>Username</td>
<td>%s</td>
</tr>
<tr>
<td>Points</td>
<td>%d</td>
</tr>
<tr>
<td>Locale</td>
<td>%s</td>
</tr>
<tr>
<td>Type</td>
<td>%s</td>
</tr>
<tr>
<td>Premium</td>
<td>%d days</td>
</tr>
<tr>
<td>Expiration</td>
<td>%s</td>
</tr>`,
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(`
<tr>
<td rowspan="23">Config</td>
<td>Version</td>
<td>%s</td>
</tr>
<tr>
<td>Token</td>
<td>%s</td>
</tr>
<tr>
<td>Host</td>
<td>%s</td>
</tr>
<tr>
<td>Port</td>
<td>%s</td>
</tr>
<tr>
<td>Workers</td>
<td>%d running / %d free / %d total</td>
</tr>
<tr>
<td>Refresh Every...</td>
<td>%d secs</td>
</tr>
<tr>
<td>Retain RD Torrent Name</td>
<td>%t</td>
</tr>
<tr>
<td>Retain Folder Name Extension</td>
<td>%t</td>
</tr>
<tr>
<td>Can Repair</td>
<td>%t</td>
</tr>
<tr>
<td>Action to take on RAR'ed torrents</td>
<td>%s</td>
</tr>
<tr>
<td>Repair Every...</td>
<td>%d mins</td>
</tr>
<tr>
<td>Refresh Download Mount Every...</td>
<td>%d mins</td>
</tr>
<tr>
<td>Dump Torrents Every...</td>
<td>%d mins</td>
</tr>
<tr>
<td>API Timeout</td>
<td>%d secs</td>
</tr>
<tr>
<td>Download Timeout</td>
<td>%d secs</td>
</tr>
<tr>
<td>Retries Until Failed</td>
<td>%d</td>
</tr>
<tr>
<td>Auto-Analyze New Torrents</td>
<td>%t</td>
</tr>
<tr>
<td>Cache Network Test Results</td>
<td>%t</td>
</tr>
<tr>
<td>Additional Playable Extensions</td>
<td>%s</td>
</tr>
<tr>
<td>Network Buffer Size</td>
<td>%d bytes</td>
</tr>
<tr>
<td>Serve From Rclone</td>
<td>%t</td>
</tr>
<tr>
<td>Force IPv6</td>
<td>%t</td>
</tr>
<tr>
<td>On Library Update</td>
<td>%v</td>
</tr>`,
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(`
<tr>
<td>IDs to be deleted</td>
<td colspan="2">%v</td>
</tr>
<tr>
<td>Torrents to be repaired</td>
<td colspan="2">%s</td>
</tr>
<tr>
<td>Hosts</td>
<td colspan="2">%v</td>
</tr>
<tr>
<td>Utilities</td>
<td colspan="2">
<form method="post" action="/torrents/dump">
<input type="submit" value="Dump torrents" /> Copy all your zurgtorrent files to your dump folder
</form>
<form method="post" action="/torrents/analyze">
<input type="submit" value="Analyze torrents" /> Required to use media_info_* filters
</form>
<form method="post" action="/torrents/repair">
<input type="submit" value="Repair torrents" /> Trigger repair of all torrents
</form>
<form method="post" action="/torrents/reset-repair-state">
<input type="submit" value="Reset repair state" /> Reset repair state of all torrents so they can be repaired again
</form>
</td>
</tr>
<tr>
<td>Debug</td>
<td colspan="2">
<form method="get" action="/logs/upload">
<input type="submit" value="Upload logs" /> Share the link to get help
</form>
<form method="post" action="/reboot-worker">
<input type="submit" value="Reboot worker pool" />
</form>
<form method="post" action="/reboot-refresh">
<input type="submit" value="Reboot refresh worker" />
</form>
<form method="post" action="/reboot-repair">
<input type="submit" value="Reboot repair worker" />
</form>
<form method="post" action="/downloads/remount">
<input type="submit" value="Remount downloads" />
</form>
</td>
</tr>
</table>`,
response.IDsToDelete,
response.TorrentsToRepair,
response.Hosts,

View File

@@ -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)