mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-15 18:35:55 -07:00
chore: better request logging, remove unused env
This commit is contained in:
parent
0030628db1
commit
a14fc55d55
6 changed files with 30 additions and 23 deletions
|
|
@ -105,7 +105,7 @@ func serveDefaultImage(w http.ResponseWriter, r *http.Request, size catalog.Imag
|
||||||
defaultImagePath := filepath.Join(catalog.SourceImageDir(), "default_img")
|
defaultImagePath := filepath.Join(catalog.SourceImageDir(), "default_img")
|
||||||
if _, err = os.Stat(defaultImagePath); os.IsNotExist(err) {
|
if _, err = os.Stat(defaultImagePath); os.IsNotExist(err) {
|
||||||
l.Debug().Msg("Default image does not exist in cache, attempting to move...")
|
l.Debug().Msg("Default image does not exist in cache, attempting to move...")
|
||||||
err = os.MkdirAll(filepath.Dir(defaultImagePath), 0755)
|
err = os.MkdirAll(filepath.Dir(defaultImagePath), 0744)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Err(err).Msg("Error when attempting to create image_cache/full dir")
|
l.Err(err).Msg("Error when attempting to create image_cache/full dir")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,7 @@ func ReplaceImageHandler(store db.DB) http.HandlerFunc {
|
||||||
l.Debug().Msg("Image identified as remote file")
|
l.Debug().Msg("Image identified as remote file")
|
||||||
err = catalog.ValidateImageURL(fileUrl)
|
err = catalog.ValidateImageURL(fileUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
l.Debug().AnErr("error", err).Msg("Invalid image")
|
||||||
utils.WriteError(w, "url is invalid or not an image file", http.StatusBadRequest)
|
utils.WriteError(w, "url is invalid or not an image file", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -133,7 +133,7 @@ func saveImage(filename string, size ImageSize, data io.Reader) error {
|
||||||
cacheDir := filepath.Join(configDir, ImageCacheDir)
|
cacheDir := filepath.Join(configDir, ImageCacheDir)
|
||||||
|
|
||||||
// Ensure the cache directory exists
|
// Ensure the cache directory exists
|
||||||
err := os.MkdirAll(filepath.Join(cacheDir, string(size)), os.ModePerm)
|
err := os.MkdirAll(filepath.Join(cacheDir, string(size)), 0744)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create full image cache directory: %w", err)
|
return fmt.Errorf("failed to create full image cache directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,13 +9,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultBaseUrl = "http://127.0.0.1"
|
// defaultBaseUrl = "http://127.0.0.1"
|
||||||
defaultListenPort = 4110
|
defaultListenPort = 4110
|
||||||
defaultMusicBrainzUrl = "https://musicbrainz.org"
|
defaultMusicBrainzUrl = "https://musicbrainz.org"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
BASE_URL_ENV = "KOITO_BASE_URL"
|
// BASE_URL_ENV = "KOITO_BASE_URL"
|
||||||
DATABASE_URL_ENV = "KOITO_DATABASE_URL"
|
DATABASE_URL_ENV = "KOITO_DATABASE_URL"
|
||||||
BIND_ADDR_ENV = "KOITO_BIND_ADDR"
|
BIND_ADDR_ENV = "KOITO_BIND_ADDR"
|
||||||
LISTEN_PORT_ENV = "KOITO_LISTEN_PORT"
|
LISTEN_PORT_ENV = "KOITO_LISTEN_PORT"
|
||||||
|
|
@ -26,7 +26,6 @@ const (
|
||||||
ENABLE_LBZ_RELAY_ENV = "KOITO_ENABLE_LBZ_RELAY"
|
ENABLE_LBZ_RELAY_ENV = "KOITO_ENABLE_LBZ_RELAY"
|
||||||
LBZ_RELAY_URL_ENV = "KOITO_LBZ_RELAY_URL"
|
LBZ_RELAY_URL_ENV = "KOITO_LBZ_RELAY_URL"
|
||||||
LBZ_RELAY_TOKEN_ENV = "KOITO_LBZ_RELAY_TOKEN"
|
LBZ_RELAY_TOKEN_ENV = "KOITO_LBZ_RELAY_TOKEN"
|
||||||
LASTFM_API_KEY_ENV = "KOITO_LASTFM_API_KEY"
|
|
||||||
CONFIG_DIR_ENV = "KOITO_CONFIG_DIR"
|
CONFIG_DIR_ENV = "KOITO_CONFIG_DIR"
|
||||||
DEFAULT_USERNAME_ENV = "KOITO_DEFAULT_USERNAME"
|
DEFAULT_USERNAME_ENV = "KOITO_DEFAULT_USERNAME"
|
||||||
DEFAULT_PASSWORD_ENV = "KOITO_DEFAULT_PASSWORD"
|
DEFAULT_PASSWORD_ENV = "KOITO_DEFAULT_PASSWORD"
|
||||||
|
|
@ -40,10 +39,10 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
bindAddr string
|
bindAddr string
|
||||||
listenPort int
|
listenPort int
|
||||||
configDir string
|
configDir string
|
||||||
baseUrl string
|
// baseUrl string
|
||||||
databaseUrl string
|
databaseUrl string
|
||||||
musicBrainzUrl string
|
musicBrainzUrl string
|
||||||
logLevel int
|
logLevel int
|
||||||
|
|
@ -82,10 +81,10 @@ func Load(getenv func(string) string) error {
|
||||||
// loadConfig loads the configuration from environment variables.
|
// loadConfig loads the configuration from environment variables.
|
||||||
func loadConfig(getenv func(string) string) (*config, error) {
|
func loadConfig(getenv func(string) string) (*config, error) {
|
||||||
cfg := new(config)
|
cfg := new(config)
|
||||||
cfg.baseUrl = getenv(BASE_URL_ENV)
|
// cfg.baseUrl = getenv(BASE_URL_ENV)
|
||||||
if cfg.baseUrl == "" {
|
// if cfg.baseUrl == "" {
|
||||||
cfg.baseUrl = defaultBaseUrl
|
// cfg.baseUrl = defaultBaseUrl
|
||||||
}
|
// }
|
||||||
cfg.databaseUrl = getenv(DATABASE_URL_ENV)
|
cfg.databaseUrl = getenv(DATABASE_URL_ENV)
|
||||||
if cfg.databaseUrl == "" {
|
if cfg.databaseUrl == "" {
|
||||||
return nil, errors.New("required parameter " + DATABASE_URL_ENV + " not provided")
|
return nil, errors.New("required parameter " + DATABASE_URL_ENV + " not provided")
|
||||||
|
|
@ -175,11 +174,11 @@ func ConfigDir() string {
|
||||||
return globalConfig.configDir
|
return globalConfig.configDir
|
||||||
}
|
}
|
||||||
|
|
||||||
func BaseUrl() string {
|
// func BaseUrl() string {
|
||||||
lock.RLock()
|
// lock.RLock()
|
||||||
defer lock.RUnlock()
|
// defer lock.RUnlock()
|
||||||
return globalConfig.baseUrl
|
// return globalConfig.baseUrl
|
||||||
}
|
// }
|
||||||
|
|
||||||
func DatabaseUrl() string {
|
func DatabaseUrl() string {
|
||||||
lock.RLock()
|
lock.RLock()
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,9 @@ func (c *DeezerClient) queue(ctx context.Context, req *http.Request) ([]byte, er
|
||||||
l.Debug().Err(err).Str("url", req.RequestURI).Msg("Failed to contact ImageSrc")
|
l.Debug().Err(err).Str("url", req.RequestURI).Msg("Failed to contact ImageSrc")
|
||||||
done <- queue.RequestResult{Err: err}
|
done <- queue.RequestResult{Err: err}
|
||||||
return
|
return
|
||||||
|
} else if resp.StatusCode >= 300 || resp.StatusCode < 200 {
|
||||||
|
err = fmt.Errorf("recieved non-ok status from Deezer: %s", resp.Status)
|
||||||
|
done <- queue.RequestResult{Body: nil, Err: err}
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
|
@ -93,13 +96,13 @@ func (c *DeezerClient) getEntity(ctx context.Context, endpoint string, result an
|
||||||
l.Debug().Msg("Adding ImageSrc request to queue")
|
l.Debug().Msg("Adding ImageSrc request to queue")
|
||||||
body, err := c.queue(ctx, req)
|
body, err := c.queue(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Debug().Err(err)
|
l.Err(err).Msg("Deezer request failed")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, result)
|
err = json.Unmarshal(body, result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Debug().Err(err)
|
l.Err(err).Msg("Failed to unmarshal Deezer response")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,18 +51,19 @@ func (c *MusicBrainzClient) getEntity(ctx context.Context, fmtStr string, id uui
|
||||||
url := fmt.Sprintf(fmtStr, c.url, id.String())
|
url := fmt.Sprintf(fmtStr, c.url, id.String())
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
l.Err(err).Msg("Failed to build MusicBrainz request")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
l.Debug().Msg("Adding MusicBrainz request to queue")
|
l.Debug().Msg("Adding MusicBrainz request to queue")
|
||||||
body, err := c.queue(ctx, req)
|
body, err := c.queue(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Debug().Err(err)
|
l.Err(err).Msg("MusicBrainz request failed")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, result)
|
err = json.Unmarshal(body, result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Debug().Err(err)
|
l.Err(err).Str("body", string(body)).Msg("Failed to unmarshal MusicBrainz response body")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -77,9 +78,12 @@ func (c *MusicBrainzClient) queue(ctx context.Context, req *http.Request) ([]byt
|
||||||
resultChan := c.requestQueue.Enqueue(func(client *http.Client, done chan<- queue.RequestResult) {
|
resultChan := c.requestQueue.Enqueue(func(client *http.Client, done chan<- queue.RequestResult) {
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Debug().Err(err).Str("url", req.RequestURI).Msg("Failed to contact MusicBrainz")
|
l.Err(err).Str("url", req.RequestURI).Msg("Failed to contact MusicBrainz")
|
||||||
done <- queue.RequestResult{Err: err}
|
done <- queue.RequestResult{Err: err}
|
||||||
return
|
return
|
||||||
|
} else if resp.StatusCode >= 300 || resp.StatusCode < 200 {
|
||||||
|
err = fmt.Errorf("recieved non-ok status from MusicBrainz: %s", resp.Status)
|
||||||
|
done <- queue.RequestResult{Body: nil, Err: err}
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue