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,11 +115,37 @@ 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
}
out := `<table border="1px">
func (zr *Handlers) handleHomeJson(resp http.ResponseWriter, req *http.Request) {
response, err := zr.generateResponse(resp, req)
if err != nil {
return
}
// 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>
@@ -143,7 +176,21 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<tr>
<td>Logs</td>
<td colspan="2"><a href="%s">%s</a></td>
</tr>
</tr>`,
response.Version,
response.BuiltAt,
response.GitCommit,
response.Html,
response.Html,
response.Dav,
response.Dav,
response.Infuse,
response.Infuse,
response.Logs,
response.Logs,
)
out += fmt.Sprintf(`
<tr>
<td>Library Size</td>
<td colspan="2">%d items</td>
@@ -171,7 +218,17 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<tr>
<td>RD Traffic Used</td>
<td colspan="2">%d GB</td>
</tr>
</tr>`,
response.LibrarySize,
response.MemAlloc,
response.TotalAlloc,
response.Sys,
response.NumGC,
response.PID,
response.RDTrafficUsed,
)
out += fmt.Sprintf(`
<tr>
<td rowspan="3">Sponsor Zurg</td>
<td>Patreon</td>
@@ -184,7 +241,16 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<tr>
<td>Paypal</td>
<td><a href="%s">%s</a></td>
</tr>
</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>
@@ -209,7 +275,16 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<tr>
<td>Expiration</td>
<td>%s</td>
</tr>
</tr>`,
response.UserInfo.Username,
response.UserInfo.Points,
response.UserInfo.Locale,
response.UserInfo.Type,
response.UserInfo.Premium,
response.UserInfo.Expiration,
)
out += fmt.Sprintf(`
<tr>
<td rowspan="23">Config</td>
<td>Version</td>
@@ -302,7 +377,35 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<tr>
<td>On Library Update</td>
<td>%v</td>
</tr>
</tr>`,
response.Config.Version,
response.Token,
response.Config.GetHost(),
response.Config.GetPort(),
zr.workerPool.Running(),
zr.workerPool.Free(),
zr.workerPool.Cap(),
response.Config.GetRefreshEverySecs(),
response.Config.EnableRetainRDTorrentName(),
response.Config.EnableRetainFolderNameExtension(),
response.Config.EnableRepair(),
response.Config.GetRarAction(),
response.Config.GetRepairEveryMins(),
response.Config.GetDownloadsEveryMins(),
response.Config.GetDumpTorrentsEveryMins(),
response.Config.GetApiTimeoutSecs(),
response.Config.GetDownloadTimeoutSecs(),
response.Config.GetRetriesUntilFailed(),
response.Config.ShouldAutoAnalyzeNewTorrents(),
response.Config.ShouldCacheNetworkTestResults(),
response.Config.GetPlayableExtensions(),
response.Config.GetNetworkBufferSize(),
response.Config.ShouldServeFromRclone(),
response.Config.ShouldForceIPv6(),
response.Config.GetOnLibraryUpdate(),
)
out += fmt.Sprintf(`
<tr>
<td>IDs to be deleted</td>
<td colspan="2">%v</td>
@@ -331,6 +434,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<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">
@@ -351,64 +455,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
</form>
</td>
</tr>
</table>
`
out = fmt.Sprintf(out,
response.Version,
response.BuiltAt,
response.GitCommit,
response.Html,
response.Html,
response.Dav,
response.Dav,
response.Infuse,
response.Infuse,
response.Logs,
response.Logs,
response.LibrarySize,
response.MemAlloc,
response.TotalAlloc,
response.Sys,
response.NumGC,
response.PID,
response.RDTrafficUsed,
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/86400,
strings.Replace(response.UserInfo.Expiration, "Z", "+01:00", 1),
response.Config.Version,
strings.Replace(response.Config.Token, response.Config.Token[len(response.Config.Token)-48:], "*****", 1),
response.Config.GetHost(),
response.Config.GetPort(),
zr.workerPool.Running(),
zr.workerPool.Free(),
zr.workerPool.Cap(),
response.Config.GetRefreshEverySecs(),
response.Config.EnableRetainRDTorrentName(),
response.Config.EnableRetainFolderNameExtension(),
response.Config.EnableRepair(),
response.Config.GetRarAction(),
response.Config.GetRepairEveryMins(),
response.Config.GetDownloadsEveryMins(),
response.Config.GetDumpTorrentsEveryMins(),
response.Config.GetApiTimeoutSecs(),
response.Config.GetDownloadTimeoutSecs(),
response.Config.GetRetriesUntilFailed(),
response.Config.ShouldAutoAnalyzeNewTorrents(),
response.Config.ShouldCacheNetworkTestResults(),
response.Config.GetPlayableExtensions(),
response.Config.GetNetworkBufferSize(),
response.Config.ShouldServeFromRclone(),
response.Config.ShouldForceIPv6(),
response.Config.GetOnLibraryUpdate(),
</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)