added gzip compression to sitemap endpoints
This commit is contained in:
33
main.go
33
main.go
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
"context"
|
"context"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"entgo.io/ent/dialect"
|
"entgo.io/ent/dialect"
|
||||||
@@ -1059,12 +1060,24 @@ func getSiteMapIndex(c *gin.Context) {
|
|||||||
return
|
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 {
|
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.Header("Content-Type", "application/xml")
|
||||||
c.Status(http.StatusOK)
|
c.Status(http.StatusOK)
|
||||||
} else {
|
} 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
|
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 {
|
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.Header("Content-Type", "application/xml")
|
||||||
c.Status(http.StatusOK)
|
c.Status(http.StatusOK)
|
||||||
} else {
|
} else {
|
||||||
c.Data(http.StatusOK, "application/xml", res)
|
c.Data(http.StatusOK, "application/xml", compBytes.Bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user