Skip to content

Commit

Permalink
Library Updates (#3)
Browse files Browse the repository at this point in the history
* rename webroot to urlbase. update starr

* lidar updates
  • Loading branch information
davidnewhall authored Dec 22, 2020
1 parent f492115 commit f9eb81b
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Change this line.
/discordnotifier-client
/dnclient.conf
# The rest is probably fine.
/rsrc*.syso
/*.log
/*.conf
/*.gz
/*.zip
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ docker logs <container id from docker run>
|Config Name|Variable Name|Default / Note|
|---|---|---|
debug|`DN_DEBUG`|`false` / Turns on more detailed logs.|
quiet|`DN_QUIET`|`false` / Turns off output. Recommend setting a log_file when this is true.|
quiet|`DN_QUIET`|`false` / Turns off output. Set a log_file if this is true.|
bind_addr|`DN_BIND_ADDR`|`0.0.0.0:5454` / The IP and port to listen on.|
webroot|`DN_WEBROOT`|`/` / API paths serve on `/api`. Use this to change it.|
urlbase|`DN_URLBASE`|default: `/` Change the web root with this setting.|
api_key|`DN_API_KEY`|**Required** / API Key from DiscordNotifier.com.|
log_file|`DN_LOG_FILE`|None by default. Optionally provide a file path to write logs.|
log_files|`DN_LOG_FILES`|`10` / Log files to keep after rotating. `0` disables rotation.|
Expand Down
2 changes: 1 addition & 1 deletion dnclient/appmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *Client) handleAPIpath(app App, api string, next apiHandle, method ...st
}

// disccordnotifier uses 1-indexes.
c.router.Handle(path.Join("/", c.Config.WebRoot, "api", string(app), api),
c.router.Handle(path.Join("/", c.Config.URLBase, "api", string(app), api),
c.checkAPIKey(c.responseWrapper(func(r *http.Request) (int, interface{}) {
switch id, _ := strconv.Atoi(mux.Vars(r)["id"]); {
default: // unknown app, just run the handler.
Expand Down
7 changes: 4 additions & 3 deletions dnclient/dnclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

// Application Defaults.
const (
DefaultName = "discordnotifier-client"
DefaultLogFileMb = 100
DefaultLogFiles = 0 // delete none
DefaultTimeout = 5 * time.Second
Expand All @@ -42,7 +43,7 @@ type Config struct {
LogFile string `json:"log_file" toml:"log_file" xml:"log_file" yaml:"log_file"`
LogFiles int `json:"log_files" toml:"log_files" xml:"log_files" yaml:"log_files"`
LogFileMb int `json:"log_file_mb" toml:"log_file_mb" xml:"log_file_mb" yaml:"log_file_mb"`
WebRoot string `json:"webroot" toml:"webroot" xml:"webroot" yaml:"webroot"`
URLBase string `json:"urlbase" toml:"urlbase" xml:"urlbase" yaml:"urlbase"`
Timeout cnfg.Duration `json:"timeout" toml:"timeout" xml:"timeout" yaml:"timeout"`
Sonarr []*SonarrConfig `json:"sonarr,omitempty" toml:"sonarr" xml:"sonarr" yaml:"sonarr,omitempty"`
Radarr []*RadarrConfig `json:"radarr,omitempty" toml:"radarr" xml:"radarr" yaml:"radarr,omitempty"`
Expand Down Expand Up @@ -78,13 +79,13 @@ func NewDefaults() *Client {
signal: make(chan os.Signal, 1),
Logger: &Logger{Logger: log.New(os.Stdout, "", log.LstdFlags)},
Config: &Config{
WebRoot: "/",
URLBase: "/",
LogFiles: DefaultLogFiles,
LogFileMb: DefaultLogFileMb,
BindAddr: DefaultBindAddr,
Timeout: cnfg.Duration{Duration: DefaultTimeout},
}, Flags: &Flags{
FlagSet: flag.NewFlagSet("discordnotifier-client", flag.ExitOnError),
FlagSet: flag.NewFlagSet(DefaultName, flag.ExitOnError),
ConfigFile: DefaultConfFile,
EnvPrefix: DefaultEnvPrefix,
},
Expand Down
4 changes: 2 additions & 2 deletions dnclient/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func (c *Client) InitStartup() {
c.Print(" => Debug / Quiet:", c.Config.Debug, "/", c.Config.Quiet)

if c.Config.SSLCrtFile != "" && c.Config.SSLKeyFile != "" {
c.Print(" => Web HTTPS Listen:", "https://"+c.Config.BindAddr+path.Join("/", c.Config.WebRoot))
c.Print(" => Web HTTPS Listen:", "https://"+c.Config.BindAddr+path.Join("/", c.Config.URLBase))
c.Print(" => Web Cert & Key Files:", c.Config.SSLCrtFile+", "+c.Config.SSLKeyFile)
} else {
c.Print(" => Web HTTP Listen:", "http://"+c.Config.BindAddr+path.Join("/", c.Config.WebRoot))
c.Print(" => Web HTTP Listen:", "http://"+c.Config.BindAddr+path.Join("/", c.Config.URLBase))
}

if c.Config.LogFile != "" {
Expand Down
45 changes: 40 additions & 5 deletions dnclient/lidarr.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package dnclient

import (
"encoding/json"
"fmt"
"net/http"

"github.com/gorilla/mux"
"golift.io/starr/lidarr"
)

/*
Expand All @@ -12,7 +16,7 @@ mbid - music brainz is the source for lidarr (todo)
// lidarrHandlers is called once on startup to register the web API paths.
func (c *Client) lidarrHandlers() {
c.handleAPIpath(Lidarr, "/add/{id:[0-9]+}", c.lidarrAddAlbum, "POST")
c.handleAPIpath(Lidarr, "/check/{id:[0-9]+}/{albumid:[0-9]+}", c.lidarrCheckAlbum, "GET")
c.handleAPIpath(Lidarr, "/check/{id:[0-9]+}/{albumid:[-a-z]+}", c.lidarrCheckAlbum, "GET")
c.handleAPIpath(Lidarr, "/qualityProfiles/{id:[0-9]+}", c.lidarrProfiles, "GET")
c.handleAPIpath(Lidarr, "/qualityDefinitions/{id:[0-9]+}", c.lidarrQualityDefs, "GET")
c.handleAPIpath(Lidarr, "/rootFolder/{id:[0-9]+}", c.lidarrRootFolders, "GET")
Expand Down Expand Up @@ -42,7 +46,7 @@ func (c *Client) lidarrProfiles(r *http.Request) (int, interface{}) {
}

// Format profile ID=>Name into a nice map.
p := make(map[int]string)
p := make(map[int64]string)
for i := range profiles {
p[profiles[i].ID] = profiles[i].Name
}
Expand All @@ -58,7 +62,7 @@ func (c *Client) lidarrQualityDefs(r *http.Request) (int, interface{}) {
}

// Format definitions ID=>Title into a nice map.
p := make(map[int]string)
p := make(map[int64]string)
for i := range definitions {
p[definitions[i].ID] = definitions[i].Title
}
Expand All @@ -67,9 +71,40 @@ func (c *Client) lidarrQualityDefs(r *http.Request) (int, interface{}) {
}

func (c *Client) lidarrCheckAlbum(r *http.Request) (int, interface{}) {
return http.StatusLocked, "lidarr does not work yet"
// Check for existing movie.
if m, err := getLidarr(r).GetAlbum(mux.Vars(r)["albumid"]); err != nil {
return http.StatusServiceUnavailable, fmt.Errorf("checking album: %w", err)
} else if len(m) > 0 {
return http.StatusConflict, fmt.Errorf("%s: %w", mux.Vars(r)["albumid"], ErrExists)
}

return http.StatusOK, http.StatusText(http.StatusNotFound)
}

func (c *Client) lidarrAddAlbum(r *http.Request) (int, interface{}) {
return http.StatusLocked, "lidarr does not work yet"
var payload lidarr.AddAlbumInput
// Extract payload and check for TMDB ID.
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
return http.StatusBadRequest, fmt.Errorf("decoding payload: %w", err)
} else if payload == nil {
return http.StatusUnprocessableEntity, fmt.Errorf("0: %w", ErrNoGRID)
}

lidar := getLidarr(r)
// Check for existing album.
/* broken:
if m, err := lidar.GetAlbum(payload.AlbumID); err != nil {
return http.StatusServiceUnavailable, fmt.Errorf("checking album: %w", err)
} else if len(m) > 0 {
return http.StatusConflict, fmt.Errorf("%d: %w", payload.AlbumID, ErrExists)
}
*/

// Add book using payload.
book, err := lidar.AddAlbum(&payload)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("adding album: %w", err)
}

return http.StatusCreated, book
}
6 changes: 3 additions & 3 deletions dnclient/radarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Client) radarrProfiles(r *http.Request) (int, interface{}) {
}

// Format profile ID=>Name into a nice map.
p := make(map[int]string)
p := make(map[int64]string)
for i := range profiles {
p[profiles[i].ID] = profiles[i].Name
}
Expand All @@ -51,7 +51,7 @@ func (c *Client) radarrProfiles(r *http.Request) (int, interface{}) {
}

func (c *Client) radarrCheckMovie(r *http.Request) (int, interface{}) {
tmdbID, _ := strconv.Atoi(mux.Vars(r)["tmdbid"])
tmdbID, _ := strconv.ParseInt(mux.Vars(r)["tmdbid"], 10, 64)
// Check for existing movie.
if m, err := getRadarr(r).GetMovie(tmdbID); err != nil {
return http.StatusServiceUnavailable, fmt.Errorf("checking movie: %w", err)
Expand Down Expand Up @@ -81,7 +81,7 @@ func (c *Client) radarrAddMovie(r *http.Request) (int, interface{}) {

if payload.Title == "" {
// Title must exist, even if it's wrong.
payload.Title = strconv.Itoa(payload.TmdbID)
payload.Title = strconv.FormatInt(payload.TmdbID, 10)
}

if payload.MinimumAvailability == "" {
Expand Down
6 changes: 3 additions & 3 deletions dnclient/readarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *Client) readarrMetaProfiles(r *http.Request) (int, interface{}) {
}

// Format profile ID=>Name into a nice map.
p := make(map[int]string)
p := make(map[int64]string)
for i := range profiles {
p[profiles[i].ID] = profiles[i].Name
}
Expand All @@ -60,7 +60,7 @@ func (c *Client) readarrProfiles(r *http.Request) (int, interface{}) {
}

// Format profile ID=>Name into a nice map.
p := make(map[int]string)
p := make(map[int64]string)
for i := range profiles {
p[profiles[i].ID] = profiles[i].Name
}
Expand All @@ -69,7 +69,7 @@ func (c *Client) readarrProfiles(r *http.Request) (int, interface{}) {
}

func (c *Client) readarrCheckBook(r *http.Request) (int, interface{}) {
grid, _ := strconv.Atoi(mux.Vars(r)["grid"])
grid, _ := strconv.ParseInt(mux.Vars(r)["grid"], 10, 64)
// Check for existing book.
if m, err := getReadarr(r).GetBook(grid); err != nil {
return http.StatusServiceUnavailable, fmt.Errorf("checking book: %w", err)
Expand Down
6 changes: 3 additions & 3 deletions dnclient/sonarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *Client) sonarrProfiles(r *http.Request) (int, interface{}) {
}

// Format profile ID=>Name into a nice map.
p := make(map[int]string)
p := make(map[int64]string)
for i := range profiles {
p[profiles[i].ID] = profiles[i].Name
}
Expand All @@ -60,7 +60,7 @@ func (c *Client) sonarrLangProfiles(r *http.Request) (int, interface{}) {
}

// Format profile ID=>Name into a nice map.
p := make(map[int]string)
p := make(map[int64]string)
for i := range profiles {
p[profiles[i].ID] = profiles[i].Name
}
Expand All @@ -69,7 +69,7 @@ func (c *Client) sonarrLangProfiles(r *http.Request) (int, interface{}) {
}

func (c *Client) sonarrCheckSeries(r *http.Request) (int, interface{}) {
tvdbid, _ := strconv.Atoi(mux.Vars(r)["tvdbid"])
tvdbid, _ := strconv.ParseInt(mux.Vars(r)["tvdbid"], 10, 64)
// Check for existing series.
if m, err := getSonarr(r).GetSeries(tvdbid); err != nil {
return http.StatusServiceUnavailable, fmt.Errorf("checking series: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion dnclient/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c *Client) RunWebServer() {
c.sonarrHandlers()

// Initialize "special" internal API paths.
c.router.Handle(path.Join("/", c.Config.WebRoot, "api", "status"), // does not return any data
c.router.Handle(path.Join("/", c.Config.URLBase, "api", "status"), // does not return any data
c.responseWrapper(c.statusResponse)).Methods("GET", "HEAD") // does not require a key
c.handleAPIpath("", "version", c.versionResponse, "GET", "HEAD") // requires a key

Expand Down
4 changes: 2 additions & 2 deletions examples/dnclient.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ debug = false
quiet = false

# All API paths start with /api.
# Use this to set it to /somethingelse/api by setting webroot to "/somethingelse"
webroot = "/"
# Set it to /somethingelse/api by setting urlbase to "/somethingelse"
urlbase = "/"

# If you provide a cert and key file (pem) paths, this app will listen with SSL/TLS.
# Uncomment both lines and add valid file paths. Make sure this app can read them.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ require (
github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c
golift.io/cnfg v0.0.7
golift.io/rotatorr v0.0.0-20201213130124-94efc0b9aff1
golift.io/starr v0.9.5-0.20201221075615-9f8d9de6dad0
golift.io/starr v0.9.5-0.20201222022736-eaadaabe9989
golift.io/version v0.0.2
)
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c h1:zqmyTlQyufRC65JnImJ6H1Sf7BDj8bG31EV919NVEQc=
github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -20,8 +22,12 @@ golift.io/cnfg v0.0.7 h1:qkNpP5Bq+5Gtoc6HcI8kapMD5zFOVan6qguxqBQF3OY=
golift.io/cnfg v0.0.7/go.mod h1:AsB0DJe7nv0bizKaoy3e3MjjOF7upTpMOMvsfv4CNNk=
golift.io/rotatorr v0.0.0-20201213130124-94efc0b9aff1 h1:8SDkFT5QpXyN24BCPw5Yux7SiMExnbL8o2hK1yfW3pk=
golift.io/rotatorr v0.0.0-20201213130124-94efc0b9aff1/go.mod h1:EZevRvIGRh8jDMwuYL0/tlPns0KynquPZzb0SerIC1s=
golift.io/starr v0.9.5-0.20201220000327-b89a1998b616 h1:rCRKgZvaJRoyOhK06BKFALPJnmOSezqoqyMikMNrrRw=
golift.io/starr v0.9.5-0.20201220000327-b89a1998b616/go.mod h1:f9tA8CmUzI7QNtZlhIrpVEX9RhB7dcPQBwbq1ice5DE=
golift.io/starr v0.9.4 h1:lKs33b22mLOTwebqv1+Z5Vkv6BJ1oth8g16jlAZxHyk=
golift.io/starr v0.9.4/go.mod h1:vc04EcdpqkBG7jCySmehqtlQEJ1nrEYeqZmDV1Xpj3k=
golift.io/starr v0.9.5-0.20201221214402-b08bed538e3a h1://hGYlq5/vSfMAzyDS/fOsSoBku1++tAKBisHSm/uuQ=
golift.io/starr v0.9.5-0.20201221214402-b08bed538e3a/go.mod h1:EE8B7OlqZlE/EGmBP1bLsK1OHEgWwbNpyjDXX0B2f0Y=
golift.io/starr v0.9.5-0.20201222022736-eaadaabe9989 h1:tsjSnwtW1gxWmY3e50LqQacAE6uWNPmuLN8EZXD7h+c=
golift.io/starr v0.9.5-0.20201222022736-eaadaabe9989/go.mod h1:EE8B7OlqZlE/EGmBP1bLsK1OHEgWwbNpyjDXX0B2f0Y=
golift.io/version v0.0.2 h1:i0gXRuSDHKs4O0sVDUg4+vNIuOxYoXhaxspftu2FRTE=
golift.io/version v0.0.2/go.mod h1:76aHNz8/Pm7CbuxIsDi97jABL5Zui3f2uZxDm4vB6hU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit f9eb81b

Please sign in to comment.