Some time ago I started to learn about rust-lang and after read and watch some stuffs I decided to give a try to create a basic CRUD using tide as a web framework.
This example ( and approach ) is heavy inspired in two awesome resources :
Les start by creating a new binary
project.
> If you don’t have rust installed, please check the install page and also can check my notes about rustup
and cargo
.
1
| $ cargo new tide-basic-crud && cd tide-basic-crud
|
Next we want to add tide
as dependency since is the http framework
I selected to use, we can add it by hand editing the Cargo.toml
file or install cargo-edit and use the add
command.
1
2
3
4
| $ cargo install cargo-edit
$ cargo add tide
Updating 'https://github.com/rust-lang/crates.io-index' index
Adding tide v0.13.0 to dependencies
|
Now following the getting started guide of tide add async-std
with the attributes
feature enable by adding this line to your deps ( in Cargo.toml
file ).
1
| async-std = { version = "1.6.0", features = ["attributes"] } |
And edit our main.rs
with the basic example.
1
2
3
4
5
6
7
8
| #[async_std::main]
async fn main() -> Result<(), std::io::Error> {
tide::log::start();
let mut app = tide::new();
app.at("/").get(|_| async { Ok("Hello, world!") });
app.listen("127.0.0.1:8080").await?;
Ok(())
}
|
Now we can compile and run the server with
1
2
3
4
5
6
7
8
9
| $ cargo run
[Running 'cargo run']
Compiling tide-basic-crud v0.1.0 (/Users/pepo/personal/rust/tide-basic-crud)
Finished dev [unoptimized + debuginfo] target(s) in 5.25s
Running `target/debug/tide-basic-crud`
tide::log Logger started
level Info
tide::listener::tcp_listener Server listening on http://127.0.0.1:8080
|
And check with curl
1
2
| $ curl localhost:8080
Hello, world!
|
Awesome! we have our server up and running, but at this point doesn’t do much so let’s add some code. For the purpose of this example let’s create a simple CRUD
that allow us to track dinosaurs
information.
We will recording the name
, weight
( in kilograms ) and the diet
( type ).
First let’s create a route for create a dinos
, adding the /dinos
route ( at ) with the verb post
and following the request/response concept
1
2
3
4
5
6
7
8
| (...)
app.at("/dinos").post(|mut req: Request<()>| async move {
let body = req.body_string().await?;
println!("{:?}", body);
let mut res = Response::new(201);
res.set_body(String::from("created!"));
Ok(res)
});
|
And test…
1
2
3
4
5
6
7
| $ curl -v -d '{"name":"velociraptor", "weight": 50, "diet":"carnivorous"}' http://localhost:8080/dinos
(...)
* upload completely sent off: 59 out of 59 bytes
< HTTP/1.1 201 Created
(...)
created!
|
And we can check the payload of the request in the logs
1
2
3
4
5
6
7
8
9
| tide::log::middleware <-- Request received
method POST
path /dinos
"{\"name\":\"velociraptor\", \"weight\": 50, \"diet\":\"carnivorous\"}"
tide::log::middleware --> Response sent
method POST
path /dinos
status 201
duration 59.609µs
|
Nice! But we get the body as string
and we need to parse as json
. If you are familiarized with node.js
and express
this could be done with the body-parser
middleware, but tide
can parse json
and form
(urlencoded) out of the box with body_json
and body_form
methods.
Let’s change body_string()
to body_json
and try again.
1
2
3
| curl -v -d '{"name":"velociraptor", "weight": 50, "diet":"carnivorous"}' http://localhost:8080/dinos
< HTTP/1.1 422 Unprocessable Entity
|
422 Unprocessable Entity
, doesn’t works as expected (or maybe yes). Tide
use serde
to deserialize the request body and need to be parse into
a struct. So, let’s create our Dino
struct and deserialize the body into.
1
2
3
4
5
6
| #[derive(Debug, Deserialize, Serialize)]
struct Dino {
name: String,
weight: u16,
diet: String
}
|
Here we use the derive
attributes to all to serialize/deserialize
, now let’s change the route to deserialize the body into the the Dino
struct and return the json
representation.
1
2
3
4
5
6
7
| app.at("/dinos").post(|mut req: Request<()>| async move {
let dino: Dino = req.body_json().await?;
println!("{:?}", dino);
let mut res = Response::new(201);
res.set_body(Body::from_json(&dino)?);
Ok(res)
});
|
And check if works as expected…
1
2
3
| $ curl -d '{"name":"velociraptor", "weight": 50, "diet":"carnivorous"}' http://localhost:8080/dinos
{"name":"velociraptor","weight":50,"diet":"carnivorous"}
|
Nice! let’s move forward and store those dinos
using a hashMap
to store a key/value
in memory. We will add a db persistence later.
Looking the tide
documentation, we can use tide::
with_state to create a server with shared application scoped state.
Let’s use the state to store our hashMap
.
1
2
3
4
| #[derive(Clone,Debug)]
struct State {
dinos: Arc<RwLock<HashMap<String,Dino>>>
}
|
We wrap
our hashMap in a mutex here to use in the State
( thanks to the tide awesome community for the tip ).
Then in the main fn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| let state = State {
dinos: Default::default()
};
let mut app = tide::with_state(state);
app.at("/").get(|_| async { Ok("Hello, world!") });
app.at("/dinos")
.post(|mut req: Request<State>| async move {
let dino: Dino = req.body_json().await?;
// let get a mut ref of our store ( hashMap )
let mut dinos = req.state().dinos.write().await;
dinos.insert(String::from(&dino.name), dino.clone());
let mut res = Response::new(201);
res.set_body(Body::from_json(&dino)?);
Ok(res)
})
|
Nice, let’s add a route to list
the dinos
and check how it’s works
1
2
3
4
5
6
7
8
9
| app.at("/dinos")
.get(|req: Request<State>| async move {
let dinos = req.state().dinos.read().await;
// get all the dinos as a vector
let dinos_vec: Vec<Dino> = dinos.values().cloned().collect();
let mut res = Response::new(200);
res.set_body(Body::from_json(&dinos_vec)?);
Ok(res)
})
|
1
2
3
4
5
6
7
| $ curl -d '{"name":"velociraptor", "weight": 50, "diet":"carnivorous"}' http://localhost:8080/dinos
$ curl -d '{"name":"t-rex", "weight": 5000, "diet":"carnivorous"}' http://localhost:8080/dinos
$ curl http://localhost:8080/dinos
[{"name":"velociraptor","weight":50,"diet":"carnivorous"},{"name":"t-rex","weight":5000,"diet":"carnivorous"}]
|
Nice! now let’s try getting and individual dino
…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| app.at("/dinos/:name")
.get(|req: Request<State>| async move {
let mut dinos = req.state().dinos.write().await;
let key: String = req.param("name")?;
let res = match dinos.entry(key) {
Entry::Vacant(_entry) => Response::new(404),
Entry::Occupied(entry) => {
let mut res = Response::new(200);
res.set_body(Body::from_json(&entry.get())?);
res
}
};
Ok(res)
})
|
We are using the entry
api and match
in the get to return the dino
or 404
if the requested name doesn’t exists.
1
2
3
4
5
6
7
| $ curl http://localhost:8080/dinos/t-rex
{"name":"t-rex","weight":5000,"diet":"carnivorous"}
$ curl -I http://localhost:8080/dinos/trex
HTTP/1.1 404 Not Found
content-length: 0
|
Awesome! We are almost there, add the missing routes to complete the CRUD
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| .put(|mut req: Request<State>| async move {
let dino_update: Dino = req.body_json().await?;
let mut dinos = req.state().dinos.write().await;
let key: String = req.param("name")?;
let res = match dinos.entry(key) {
Entry::Vacant(_entry) => Response::new(404),
Entry::Occupied(mut entry) => {
*entry.get_mut() = dino_update;
let mut res = Response::new(200);
res.set_body(Body::from_json(&entry.get())?);
res
}
};
Ok(res)
})
.delete(|req: Request<State>| async move {
let mut dinos = req.state().dinos.write().await;
let key: String = req.param("name")?;
let deleted = dinos.remove(&key);
let res = match deleted {
None => Response::new(404),
Some(_) => Response::new(204),
};
Ok(res)
});
|
Let’s try update…
1
2
3
4
5
| $ curl -v -X PUT -d '{"name":"t-rex", "weight": 5, "diet":"carnivorous"}' http://localhost:8080/dinos/t-rex
$ curl http://localhost:8080/dinos/t-rex
{"name":"t-rex","weight":5,"diet":"carnivorous"}
|
and delete
1
2
3
4
| $ curl -v -X DELETE http://localhost:8080/dinos/t-rex
$ curl -I http://localhost:8080/dinos/t-rex
HTTP/1.1 404 Not Found
|
We made it! a complete CRUD with tide. But that was a lot
of manual testing, let’s add some basic unit test to smooth the next steps.
First decouple our main
function of the server
creation allowing us to create a server without need to actually listen in any port.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| #[async_std::main]
async fn main() {
tide::log::start();
let dinos_store = Default::default();
let app = server(dinos_store).await;
app.listen("127.0.0.1:8080").await.unwrap();
}
async fn server(dinos_store: Arc<RwLock<HashMap<String, Dino>>>) -> Server<State> {
let state = State {
dinos: dinos_store, //Default::default(),
};
let mut app = tide::with_state(state);
app.at("/").get(|_| async { Ok("ok") });
(...)
app
|
Great, now we can add some basic test using the server
function using cargo
for running our tests. There is more information about tests in the cargo book but in general
Cargo can run your tests with the cargo test command. Cargo looks for tests to run in two places: in each of your src files and any tests in tests/. Tests in your src files should be unit tests, and tests in tests/ should be integration-style tests. As such, you’ll need to import your crates into the files in tests.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| #[async_std::test]
async fn list_dinos() -> tide::Result<()> {
use tide::http::{Method, Request, Response, Url};
let dino = Dino {
name: String::from("test"),
weight: 50,
diet: String::from("carnivorous"),
};
let mut dinos_store = HashMap::new();
dinos_store.insert(dino.name.clone(), dino);
let dinos: Vec<Dino> = dinos_store.values().cloned().collect();
let dinos_as_json_string = serde_json::to_string(&dinos)?;
let state = Arc::new(RwLock::new(dinos_store));
let app = server(state).await;
let url = Url::parse("https://example.com/dinos").unwrap();
let req = Request::new(Method::Get, url);
let mut res: Response = app.respond(req).await?;
let v = res.body_string().await?;
assert_eq!(dinos_as_json_string, v);
Ok(())
}
#[async_std::test]
async fn create_dino() -> tide::Result<()> {
use tide::http::{Method, Request, Response, Url};
let dino = Dino {
name: String::from("test"),
weight: 50,
diet: String::from("carnivorous"),
};
let dinos_store = HashMap::new();
let state = Arc::new(RwLock::new(dinos_store));
let app = server(state).await;
let url = Url::parse("https://example.com/dinos").unwrap();
let mut req = Request::new(Method::Post, url);
req.set_body(serde_json::to_string(&dino)?);
let res: Response = app.respond(req).await?;
assert_eq!(201, res.status());
Ok(())
}
|
This unit tests are inspired in this gist that was posted on the tide-users
discord channel.
The main idea here is that we can create our server ( app
), calling the endpoint
with a req
uest without need to make an actual http request
and have the server listen
in any port.
We can now run cargo test
1
2
3
4
5
6
7
8
9
10
11
12
13
| $ cargo test
Compiling tide-basic-crud v0.1.0 (/Users/pepo/personal/rust/tide-basic-crud)
Finished test [unoptimized + debuginfo] target(s) in 5.99s
Running target/debug/deps/tide_basic_crud-3d6db2bae3cd08a5
running 5 tests
test list_dinos ... ok
test index_page ... ok
test create_dino ... ok
test delete_dino ... ok
test update_dino ... ok
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
|
Just one more think to add, cargo
is an awesome tool and you can add more functionality like watching
changes in you code and react ( for example running test ) with cargo-watch.
This is all for now, my intent to learn by doing. I’m sure that there is other ways ( likely betters ) to create a crud
with tide
and any feedback is welcome :) .
In the next post I will refactor the code to move the dinos_store
to a database ( postgresql / sqlx ).
You can find the complete code in this repo Thanks!