假设我有一个像这样使用RestRserve
的REST API,有没有办法添加Etag来启用云服务上的缓存?
writeLines("Hello World", "myfile.txt")
app <- Application$new(content_type = "application/json")
app$add_static("/", ".")
backend <- BackendRserve$new()
# backend$start(app, http_port = 8080)
req <- Request$new(path = "/myfile.txt", method = "GET")
app$process_request(req)$headers
#> $Server
#> [1] "RestRserve/0.4.1001"
正如我们所看到的,没有Etag。
使用Go光纤的示例
使用GO光纤,我会这样使用:
package main
import (
"flag"
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/etag"
)
var (
port = flag.String("port", ":3000", "Port to listen on")
)
func main() {
app := fiber.New()
app.Use(etag.New())
app.Static("/", ".")
log.Fatal(app.Listen(*port))
}
然后查询localhost:3000/myfile.txt
,我会看到类似于这个的标题
HTTP/1.1 200 OK
Date: Fri, 18 Mar 2022 13:13:44 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 12
Last-Modified: Fri, 21 Jan 2022 16:24:47 GMT
Etag: "12-823400506"
Connection: close
Hello World
有没有一种方法可以使用RestRserve将Etag标头添加到静态文件中?
RestRserve 1.1.1版(在CRAN上(有一个ETag中间件类。像这样使用:
# ... code like before
etag <- ETagMiddleware$new()
app$append_middleware(etag)
# ...
另请参阅https://restrserve.org/reference/ETagMiddleware.html