echo icon indicating copy to clipboard operation
echo copied to clipboard

TTL (Cache-Control header in response) for static files

Open bernalandres opened this issue 4 years ago • 7 comments

Please add TTL (Cache-Control header) function for static files responses as some configuration parameter. Static(prefix, root, config) for example. Thanks!

Golang 1.16.5 Echo v4.3.0

bernalandres avatar Jun 29 '21 09:06 bernalandres

note to self:

e.Static signature should be changed to func (e *Echo) Static(prefix, root string, middleware ...MiddlewareFunc) *Route so additional middlewares could be set

aldas avatar Jun 29 '21 19:06 aldas

note to self:

e.Static signature should be changed to func (e *Echo) Static(prefix, root string, middleware ...MiddlewareFunc) *Route so additional middlewares could be set

I think it would be convenient to add the middleware to all the http methods (how has it, for example, fiber)

bernalandres avatar Jul 02 '21 00:07 bernalandres

Those how are missing that feature in v4 you can have a workaround.

Workaround for middlewares would be to add group with middlewares and add static route to that.

func main() {
	e := echo.New()

	rootMw := func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			e.Logger.Printf("in root level middleware: %v", c.Request().URL)
			return next(c)
		}
	}
	e.Use(rootMw)

	groupMw := func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			e.Logger.Printf("in group level middleware: %v", c.Request().URL)
			return next(c)
		}
	}
	g := e.Group("", groupMw)
	g.Static("/", "./")

	e.GET("/", func(c echo.Context) error {
		return c.JSON(http.StatusOK, map[string]string{"message": "OK"})
	})

	if err := e.Start(":8080"); err != http.ErrServerClosed {
		log.Fatal(err)
	}
}

CMD

x@x:~/code$ curl -v http://localhost:8080/
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8
< Date: Sat, 21 Aug 2021 14:06:26 GMT
< Content-Length: 17
< 
{"message":"OK"}
* Connection #0 to host localhost left intact

x@x:~/code$ curl -v http://localhost:8080/go.sum
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /go.sum HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Content-Length: 4449
< Content-Type: text/plain; charset=utf-8
< Last-Modified: Thu, 19 Aug 2021 18:57:54 GMT
< Date: Sat, 21 Aug 2021 14:07:01 GMT
< 
...
...

Application output:

⇨ http server started on [::]:8080
{"time":"2021-08-21T17:06:26.264115335+03:00","level":"-","prefix":"echo","file":"main.go","line":"24","message":"in root level middleware: /"}
{"time":"2021-08-21T17:07:01.086272264+03:00","level":"-","prefix":"echo","file":"main.go","line":"24","message":"in root level middleware: /go.sum"}
{"time":"2021-08-21T17:07:01.086294329+03:00","level":"-","prefix":"echo","file":"main.go","line":"32","message":"in group level middleware: /go.sum"}

aldas avatar Aug 21 '21 14:08 aldas

Those how are missing that feature in v4 you can have a workaround.

Workaround for middlewares would be to add group with middlewares and add static route to that.

func main() {
	e := echo.New()

	rootMw := func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			e.Logger.Printf("in root level middleware: %v", c.Request().URL)
			return next(c)
		}
	}
	e.Use(rootMw)

	groupMw := func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			e.Logger.Printf("in group level middleware: %v", c.Request().URL)
			return next(c)
		}
	}
	g := e.Group("", groupMw)
	g.Static("/", "./")

	e.GET("/", func(c echo.Context) error {
		return c.JSON(http.StatusOK, map[string]string{"message": "OK"})
	})

	if err := e.Start(":8080"); err != http.ErrServerClosed {
		log.Fatal(err)
	}
}

CMD

x@x:~/code$ curl -v http://localhost:8080/
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8
< Date: Sat, 21 Aug 2021 14:06:26 GMT
< Content-Length: 17
< 
{"message":"OK"}
* Connection #0 to host localhost left intact

x@x:~/code$ curl -v http://localhost:8080/go.sum
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /go.sum HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Content-Length: 4449
< Content-Type: text/plain; charset=utf-8
< Last-Modified: Thu, 19 Aug 2021 18:57:54 GMT
< Date: Sat, 21 Aug 2021 14:07:01 GMT
< 
...
...

Application output:

⇨ http server started on [::]:8080
{"time":"2021-08-21T17:06:26.264115335+03:00","level":"-","prefix":"echo","file":"main.go","line":"24","message":"in root level middleware: /"}
{"time":"2021-08-21T17:07:01.086272264+03:00","level":"-","prefix":"echo","file":"main.go","line":"24","message":"in root level middleware: /go.sum"}
{"time":"2021-08-21T17:07:01.086294329+03:00","level":"-","prefix":"echo","file":"main.go","line":"32","message":"in group level middleware: /go.sum"}

I believe I should mention to anyone reading the issue that this is something that should be done with care lest you tell browsers to cache 404s because you forgot to add a file to the static folder.

oka-tan avatar Sep 12 '22 19:09 oka-tan