feat: wire compression and cache middlewares into router
This commit is contained in:
@@ -26,7 +26,7 @@ func ProvideRouter(cfg config.Config, htmlRender render.HTMLRender) *gin.Engine
|
|||||||
gin.SetMode(cfg.GinMode)
|
gin.SetMode(cfg.GinMode)
|
||||||
}
|
}
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
r.Use(CORSMiddlewareWithConfig(cfg), RequestContextMiddleware(), audit.ContextMiddleware(), RequestLogger(), gin.Recovery())
|
r.Use(CORSMiddlewareWithConfig(cfg), RequestContextMiddleware(), audit.ContextMiddleware(), RequestLogger(), CompressionMiddleware(), StaticCacheMiddleware(), gin.Recovery())
|
||||||
r.NoRoute(func(c *gin.Context) {
|
r.NoRoute(func(c *gin.Context) {
|
||||||
if acceptsHTML(c) {
|
if acceptsHTML(c) {
|
||||||
c.HTML(http.StatusNotFound, "not_found.gohtml", gin.H{
|
c.HTML(http.StatusNotFound, "not_found.gohtml", gin.H{
|
||||||
|
|||||||
@@ -81,19 +81,16 @@ type cacheControlWriter struct {
|
|||||||
policy string
|
policy string
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:unused
|
|
||||||
func (w *cacheControlWriter) WriteHeader(status int) {
|
func (w *cacheControlWriter) WriteHeader(status int) {
|
||||||
w.apply(status)
|
w.apply(status)
|
||||||
w.ResponseWriter.WriteHeader(status)
|
w.ResponseWriter.WriteHeader(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:unused
|
|
||||||
func (w *cacheControlWriter) WriteHeaderNow() {
|
func (w *cacheControlWriter) WriteHeaderNow() {
|
||||||
w.apply(w.Status())
|
w.apply(w.Status())
|
||||||
w.ResponseWriter.WriteHeaderNow()
|
w.ResponseWriter.WriteHeaderNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:unused
|
|
||||||
func (w *cacheControlWriter) Write(data []byte) (int, error) {
|
func (w *cacheControlWriter) Write(data []byte) (int, error) {
|
||||||
if !w.Written() {
|
if !w.Written() {
|
||||||
w.apply(w.Status())
|
w.apply(w.Status())
|
||||||
@@ -101,7 +98,6 @@ func (w *cacheControlWriter) Write(data []byte) (int, error) {
|
|||||||
return w.ResponseWriter.Write(data)
|
return w.ResponseWriter.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:unused
|
|
||||||
func (w *cacheControlWriter) WriteString(data string) (int, error) {
|
func (w *cacheControlWriter) WriteString(data string) (int, error) {
|
||||||
if !w.Written() {
|
if !w.Written() {
|
||||||
w.apply(w.Status())
|
w.apply(w.Status())
|
||||||
@@ -109,7 +105,6 @@ func (w *cacheControlWriter) WriteString(data string) (int, error) {
|
|||||||
return w.ResponseWriter.WriteString(data)
|
return w.ResponseWriter.WriteString(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:unused
|
|
||||||
func (w *cacheControlWriter) Flush() {
|
func (w *cacheControlWriter) Flush() {
|
||||||
if !w.Written() {
|
if !w.Written() {
|
||||||
w.apply(w.Status())
|
w.apply(w.Status())
|
||||||
@@ -117,12 +112,10 @@ func (w *cacheControlWriter) Flush() {
|
|||||||
w.ResponseWriter.Flush()
|
w.ResponseWriter.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:unused
|
|
||||||
func (w *cacheControlWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
func (w *cacheControlWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||||
return w.ResponseWriter.Hijack()
|
return w.ResponseWriter.Hijack()
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:unused
|
|
||||||
func (w *cacheControlWriter) apply(status int) {
|
func (w *cacheControlWriter) apply(status int) {
|
||||||
if status >= http.StatusOK && status < http.StatusMultipleChoices || status == http.StatusNotModified {
|
if status >= http.StatusOK && status < http.StatusMultipleChoices || status == http.StatusNotModified {
|
||||||
w.Header().Set("Cache-Control", w.policy)
|
w.Header().Set("Cache-Control", w.policy)
|
||||||
@@ -131,7 +124,6 @@ func (w *cacheControlWriter) apply(status int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:unused
|
|
||||||
func StaticCacheMiddleware() gin.HandlerFunc {
|
func StaticCacheMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
if c.Request.Method != http.MethodGet && c.Request.Method != http.MethodHead {
|
if c.Request.Method != http.MethodGet && c.Request.Method != http.MethodHead {
|
||||||
|
|||||||
Reference in New Issue
Block a user