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

@@ -40,10 +40,10 @@ jobs:
race: false
ldflags: >
-s -w
-X 'github.com/debridmediamanager/zurg/internal.BuiltAt=$(date +%Y-%m-%dT%H:%M:%S)'
-X 'github.com/debridmediamanager/zurg/internal.GoVersion=$(go version | cut -d ' ' -f 3)'
-X 'github.com/debridmediamanager/zurg/internal.GitCommit=$(git rev-parse HEAD)'
-X 'github.com/debridmediamanager/zurg/internal.Version=${{ steps.version.outputs.version }}'
-X 'github.com/debridmediamanager/zurg/internal/version.BuiltAt=$(date +%Y-%m-%dT%H:%M:%S)'
-X 'github.com/debridmediamanager/zurg/internal/version.GoVersion=$(go version | cut -d ' ' -f 3)'
-X 'github.com/debridmediamanager/zurg/internal/version.GitCommit=$(git rev-parse HEAD)'
-X 'github.com/debridmediamanager/zurg/internal/version.Version=${{ steps.version.outputs.version }}'
buildmode: default
trimpath: true

View File

@@ -11,7 +11,7 @@ FROM golang:1-alpine AS builder
WORKDIR /app
COPY . .
RUN apk add --no-cache bash git go gcc musl-dev curl fuse
RUN go build -ldflags="-s -w -X 'github.com/debridmediamanager/zurg/internal.BuiltAt=${BuiltAt}' -X 'github.com/debridmediamanager/zurg/internal.GoVersion=${GoVersion}' -X 'github.com/debridmediamanager/zurg/internal.GitCommit=${GitCommit}' -X 'github.com/debridmediamanager/zurg/internal.Version=${Version}'" -o zurg ./cmd/zurg
RUN go build -ldflags="-s -w -X 'github.com/debridmediamanager/zurg/internal/version.BuiltAt=${BuiltAt}' -X 'github.com/debridmediamanager/zurg/internal/version.GoVersion=${GoVersion}' -X 'github.com/debridmediamanager/zurg/internal/version.GitCommit=${GitCommit}' -X 'github.com/debridmediamanager/zurg/internal/version.Version=${Version}'" -o zurg ./cmd/zurg
# Obfuscation stage
FROM alpine:3 AS obfuscator

109
build.sh
View File

@@ -1,109 +0,0 @@
#!/bin/bash
appName="zurg"
builtAt="$(date +'%F %T %z')"
goVersion=$(go version | sed 's/go version //')
gitCommit=$(git log --pretty=format:"%h" -1)
if [ "$1" = "dev" ]; then
version="dev"
else
version=$(git describe --abbrev=0 --tags)
fi
echo "app version: $version"
ldflags="\
-w -s \
-X 'github.com/debridmediamanager/zurg/internal/version.BuiltAt=$builtAt' \
-X 'github.com/debridmediamanager/zurg/internal/version.GoVersion=$goVersion' \
-X 'github.com/debridmediamanager/zurg/internal/version.GitCommit=$gitCommit' \
-X 'github.com/debridmediamanager/zurg/internal/version.Version=$version' \
"
BuildDev() {
# rm -rf .git/
xgo -targets=linux/amd64,windows/amd64,darwin/arm64 -out "$appName" -ldflags="$ldflags" ./cmd/zurg
mkdir -p "dist"
mv zurg-* dist
cd dist
upx -9 ./zurg-linux-amd64
upx -9 ./zurg-windows*
find . -type f -print0 | xargs -0 md5sum >md5.txt
cat md5.txt
}
BuildDocker() {
go build -o ./bin/zurg -ldflags="$ldflags" .
}
BuildRelease() {
# rm -rf .git/
mkdir -p "build"
muslflags="--extldflags '-static -fpic' $ldflags"
BASE="https://musl.nn.ci/"
FILES=(x86_64-linux-musl-cross aarch64-linux-musl-cross arm-linux-musleabihf-cross mips-linux-musl-cross mips64-linux-musl-cross mips64el-linux-musl-cross mipsel-linux-musl-cross powerpc64le-linux-musl-cross s390x-linux-musl-cross)
for i in "${FILES[@]}"; do
url="${BASE}${i}.tgz"
curl -L -o "${i}.tgz" "${url}"
sudo tar xf "${i}.tgz" --strip-components 1 -C /usr/local
done
OS_ARCHES=(linux-musl-amd64 linux-musl-arm64 linux-musl-arm linux-musl-mips linux-musl-mips64 linux-musl-mips64le linux-musl-mipsle linux-musl-ppc64le linux-musl-s390x)
CGO_ARGS=(x86_64-linux-musl-gcc aarch64-linux-musl-gcc arm-linux-musleabihf-gcc mips-linux-musl-gcc mips64-linux-musl-gcc mips64el-linux-musl-gcc mipsel-linux-musl-gcc powerpc64le-linux-musl-gcc s390x-linux-musl-gcc)
for i in "${!OS_ARCHES[@]}"; do
os_arch=${OS_ARCHES[$i]}
cgo_cc=${CGO_ARGS[$i]}
echo building for ${os_arch}
export GOOS=${os_arch%%-*}
export GOARCH=${os_arch##*-}
export CC=${cgo_cc}
export CGO_ENABLED=1
go build -o ./build/$appName-$os_arch -ldflags="$muslflags" .
done
xgo -out "$appName" -ldflags="$ldflags" .
# why? Because some target platforms seem to have issues with upx compression
upx -9 ./zurg-linux-amd64
upx -9 ./zurg-windows*
mv zurg-* build
}
MakeRelease() {
cd build
mkdir compress
for i in $(find . -type f -name "$appName-linux-*"); do
cp "$i" zurg
tar -czvf compress/"$i".tar.gz zurg
rm -f zurg
done
for i in $(find . -type f -name "$appName-darwin-*"); do
cp "$i" zurg
tar -czvf compress/"$i".tar.gz zurg
rm -f zurg
done
for i in $(find . -type f -name "$appName-windows-*"); do
cp "$i" zurg.exe
zip compress/$(echo $i | sed 's/\.[^.]*$//').zip zurg.exe
rm -f zurg.exe
done
cd compress
find . -type f -print0 | xargs -0 md5sum >md5.txt
cat md5.txt
cd ../..
}
if [ "$1" = "dev" ]; then
if [ "$2" = "docker" ]; then
BuildDocker
else
BuildDev
fi
elif [ "$1" = "release" ]; then
if [ "$2" = "docker" ]; then
BuildDocker
else
BuildRelease
MakeRelease
fi
else
echo -e "Parameter error"
fi

View File

@@ -59,7 +59,7 @@ func MainApp(configPath string) {
handler := httprouter.New()
handler.RedirectTrailingSlash = false
handler.RedirectFixedPath = true
router.ApplyRouteTable(handler, getfile, torrentMgr, config, log.Named("router"))
router.ApplyRouteTable(handler, getfile, torrentMgr, config, rd, log.Named("router"))
addr := fmt.Sprintf("%s:%s", config.GetHost(), config.GetPort())
zurglog.Infof("Starting server on %s", addr)

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

View File

@@ -2,16 +2,11 @@ package internal
import (
"fmt"
)
var (
BuiltAt string
GoVersion string
GitCommit string
Version string = "dev"
"github.com/debridmediamanager/zurg/internal/version"
)
func ShowVersion() {
fmt.Printf("zurg\nBuilt At: %s\nGo Version: %s\nCommit: %s\nVersion: %s\n",
BuiltAt, GoVersion, GitCommit, Version)
version.GetBuiltAt(), version.GetGoVersion(), version.GetGitCommit(), version.GetVersion())
}

View File

@@ -0,0 +1,21 @@
package version
var (
BuiltAt string
GoVersion string
GitCommit string
Version string = "dev"
)
func GetBuiltAt() string {
return BuiltAt
}
func GetGoVersion() string {
return GoVersion
}
func GetGitCommit() string {
return GitCommit
}
func GetVersion() string {
return Version
}

View File

@@ -360,3 +360,32 @@ func (rd *RealDebrid) GetDownloads(page, offset int) ([]Download, int, error) {
rd.log.Debugf("Got %d downloads (page %d), total count is %d", len(allDownloads)+offset, page, totalCount)
return allDownloads, totalCount, nil
}
// GetUserInformation gets the current user information.
func (rd *RealDebrid) GetUserInformation() (*User, error) {
// Construct request URL
reqURL := "https://api.real-debrid.com/rest/1.0/user"
req, err := http.NewRequest("GET", reqURL, nil)
if err != nil {
rd.log.Errorf("Error when creating a user information request: %v", err)
return nil, err
}
// Send the request
resp, err := rd.client.Do(req)
if err != nil {
rd.log.Errorf("Error when executing the user information request: %v", err)
return nil, err
}
defer resp.Body.Close()
// Decode the JSON response into the User struct
var user User
err = json.NewDecoder(resp.Body).Decode(&user)
if err != nil {
rd.log.Errorf("Error when decoding user information JSON: %v", err)
return nil, err
}
return &user, nil
}

View File

@@ -107,3 +107,15 @@ type ActiveTorrentCountResponse struct {
DownloadingCount int `json:"nb"`
MaxNumberOfTorrents int `json:"limit"`
}
type User struct {
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Points int `json:"points"` // Fidelity points
Locale string `json:"locale"` // User language
Avatar string `json:"avatar"` // URL
Type string `json:"type"` // "premium" or "free"
Premium int `json:"premium"` // seconds left as a Premium user
Expiration string `json:"expiration"` // jsonDate
}