Add a json handler for home
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -18,6 +19,7 @@ type SponsorResponse struct {
|
|||||||
Github string `json:"github"`
|
Github string `json:"github"`
|
||||||
Paypal string `json:"paypal"`
|
Paypal string `json:"paypal"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RootResponse struct {
|
type RootResponse struct {
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
BuiltAt string `json:"built_at"`
|
BuiltAt string `json:"built_at"`
|
||||||
@@ -37,21 +39,22 @@ type RootResponse struct {
|
|||||||
PID int `json:"pid"` // Process ID
|
PID int `json:"pid"` // Process ID
|
||||||
Sponsor SponsorResponse `json:"sponsor_zurg"` // Sponsorship links
|
Sponsor SponsorResponse `json:"sponsor_zurg"` // Sponsorship links
|
||||||
Config config.ZurgConfig `json:"config"`
|
Config config.ZurgConfig `json:"config"`
|
||||||
|
Token string `json:"token"`
|
||||||
IDsToDelete []string `json:"ids_to_delete"`
|
IDsToDelete []string `json:"ids_to_delete"`
|
||||||
Hosts []string `json:"hosts"`
|
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()
|
userInfo, err := zr.api.GetUserInformation()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(resp, err.Error(), http.StatusInternalServerError)
|
http.Error(resp, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
trafficDetails, err := zr.api.GetTrafficDetails()
|
trafficDetails, err := zr.api.GetTrafficDetails()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(resp, err.Error(), http.StatusInternalServerError)
|
http.Error(resp, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var mem runtime.MemStats
|
var mem runtime.MemStats
|
||||||
@@ -85,7 +88,11 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
|
|||||||
rdTrafficUsed = trafficDetails["real-debrid.com"]
|
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(),
|
Version: version.GetVersion(),
|
||||||
BuiltAt: version.GetBuiltAt(),
|
BuiltAt: version.GetBuiltAt(),
|
||||||
GitCommit: version.GetGitCommit(),
|
GitCommit: version.GetGitCommit(),
|
||||||
@@ -108,11 +115,37 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
|
|||||||
Paypal: "https://paypal.me/yowmamasita",
|
Paypal: "https://paypal.me/yowmamasita",
|
||||||
},
|
},
|
||||||
Config: zr.cfg.GetConfig(),
|
Config: zr.cfg.GetConfig(),
|
||||||
|
Token: strings.Replace(token, token[len(token)-48:], "*****", 1),
|
||||||
IDsToDelete: sortedIDs,
|
IDsToDelete: sortedIDs,
|
||||||
Hosts: zr.hosts,
|
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>
|
<tr>
|
||||||
<th colspan="3">zurg</th>
|
<th colspan="3">zurg</th>
|
||||||
</tr>
|
</tr>
|
||||||
@@ -143,7 +176,21 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
|
|||||||
<tr>
|
<tr>
|
||||||
<td>Logs</td>
|
<td>Logs</td>
|
||||||
<td colspan="2"><a href="%s">%s</a></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>
|
<tr>
|
||||||
<td>Library Size</td>
|
<td>Library Size</td>
|
||||||
<td colspan="2">%d items</td>
|
<td colspan="2">%d items</td>
|
||||||
@@ -171,7 +218,17 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
|
|||||||
<tr>
|
<tr>
|
||||||
<td>RD Traffic Used</td>
|
<td>RD Traffic Used</td>
|
||||||
<td colspan="2">%d GB</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>
|
<tr>
|
||||||
<td rowspan="3">Sponsor Zurg</td>
|
<td rowspan="3">Sponsor Zurg</td>
|
||||||
<td>Patreon</td>
|
<td>Patreon</td>
|
||||||
@@ -184,7 +241,16 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
|
|||||||
<tr>
|
<tr>
|
||||||
<td>Paypal</td>
|
<td>Paypal</td>
|
||||||
<td><a href="%s">%s</a></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>
|
<tr>
|
||||||
<td rowspan="6">User Info</td>
|
<td rowspan="6">User Info</td>
|
||||||
<td>Username</td>
|
<td>Username</td>
|
||||||
@@ -209,7 +275,16 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
|
|||||||
<tr>
|
<tr>
|
||||||
<td>Expiration</td>
|
<td>Expiration</td>
|
||||||
<td>%s</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>
|
<tr>
|
||||||
<td rowspan="23">Config</td>
|
<td rowspan="23">Config</td>
|
||||||
<td>Version</td>
|
<td>Version</td>
|
||||||
@@ -302,7 +377,35 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
|
|||||||
<tr>
|
<tr>
|
||||||
<td>On Library Update</td>
|
<td>On Library Update</td>
|
||||||
<td>%v</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>
|
<tr>
|
||||||
<td>IDs to be deleted</td>
|
<td>IDs to be deleted</td>
|
||||||
<td colspan="2">%v</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
|
<input type="submit" value="Reset repair state" /> Reset repair state of all torrents so they can be repaired again
|
||||||
</form>
|
</form>
|
||||||
</td>
|
</td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Debug</td>
|
<td>Debug</td>
|
||||||
<td colspan="2">
|
<td colspan="2">
|
||||||
@@ -351,64 +455,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
|
|||||||
</form>
|
</form>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</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(),
|
|
||||||
response.IDsToDelete,
|
response.IDsToDelete,
|
||||||
response.TorrentsToRepair,
|
response.TorrentsToRepair,
|
||||||
response.Hosts,
|
response.Hosts,
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
|
|||||||
router.Use(hs.options)
|
router.Use(hs.options)
|
||||||
|
|
||||||
router.Get("/", hs.handleHome)
|
router.Get("/", hs.handleHome)
|
||||||
|
router.Get("/stats", hs.handleHomeJson)
|
||||||
// debug
|
// debug
|
||||||
router.Post("/reboot-worker", hs.handleRebootWorkerPool)
|
router.Post("/reboot-worker", hs.handleRebootWorkerPool)
|
||||||
router.Post("/reboot-refresh", hs.handleRebootRefreshWorker)
|
router.Post("/reboot-refresh", hs.handleRebootRefreshWorker)
|
||||||
|
|||||||
Reference in New Issue
Block a user