David
David

Reputation: 10222

R RestRserve Add Etag to static path

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?

Upvotes: 0

Views: 53

Answers (1)

David
David

Reputation: 10222

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

Upvotes: 2

Related Questions