Install plumber server
This is intended for a Ubuntu 20.04 server:
sudo apt-get install git-core libssl-dev libcurl4-gnutls-dev curl libsodium-dev libxml2-dev
sudo R
install.packages("plumber")
q("no")
mkdir plumberapp
cd plumberapp
sudo apt-get install git-core libssl-dev libcurl4-gnutls-dev curl libsodium-dev libxml2-dev
sudo R
install.packages("plumber")
q("no")
mkdir plumberapp
cd plumberapp
vi plumber.R
## Copy these lines
# plumber.R
#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function(msg="") {
list(msg = paste0("The message is: '", msg, "'"))
}
## End
# plumber.R
#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function(msg="") {
list(msg = paste0("The message is: '", msg, "'"))
}
## End
vi myapi.R
## Copy these lines
r <- plumber::plumb("/home/mgirond/plumberapp/plumber.R")
r$run(host="0.0.0.0", port=8000)
## End
vi my-api.service
## Copy these lines
[Unit]
Description=plumber service
[Service]
ExecStart=/usr/bin/Rscript /home/mgirond/plumberapp/myapi.R
[Install]
WantedBy=multi-user.target
## End
[Unit]
Description=plumber service
[Service]
ExecStart=/usr/bin/Rscript /home/mgirond/plumberapp/myapi.R
[Install]
WantedBy=multi-user.target
## End
sudo ufw allow 8000/tcp
cp my-api.service /etc/systemd/system/my-api.service
systemctl start my-api
systemctl enable my-api
sudo reboot
cp my-api.service /etc/systemd/system/my-api.service
systemctl start my-api
systemctl enable my-api
sudo reboot
Then it is possible to test the API:
curl "http://localhost:8000/echo?msg=hello"
or
curl "http://0.0.0.0:8000/echo?msg=hello"
curl "http://localhost:8000/echo?msg=hello"
or
curl "http://0.0.0.0:8000/echo?msg=hello"
Commentaires
Enregistrer un commentaire