added gzip compression to sitemap endpoints
This commit is contained in:
33
main.go
33
main.go
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"entgo.io/ent/dialect"
|
||||
@@ -1059,12 +1060,24 @@ func getSiteMapIndex(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var compBytes bytes.Buffer
|
||||
compWriter := gzip.NewWriter(&compBytes)
|
||||
if _, err = compWriter.Write(res); err != nil {
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
if err = compWriter.Close(); err != nil {
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Header("Content-Encoding", "gzip")
|
||||
if c.Request.Method == http.MethodHead {
|
||||
c.Header("Content-Length", strconv.Itoa(len(res)))
|
||||
c.Header("Content-Length", strconv.Itoa(compBytes.Len()))
|
||||
c.Header("Content-Type", "application/xml")
|
||||
c.Status(http.StatusOK)
|
||||
} else {
|
||||
c.Data(http.StatusOK, "application/xml", res)
|
||||
c.Data(http.StatusOK, "application/xml", compBytes.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1091,12 +1104,24 @@ func getSiteMap(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var compBytes bytes.Buffer
|
||||
compWriter := gzip.NewWriter(&compBytes)
|
||||
if _, err = compWriter.Write(res); err != nil {
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
if err = compWriter.Close(); err != nil {
|
||||
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Header("Content-Encoding", "gzip")
|
||||
if c.Request.Method == http.MethodHead {
|
||||
c.Header("Content-Length", strconv.Itoa(len(res)))
|
||||
c.Header("Content-Length", strconv.Itoa(compBytes.Len()))
|
||||
c.Header("Content-Type", "application/xml")
|
||||
c.Status(http.StatusOK)
|
||||
} else {
|
||||
c.Data(http.StatusOK, "application/xml", res)
|
||||
c.Data(http.StatusOK, "application/xml", compBytes.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user