Use jsoniter

This commit is contained in:
Ben Sarmiento
2023-12-02 06:54:38 +01:00
parent 91ca1ebf88
commit 01fe4f0a09
7 changed files with 64 additions and 41 deletions

View File

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