Skip to content

Commit a14fc55

Browse files
committed
chore: better request logging, remove unused env
1 parent 0030628 commit a14fc55

6 files changed

Lines changed: 30 additions & 23 deletions

File tree

engine/handlers/image_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func serveDefaultImage(w http.ResponseWriter, r *http.Request, size catalog.Imag
105105
defaultImagePath := filepath.Join(catalog.SourceImageDir(), "default_img")
106106
if _, err = os.Stat(defaultImagePath); os.IsNotExist(err) {
107107
l.Debug().Msg("Default image does not exist in cache, attempting to move...")
108-
err = os.MkdirAll(filepath.Dir(defaultImagePath), 0755)
108+
err = os.MkdirAll(filepath.Dir(defaultImagePath), 0744)
109109
if err != nil {
110110
l.Err(err).Msg("Error when attempting to create image_cache/full dir")
111111
w.WriteHeader(http.StatusInternalServerError)

engine/handlers/replace_image.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func ReplaceImageHandler(store db.DB) http.HandlerFunc {
6969
l.Debug().Msg("Image identified as remote file")
7070
err = catalog.ValidateImageURL(fileUrl)
7171
if err != nil {
72+
l.Debug().AnErr("error", err).Msg("Invalid image")
7273
utils.WriteError(w, "url is invalid or not an image file", http.StatusBadRequest)
7374
return
7475
}

internal/catalog/images.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func saveImage(filename string, size ImageSize, data io.Reader) error {
133133
cacheDir := filepath.Join(configDir, ImageCacheDir)
134134

135135
// Ensure the cache directory exists
136-
err := os.MkdirAll(filepath.Join(cacheDir, string(size)), os.ModePerm)
136+
err := os.MkdirAll(filepath.Join(cacheDir, string(size)), 0744)
137137
if err != nil {
138138
return fmt.Errorf("failed to create full image cache directory: %w", err)
139139
}

internal/cfg/cfg.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99
)
1010

1111
const (
12-
defaultBaseUrl = "http://127.0.0.1"
12+
// defaultBaseUrl = "http://127.0.0.1"
1313
defaultListenPort = 4110
1414
defaultMusicBrainzUrl = "https://musicbrainz.org"
1515
)
1616

1717
const (
18-
BASE_URL_ENV = "KOITO_BASE_URL"
18+
// BASE_URL_ENV = "KOITO_BASE_URL"
1919
DATABASE_URL_ENV = "KOITO_DATABASE_URL"
2020
BIND_ADDR_ENV = "KOITO_BIND_ADDR"
2121
LISTEN_PORT_ENV = "KOITO_LISTEN_PORT"
@@ -26,7 +26,6 @@ const (
2626
ENABLE_LBZ_RELAY_ENV = "KOITO_ENABLE_LBZ_RELAY"
2727
LBZ_RELAY_URL_ENV = "KOITO_LBZ_RELAY_URL"
2828
LBZ_RELAY_TOKEN_ENV = "KOITO_LBZ_RELAY_TOKEN"
29-
LASTFM_API_KEY_ENV = "KOITO_LASTFM_API_KEY"
3029
CONFIG_DIR_ENV = "KOITO_CONFIG_DIR"
3130
DEFAULT_USERNAME_ENV = "KOITO_DEFAULT_USERNAME"
3231
DEFAULT_PASSWORD_ENV = "KOITO_DEFAULT_PASSWORD"
@@ -40,10 +39,10 @@ const (
4039
)
4140

4241
type config struct {
43-
bindAddr string
44-
listenPort int
45-
configDir string
46-
baseUrl string
42+
bindAddr string
43+
listenPort int
44+
configDir string
45+
// baseUrl string
4746
databaseUrl string
4847
musicBrainzUrl string
4948
logLevel int
@@ -82,10 +81,10 @@ func Load(getenv func(string) string) error {
8281
// loadConfig loads the configuration from environment variables.
8382
func loadConfig(getenv func(string) string) (*config, error) {
8483
cfg := new(config)
85-
cfg.baseUrl = getenv(BASE_URL_ENV)
86-
if cfg.baseUrl == "" {
87-
cfg.baseUrl = defaultBaseUrl
88-
}
84+
// cfg.baseUrl = getenv(BASE_URL_ENV)
85+
// if cfg.baseUrl == "" {
86+
// cfg.baseUrl = defaultBaseUrl
87+
// }
8988
cfg.databaseUrl = getenv(DATABASE_URL_ENV)
9089
if cfg.databaseUrl == "" {
9190
return nil, errors.New("required parameter " + DATABASE_URL_ENV + " not provided")
@@ -175,11 +174,11 @@ func ConfigDir() string {
175174
return globalConfig.configDir
176175
}
177176

178-
func BaseUrl() string {
179-
lock.RLock()
180-
defer lock.RUnlock()
181-
return globalConfig.baseUrl
182-
}
177+
// func BaseUrl() string {
178+
// lock.RLock()
179+
// defer lock.RUnlock()
180+
// return globalConfig.baseUrl
181+
// }
183182

184183
func DatabaseUrl() string {
185184
lock.RLock()

internal/images/deezer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ func (c *DeezerClient) queue(ctx context.Context, req *http.Request) ([]byte, er
7171
l.Debug().Err(err).Str("url", req.RequestURI).Msg("Failed to contact ImageSrc")
7272
done <- queue.RequestResult{Err: err}
7373
return
74+
} else if resp.StatusCode >= 300 || resp.StatusCode < 200 {
75+
err = fmt.Errorf("recieved non-ok status from Deezer: %s", resp.Status)
76+
done <- queue.RequestResult{Body: nil, Err: err}
7477
}
7578
defer resp.Body.Close()
7679

@@ -93,13 +96,13 @@ func (c *DeezerClient) getEntity(ctx context.Context, endpoint string, result an
9396
l.Debug().Msg("Adding ImageSrc request to queue")
9497
body, err := c.queue(ctx, req)
9598
if err != nil {
96-
l.Debug().Err(err)
99+
l.Err(err).Msg("Deezer request failed")
97100
return err
98101
}
99102

100103
err = json.Unmarshal(body, result)
101104
if err != nil {
102-
l.Debug().Err(err)
105+
l.Err(err).Msg("Failed to unmarshal Deezer response")
103106
return err
104107
}
105108

internal/mbz/mbz.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,19 @@ func (c *MusicBrainzClient) getEntity(ctx context.Context, fmtStr string, id uui
5151
url := fmt.Sprintf(fmtStr, c.url, id.String())
5252
req, err := http.NewRequest("GET", url, nil)
5353
if err != nil {
54+
l.Err(err).Msg("Failed to build MusicBrainz request")
5455
return err
5556
}
5657
l.Debug().Msg("Adding MusicBrainz request to queue")
5758
body, err := c.queue(ctx, req)
5859
if err != nil {
59-
l.Debug().Err(err)
60+
l.Err(err).Msg("MusicBrainz request failed")
6061
return err
6162
}
6263

6364
err = json.Unmarshal(body, result)
6465
if err != nil {
65-
l.Debug().Err(err)
66+
l.Err(err).Str("body", string(body)).Msg("Failed to unmarshal MusicBrainz response body")
6667
return err
6768
}
6869

@@ -77,9 +78,12 @@ func (c *MusicBrainzClient) queue(ctx context.Context, req *http.Request) ([]byt
7778
resultChan := c.requestQueue.Enqueue(func(client *http.Client, done chan<- queue.RequestResult) {
7879
resp, err := client.Do(req)
7980
if err != nil {
80-
l.Debug().Err(err).Str("url", req.RequestURI).Msg("Failed to contact MusicBrainz")
81+
l.Err(err).Str("url", req.RequestURI).Msg("Failed to contact MusicBrainz")
8182
done <- queue.RequestResult{Err: err}
8283
return
84+
} else if resp.StatusCode >= 300 || resp.StatusCode < 200 {
85+
err = fmt.Errorf("recieved non-ok status from MusicBrainz: %s", resp.Status)
86+
done <- queue.RequestResult{Body: nil, Err: err}
8387
}
8488
defer resp.Body.Close()
8589

0 commit comments

Comments
 (0)