Build a simple server using Robin Web Framework
“Robin is a fast, high-performance Python web framework with Rust runtime. It is designed to provide near-native Rust throughput while taking advantage of the code being written in Python. Robin is on par with web servers from other products such as Flask, FastAPI, Django and the likes. One of the major advantages of Robin is that it does not require an external web server for production, making it more efficient and streamlined.
More about Robin Here Shankar’s blog.
- Under active development!
- written in rust, btw xD
- a multithreaded runtime
- extensible
- a simple api
- Sync and asynchronous function support
- Dynamic URL Routing
- multi-core scaling
- websocket!
- middleware
- hot reloading
- Community first and truly FOSS!
py -m venv venv cd venv/Scripts activate
pip install robyn
Let’s write a simple endpoint that returns “hello world”. we create a file named app.py
,
We import Robin and create a Robin instance. in this matter, app
Char.
Then, we define a path operation decorator. when a request goes to the path /
using a get
operation, the hello
The function will handle the request and “Hello, World!” Will respond with
To run the server, we write in our terminal:
index.html
Hello, World!
here, we import static file
object, and inside the handler, we pass the path to the HTML file we want to serve.
this handler returns fake_fruit_database
List as a JSON.
get id by handler
helper.py
Contains a function to help us look up a list and return a dictionary with associated id
,
In fruit/:id
handler, we are adding the path parameter id
to bear fruit with id
pass.
The form of the request object is:
- to find the value of
id
we passed the keyparams
key first and thenid
, - if no
id
is matched, the handler returns a “fruit not found” message. Otherwise, it returns a JSON response.
post handler
In the handler below, we extract the body from the request object and decode it:
then, we use json.loads()
Method to deserialize and convert the body to a python dictionary.
we generate a id
For fruit we are adding. We create a new dictionary and add it to the list of fruits. and return the status code 201
And a JSON as body response.
add handler
we need to get out id
The parameters and body for this handler.
Then, we search with the fruit id
passed and replaces the fruit value with the one in the request body.
remove handler
Here, we remove the dictionary from the list of fruits id
passed as a parameter.
Default PORT
And URL
Huh 5000
And 127.0.0.1
respectively.
If we want to start the server on a different port and url, we need to pass the values app.start()
,
app.start(port=8000, url="0.0.0.0")
I’ve been learning Rust for a few months, and I thought it was interesting to use Rust to build a Python web framework and leverage the power of Rust. I was excited to use and write about Robin. And I’m excited to follow its development. Let me know if you have any recommendations about other packages, architectures, etc.
source code is available Here, Thank you for reading.