rexyai / RestRserve

R web API framework for building high-performance microservices and app backends
https://restrserve.org
271 stars 31 forks source link

serving static files #196

Closed mentik closed 1 year ago

mentik commented 1 year ago

I had an issue when using RestRServe to serve static files at the same time, for example, js and css script, some files response errors, so I use httpuv to do that with different ports. Can we mix httpuv performance to RestRServe for serving static files, so that can use the same port address or there another method to increase the performance of the static file on RestRServe? thank you in advance

s-u commented 1 year ago

Rserve offers a proxy which serves static files directly and supports things like conditional GET and pre-compressed files, that's what we use in productions systems. Only R-based requests are then routed to R itself (see the RCloud R script setup). RestRserve could manage similar setup. It is on my todo list wrap all the options in an easy R wrapper.

s-u commented 1 year ago

(FWIW I did some quick benchmarks of Rserve's forward vs the httpuv package for static paths and they are very clear: 14k/2.7k request/s in Rserve vs 4.3k/0.17k requests/s in httpuv where the first number is using keep-alive, second is without keep-alive [tested on macOS Intel iMac] -- so it would be definitely worth enabling that in RestRserve)

s-u commented 1 year ago

I have added an experimental API to Rserve that allows even the in-session HTTP servers to serve static paths without the use of R. Mappings from entry points can be added with Rserve:::Rserve.http.add.static(). Example:

Rserve:::Rserve.http.add.static("/", "./", last=TRUE)

will make the HTTP server act as a webserver for the current directory (this is equivalent to forward -r . when using the Rserve forward proxy). If last=FALSE (default) then next handlers will be tried if the file is not found so R handlers can be used as fall-back even if the static handler matches.

This is mainly for @dselivanov since I presume RestRserve will want to hide the low-level details, but you can use it directly if you want to test the performance since the static handlers get executed before RestRserve middleware gets control.

dselivanov commented 1 year ago

had an issue when using RestRServe to serve static files at the same time, for example, js and css script, some files response errors

A bug report would be useful here!

there another method to increase the performance of the static file on RestRServe

Some minimal code to benchmark httpuv vs RestRserve would be useful.

I have added an experimental API to Rserve that allows even the in-session HTTP servers to serve static paths without the use of R

Thanks Simon! There is definitely noticeable RestRserve overhead (usage of R6 classes is not free, etc). While this will match httpuv behaviour, I need to think how to include it to the RestRserve API. As you mentioned it will take control before any middleware. Considering authentication and authorization this might be surprising/dangerous.

mentik commented 1 year ago

@s-u if there a detail documentation for RServe, sorry, but i am not too familiar with the documentation of RServe, now we used the alternative method with cdn.

@dselivanov okay, will add the bug report Thank you all, in advanced