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
}
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) {
// Start timer
startTime := time.Now()
@ -360,10 +367,3 @@ func loggingMiddleware(c *gin.Context) {
// Log result
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) {
c.JSON(200, gin.H{
koJSON(c, 200, gin.H{
"authorized": "OK",
})
}
@ -121,7 +121,7 @@ func (api *API) koSetProgress(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{
koJSON(c, http.StatusOK, gin.H{
"document": progress.DocumentID,
"timestamp": progress.CreatedAt,
})
@ -147,7 +147,7 @@ func (api *API) koGetProgress(c *gin.Context) {
if err == sql.ErrNoRows {
// Not Found
c.JSON(http.StatusOK, gin.H{})
koJSON(c, http.StatusOK, gin.H{})
return
} else if err != nil {
log.Error("GetDocumentProgress DB Error:", err)
@ -155,7 +155,7 @@ func (api *API) koGetProgress(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{
koJSON(c, http.StatusOK, gin.H{
"document": progress.DocumentID,
"percentage": progress.Percentage,
"progress": progress.Progress,
@ -247,7 +247,7 @@ func (api *API) koAddActivities(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{
koJSON(c, http.StatusOK, gin.H{
"added": len(rActivity.Activity),
})
}
@ -298,7 +298,7 @@ func (api *API) koCheckActivitySync(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{
koJSON(c, http.StatusOK, gin.H{
"last_sync": parsedTime.Unix(),
})
}
@ -352,7 +352,7 @@ func (api *API) koAddDocuments(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{
koJSON(c, http.StatusOK, gin.H{
"changed": len(rNewDocs.Documents),
})
}
@ -447,7 +447,7 @@ func (api *API) koCheckDocumentsSync(c *gin.Context) {
rCheckDocSync.Delete = deletedDocIDs
}
c.JSON(http.StatusOK, rCheckDocSync)
koJSON(c, http.StatusOK, rCheckDocSync)
}
func (api *API) koUploadExistingDocument(c *gin.Context) {
@ -534,7 +534,7 @@ func (api *API) koUploadExistingDocument(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{
koJSON(c, http.StatusOK, gin.H{
"status": "ok",
})
}
@ -589,3 +589,10 @@ func getFileMD5(filePath string) (*string, error) {
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)
}