R RestRserve 将 Etag 添加到静态路径

发布于 2025-01-15 14:02:14 字数 1270 浏览 2 评论 0原文

假设我有一个像这样使用 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 Fiber的示例

使用GO Fiber,我会像这样使用它:

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

有没有一种方法可以使用将Etag标头添加到静态文件中休息保留吗?

Let's say I have a REST API using RestRserve like this, is there a way to add an Etag to enable caching on cloud services?

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"

As we see, there is no Etag.

Example using Go fiber

Using GO fiber, I would use it like this:

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))
}

and then querying localhost:3000/myfile.txt I would see headers like this

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

Is there a way to add Etag headers to static files using RestRserve?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

物价感观 2025-01-22 14:02:14

从 RestRserve 版本 1.1.1(在 CRAN 上)开始,有一个 ETag 中间件类。
像这样使用它:

# ... code like before
etag <- ETagMiddleware$new()
app$append_middleware(etag)
# ... 

另请参阅 https://restrserve.org/reference/ETagMiddleware.html

As of RestRserve version 1.1.1 (on CRAN), there is an ETag Middleware class.
Use it like so:

# ... code like before
etag <- ETagMiddleware$new()
app$append_middleware(etag)
# ... 

See also https://restrserve.org/reference/ETagMiddleware.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文