Skip to content

Commit

Permalink
feat: add useragent for requests (#75)
Browse files Browse the repository at this point in the history
* feature(httpclient): add useragent

* build: set buildinfo vars

* feat: print build date with build info

* fix: imports

* feat(lists): set user agent for http queries

* fix: imports
  • Loading branch information
zze0s committed Feb 28, 2024
1 parent 0ea22a3 commit e770691
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ COPY . ./
#ENV GOOS=linux
ENV CGO_ENABLED=0

RUN go build -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${REVISION} -X main.date=${BUILDTIME}" -o bin/omegabrr cmd/omegabrr/main.go
RUN go build -ldflags "-s -w -X buildinfo.Version=${VERSION} -X buildinfo.Commit=${REVISION} -X buildinfo.Date=${BUILDTIME}" -o bin/omegabrr cmd/omegabrr/main.go

# build runner
FROM alpine:latest
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ GIT_TAG := $(shell git tag --points-at HEAD 2> /dev/null | head -n 1)

GO ?= go
RM ?= rm
GOFLAGS ?= "-X main.commit=$(GIT_COMMIT) -X main.version=$(GIT_TAG)"
GOFLAGS ?= "-X buildinfo.Commit=$(GIT_COMMIT) -X buildinfo.Version=$(GIT_TAG)"
PREFIX ?= /usr/local
BINDIR ?= bin

Expand Down
14 changes: 5 additions & 9 deletions cmd/omegabrr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/autobrr/omegabrr/internal/apitoken"
"github.com/autobrr/omegabrr/internal/buildinfo"
"github.com/autobrr/omegabrr/internal/domain"
"github.com/autobrr/omegabrr/internal/http"
"github.com/autobrr/omegabrr/internal/processor"
Expand All @@ -27,11 +28,6 @@ import (
"github.com/spf13/pflag"
)

var (
version = "dev"
commit = ""
)

const usage = `omegabrr - Automagically turn your monitored titles from your arrs and lists into autobrr filters.
Usage:
Expand Down Expand Up @@ -98,7 +94,7 @@ func main() {

switch cmd := pflag.Arg(0); cmd {
case "version":
fmt.Printf("Version: %v\nCommit: %v\n", version, commit)
fmt.Printf("Version: %v\nCommit: %v\nBuild date: %v\n", buildinfo.Version, buildinfo.Commit, buildinfo.Date)

// get the latest release tag from brr-api
client := &netHTTP.Client{
Expand Down Expand Up @@ -132,7 +128,7 @@ func main() {
fmt.Printf("Latest release: %v\n", rel.TagName)

case "update":
v, err := semver.ParseTolerant(version)
v, err := semver.ParseTolerant(buildinfo.Version)
if err != nil {
log.Error().Err(err).Msg("could not parse version")
return
Expand All @@ -146,7 +142,7 @@ func main() {

if latest.Version.Equals(v) {
// latest version is the same as current version. It means current binary is up-to-date.
log.Info().Msgf("Current binary is the latest version: %s", version)
log.Info().Msgf("Current binary is the latest version: %s", buildinfo.Version)
} else {
log.Info().Msgf("Successfully updated to version: %s", latest.Version)
}
Expand Down Expand Up @@ -196,7 +192,7 @@ func main() {
case "run":
cfg := domain.NewConfig(configPath)

log.Info().Msgf("starting omegabrr: %s", version)
log.Info().Msgf("starting omegabrr: %s", buildinfo.Version)
log.Info().Msgf("running on schedule: %v", cfg.Schedule)

p := processor.NewService(cfg)
Expand Down
7 changes: 7 additions & 0 deletions internal/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package buildinfo

var (
Version = "dev"
Commit = ""
Date = ""
)
15 changes: 15 additions & 0 deletions internal/processor/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package processor

import (
"fmt"
"net/http"
"runtime"

"github.com/autobrr/omegabrr/internal/buildinfo"
)

func setUserAgent(req *http.Request) {
agent := fmt.Sprintf("omegabrr/%s (%s %s)", buildinfo.Version, runtime.GOOS, runtime.GOARCH)

req.Header.Set("User-Agent", agent)
}
2 changes: 2 additions & 0 deletions internal/processor/mdblist.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func (s Service) mdblist(ctx context.Context, cfg *domain.ListConfig, dryRun boo
req.Header.Set(k, v)
}

setUserAgent(req)

resp, err := s.httpClient.Do(req)
if err != nil {
l.Error().Err(err).Msgf("failed to fetch titles from URL: %s", cfg.URL)
Expand Down
2 changes: 2 additions & 0 deletions internal/processor/metacritic.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func (s Service) metacritic(ctx context.Context, cfg *domain.ListConfig, dryRun
req.Header.Set(k, v)
}

setUserAgent(req)

resp, err := s.httpClient.Do(req)
if err != nil {
l.Error().Err(err).Msgf("failed to fetch titles from URL: %s", cfg.URL)
Expand Down
2 changes: 2 additions & 0 deletions internal/processor/plaintext.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func (s Service) plaintext(ctx context.Context, cfg *domain.ListConfig, dryRun b
req.Header.Set(k, v)
}

setUserAgent(req)

resp, err := s.httpClient.Do(req)
if err != nil {
l.Error().Err(err).Msgf("failed to fetch titles from URL: %s", cfg.URL)
Expand Down
2 changes: 2 additions & 0 deletions internal/processor/trakt.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (s Service) trakt(ctx context.Context, cfg *domain.ListConfig, dryRun bool,
req.Header.Set(k, v)
}

setUserAgent(req)

resp, err := s.httpClient.Do(req)
if err != nil {
l.Error().Err(err).Msgf("failed to fetch titles from URL: %s", cfg.URL)
Expand Down
13 changes: 12 additions & 1 deletion pkg/autobrr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"runtime"
"strconv"
"time"

"github.com/rs/zerolog/log"
"github.com/autobrr/omegabrr/internal/buildinfo"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)

type Client struct {
Expand Down Expand Up @@ -76,6 +79,7 @@ func (c *Client) GetFilters(ctx context.Context) ([]Filter, error) {

req.SetBasicAuth(c.BasicUser, c.BasicPass)
req.Header.Add("X-API-Token", c.APIKey)
c.buildUserAgent(req)

res, err := c.client.Do(req)
if err != nil {
Expand Down Expand Up @@ -115,6 +119,7 @@ func (c *Client) UpdateFilterByID(ctx context.Context, filterID int, filter Upda

req.SetBasicAuth(c.BasicUser, c.BasicPass)
req.Header.Add("X-API-Token", c.APIKey)
c.buildUserAgent(req)

res, err := c.client.Do(req)
if err != nil {
Expand All @@ -136,6 +141,12 @@ func (c *Client) UpdateFilterByID(ctx context.Context, filterID int, filter Upda
return nil
}

func (c *Client) buildUserAgent(req *http.Request) {
agent := fmt.Sprintf("omegabrr/%s (%s %s)", buildinfo.Version, runtime.GOOS, runtime.GOARCH)

req.Header.Set("User-Agent", agent)
}

type Filter struct {
ID string `json:"id"`
Name string `json:"name"`
Expand Down

0 comments on commit e770691

Please sign in to comment.