Implement root handler

This commit is contained in:
Ben Sarmiento
2023-11-30 23:08:16 +01:00
parent 0ad879066e
commit 9f81b2b1d0
9 changed files with 105 additions and 127 deletions

View File

@@ -1,6 +1,7 @@
package router
import (
"encoding/json"
"fmt"
"net/http"
@@ -9,6 +10,8 @@ import (
intHttp "github.com/debridmediamanager/zurg/internal/http"
"github.com/debridmediamanager/zurg/internal/torrent"
"github.com/debridmediamanager/zurg/internal/universal"
"github.com/debridmediamanager/zurg/internal/version"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/julienschmidt/httprouter"
"go.uber.org/zap"
)
@@ -17,14 +20,16 @@ type ZurgRouter struct {
getfile *universal.GetFile
torMgr *torrent.TorrentManager
cfg config.ConfigInterface
api *realdebrid.RealDebrid
log *zap.SugaredLogger
}
func ApplyRouteTable(router *httprouter.Router, getfile *universal.GetFile, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, log *zap.SugaredLogger) {
func ApplyRouteTable(router *httprouter.Router, getfile *universal.GetFile, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, api *realdebrid.RealDebrid, log *zap.SugaredLogger) {
zr := &ZurgRouter{
getfile: getfile,
torMgr: torMgr,
cfg: cfg,
api: api,
log: log,
}
@@ -92,10 +97,6 @@ func (zr *ZurgRouter) httpRootHandler(resp http.ResponseWriter, req *http.Reques
fmt.Fprint(resp, *out)
}
func (zr *ZurgRouter) rootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
fmt.Fprint(resp, "<h1>zurg</h1><a href=\"/http/\">HTTP</a><br><a href=\"/dav/\">DAV</a>")
}
func (zr *ZurgRouter) propfindTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory")
torrentName := params.ByName("torrent")
@@ -170,3 +171,32 @@ func (zr *ZurgRouter) headFileHandler(resp http.ResponseWriter, req *http.Reques
fileName := params.ByName("file")
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"`
UserInfo *realdebrid.User `json:"user_info"`
}
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
}
response := RootResponse{
Version: version.GetVersion(),
BuiltAt: version.GetBuiltAt(),
GitCommit: version.GetGitCommit(),
GoVersion: version.GetGoVersion(),
UserInfo: userInfo,
}
if err := json.NewEncoder(resp).Encode(response); err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
}
}