fix(api): ko json content type
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Evan Reichard 2025-05-13 12:14:57 -04:00
parent 162adfbe16
commit 57f81e5dd7
2 changed files with 23 additions and 16 deletions

View File

@ -325,6 +325,13 @@ func (api *API) loadTemplates(
return nil return nil
} }
func (api *API) templateMiddleware(router *gin.Engine) gin.HandlerFunc {
return func(c *gin.Context) {
router.HTMLRender = *api.generateTemplates()
c.Next()
}
}
func loggingMiddleware(c *gin.Context) { func loggingMiddleware(c *gin.Context) {
// Start timer // Start timer
startTime := time.Now() startTime := time.Now()
@ -360,10 +367,3 @@ func loggingMiddleware(c *gin.Context) {
// Log result // Log result
log.WithFields(logData).Info(fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path)) log.WithFields(logData).Info(fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path))
} }
func (api *API) templateMiddleware(router *gin.Engine) gin.HandlerFunc {
return func(c *gin.Context) {
router.HTMLRender = *api.generateTemplates()
c.Next()
}
}

View File

@ -72,7 +72,7 @@ type requestDocumentID struct {
} }
func (api *API) koAuthorizeUser(c *gin.Context) { func (api *API) koAuthorizeUser(c *gin.Context) {
c.JSON(200, gin.H{ koJSON(c, 200, gin.H{
"authorized": "OK", "authorized": "OK",
}) })
} }
@ -121,7 +121,7 @@ func (api *API) koSetProgress(c *gin.Context) {
return return
} }
c.JSON(http.StatusOK, gin.H{ koJSON(c, http.StatusOK, gin.H{
"document": progress.DocumentID, "document": progress.DocumentID,
"timestamp": progress.CreatedAt, "timestamp": progress.CreatedAt,
}) })
@ -147,7 +147,7 @@ func (api *API) koGetProgress(c *gin.Context) {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
// Not Found // Not Found
c.JSON(http.StatusOK, gin.H{}) koJSON(c, http.StatusOK, gin.H{})
return return
} else if err != nil { } else if err != nil {
log.Error("GetDocumentProgress DB Error:", err) log.Error("GetDocumentProgress DB Error:", err)
@ -155,7 +155,7 @@ func (api *API) koGetProgress(c *gin.Context) {
return return
} }
c.JSON(http.StatusOK, gin.H{ koJSON(c, http.StatusOK, gin.H{
"document": progress.DocumentID, "document": progress.DocumentID,
"percentage": progress.Percentage, "percentage": progress.Percentage,
"progress": progress.Progress, "progress": progress.Progress,
@ -247,7 +247,7 @@ func (api *API) koAddActivities(c *gin.Context) {
return return
} }
c.JSON(http.StatusOK, gin.H{ koJSON(c, http.StatusOK, gin.H{
"added": len(rActivity.Activity), "added": len(rActivity.Activity),
}) })
} }
@ -298,7 +298,7 @@ func (api *API) koCheckActivitySync(c *gin.Context) {
return return
} }
c.JSON(http.StatusOK, gin.H{ koJSON(c, http.StatusOK, gin.H{
"last_sync": parsedTime.Unix(), "last_sync": parsedTime.Unix(),
}) })
} }
@ -352,7 +352,7 @@ func (api *API) koAddDocuments(c *gin.Context) {
return return
} }
c.JSON(http.StatusOK, gin.H{ koJSON(c, http.StatusOK, gin.H{
"changed": len(rNewDocs.Documents), "changed": len(rNewDocs.Documents),
}) })
} }
@ -447,7 +447,7 @@ func (api *API) koCheckDocumentsSync(c *gin.Context) {
rCheckDocSync.Delete = deletedDocIDs rCheckDocSync.Delete = deletedDocIDs
} }
c.JSON(http.StatusOK, rCheckDocSync) koJSON(c, http.StatusOK, rCheckDocSync)
} }
func (api *API) koUploadExistingDocument(c *gin.Context) { func (api *API) koUploadExistingDocument(c *gin.Context) {
@ -534,7 +534,7 @@ func (api *API) koUploadExistingDocument(c *gin.Context) {
return return
} }
c.JSON(http.StatusOK, gin.H{ koJSON(c, http.StatusOK, gin.H{
"status": "ok", "status": "ok",
}) })
} }
@ -589,3 +589,10 @@ func getFileMD5(filePath string) (*string, error) {
return &fileHash, nil return &fileHash, nil
} }
// koJSON forces koJSON Content-Type to only return `application/json`. This is addressing
// the following issue: https://github.com/koreader/koreader/issues/13629
func koJSON(c *gin.Context, code int, obj any) {
c.Header("Content-Type", "application/json")
c.JSON(code, obj)
}