Use jsoniter
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"github.com/debridmediamanager/zurg/internal/config"
|
||||
"github.com/debridmediamanager/zurg/internal/dav"
|
||||
@@ -14,8 +15,12 @@ import (
|
||||
"github.com/debridmediamanager/zurg/pkg/realdebrid"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"go.uber.org/zap"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
type ZurgRouter struct {
|
||||
getfile *universal.GetFile
|
||||
torMgr *torrent.TorrentManager
|
||||
@@ -172,35 +177,51 @@ func (zr *ZurgRouter) headFileHandler(resp http.ResponseWriter, req *http.Reques
|
||||
universal.HandleHeadRequest(directory, torrentName, fileName, resp, req, zr.torMgr, zr.log)
|
||||
}
|
||||
|
||||
// Create a struct to hold the data
|
||||
type RootResponse struct {
|
||||
Version string `json:"zurg"`
|
||||
BuiltAt string `json:"built_at"`
|
||||
GitCommit string `json:"git_commit"`
|
||||
GoVersion string `json:"go_version"`
|
||||
Dav string `json:"dav"`
|
||||
Html string `json:"html"`
|
||||
UserInfo *realdebrid.User `json:"user_info"`
|
||||
Version string `json:"version"`
|
||||
BuiltAt string `json:"built_at"`
|
||||
GitCommit string `json:"git_commit"`
|
||||
GoVersion string `json:"go_version"`
|
||||
Dav string `json:"dav"`
|
||||
Html string `json:"html"`
|
||||
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
|
||||
}
|
||||
|
||||
func (zr *ZurgRouter) rootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
||||
userInfo, err := zr.api.GetUserInformation()
|
||||
if err != nil {
|
||||
// Handle the error appropriately
|
||||
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(),
|
||||
GoVersion: version.GetGoVersion(),
|
||||
Dav: fmt.Sprintf("//%s/dav/", req.Host),
|
||||
Html: fmt.Sprintf("//%s/http/", req.Host),
|
||||
UserInfo: userInfo,
|
||||
Version: version.GetVersion(),
|
||||
BuiltAt: version.GetBuiltAt(),
|
||||
GitCommit: version.GetGitCommit(),
|
||||
GoVersion: version.GetGoVersion(),
|
||||
Dav: fmt.Sprintf("//%s/dav/", req.Host),
|
||||
Html: fmt.Sprintf("//%s/http/", req.Host),
|
||||
UserInfo: userInfo,
|
||||
MemAlloc: bToMb(mem.Alloc),
|
||||
TotalAlloc: bToMb(mem.TotalAlloc),
|
||||
Sys: bToMb(mem.Sys),
|
||||
NumGC: mem.NumGC,
|
||||
PID: os.Getpid(),
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(resp).Encode(response); err != nil {
|
||||
http.Error(resp, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func bToMb(b uint64) uint64 {
|
||||
return b / 1024 / 1024
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package torrent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
@@ -296,11 +295,11 @@ func (t *TorrentManager) startRefreshJob() {
|
||||
for {
|
||||
<-time.After(time.Duration(t.Config.GetRefreshEverySeconds()) * time.Second)
|
||||
|
||||
checksum := t.getCurrentState()
|
||||
if t.latestState.equal(checksum) {
|
||||
continue
|
||||
}
|
||||
t.log.Infof("Detected changes! Refreshing %d torrents", checksum.TotalCount)
|
||||
// checksum := t.getCurrentState()
|
||||
// if t.latestState.equal(checksum) {
|
||||
// continue
|
||||
// }
|
||||
// t.log.Infof("Detected changes! Refreshing %d torrents", checksum.TotalCount)
|
||||
|
||||
t.RefreshTorrents()
|
||||
t.log.Info("Finished refreshing torrents")
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package torrent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
oldjson "encoding/json"
|
||||
|
||||
"github.com/debridmediamanager/zurg/pkg/realdebrid"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
type Torrent struct {
|
||||
AccessKey string `json:"AccessKey"`
|
||||
SelectedFiles cmap.ConcurrentMap[string, *File] `json:"-"`
|
||||
@@ -19,8 +22,7 @@ type Torrent struct {
|
||||
func (t *Torrent) MarshalJSON() ([]byte, error) {
|
||||
type Alias Torrent
|
||||
temp := &struct {
|
||||
SelectedFilesJson json.RawMessage `json:"SelectedFiles"`
|
||||
// InstancesJson json.RawMessage `json:"Instances"`
|
||||
SelectedFilesJson oldjson.RawMessage `json:"SelectedFiles"`
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(t),
|
||||
@@ -30,19 +32,13 @@ func (t *Torrent) MarshalJSON() ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
temp.SelectedFilesJson = selectedFilesJson
|
||||
// instancesJson, err := json.Marshal(t.Instances)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// temp.InstancesJson = instancesJson
|
||||
return json.Marshal(temp)
|
||||
}
|
||||
|
||||
func (t *Torrent) UnmarshalJSON(data []byte) error {
|
||||
type Alias Torrent
|
||||
temp := &struct {
|
||||
SelectedFilesJson json.RawMessage `json:"SelectedFiles"`
|
||||
// InstancesJson json.RawMessage `json:"Instances"`
|
||||
SelectedFilesJson oldjson.RawMessage `json:"SelectedFiles"`
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(t),
|
||||
@@ -56,12 +52,6 @@ func (t *Torrent) UnmarshalJSON(data []byte) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// if len(temp.InstancesJson) > 0 {
|
||||
// instances := make([]*realdebrid.TorrentInfo, 0)
|
||||
// if err := json.Unmarshal(temp.SelectedFilesJson, &instances); err != nil {
|
||||
// return nil
|
||||
// }
|
||||
// }
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user