webapi: Try cache update before returning error.

Rather than immediately returning an error if the cache is not
initialized, attempt to initialize it first. Without this change there
is only an attempt to initialize the cache once per minute.
This commit is contained in:
jholdstock 2023-09-18 19:23:25 +01:00 committed by Jamie Holdstock
parent 99dc97d6a3
commit c7ebe28501

View File

@ -98,12 +98,15 @@ func (w *WebAPI) withSession(store *sessions.CookieStore) gin.HandlerFunc {
// Server Error. // Server Error.
func (w *WebAPI) requireWebCache(c *gin.Context) { func (w *WebAPI) requireWebCache(c *gin.Context) {
if !w.cache.initialized() { if !w.cache.initialized() {
const str = "Cache is not yet initialized" // Try to initialize it now.
w.log.Errorf(str) err := w.cache.update()
c.String(http.StatusInternalServerError, str) if err != nil {
w.log.Errorf("Failed to initialize cache: %v", err)
c.String(http.StatusInternalServerError, "Cache is not initialized")
c.Abort() c.Abort()
return return
} }
}
c.Set(cacheKey, w.cache.getData()) c.Set(cacheKey, w.cache.getData())
} }