added text

This commit is contained in:
Sander Hautvast 2022-06-08 22:41:22 +02:00
parent baa20031e0
commit 0c94f8331f
2 changed files with 54 additions and 4 deletions

View file

@ -1,10 +1,10 @@
---
title: "Get Enterprisey with Rust, part2: input validation"
title: "Get Enterprisey with Rust part 2 - Input Validation"
date: 2022-05-30T17:50:11+02:00
author: Sander Hautvast
draft: false
---
Input validation is next on the list of enterprisey features (see also [part1](/enterprisey)). This will be the only new feature to add in this post because we need to add quite a but of boilerplate (sadly) to get it working.
Input validation is next on the list of enterprisey features (see also [part 1](/enterprisey)). This will be the only new feature to add in this post because we need to add quite a but of boilerplate (sadly) to get it working.
First we need to add a service that will respond to a post request on the same url.
@ -121,7 +121,9 @@ http-body = "0.4.3"
async-trait = "0.1"
```
The code that we just added basically makes sure the post request passes through the validator. If we now execute
The good news is that this code is generic for your whole application. So I guess you can put it in a separate file and largely forget about it.
So now If we now execute:
{{<highlight bash "linenos=table">}}
curl http://localhost:3000/entries -X POST -d '{"created":"2022-05-30T17:09:00.000000Z", "title":"aha", "author":"a", "text": "2"}' -v -H "Content-Type:application/json"
{{</highlight>}}
@ -134,3 +136,8 @@ Input validation error: [ValidationError(ValidationErrors({
"text": Field([ValidationError { code: "length", message: Some("text length must be at least 10"), params: {"value": String("2"), "min": Number(10)} }]),
"title": Field([ValidationError { code: "length", message: Some("Title length must be between 10 and 100"), params: {"max": Number(100), "value": String("aha"), "min": Number(10)} }])}))]%
{{</highlight>}}
And voila!
### Final remark
Check out the [crate documentation](https://docs.rs/validator/0.15.0/validator/) for all available checks. Like in javax.validation you can also define completely custom ones.

View file

@ -0,0 +1,43 @@
---
title: "Get Enterprisey with Rust part 3 - Environments"
date: 2022-06-01T17:28:32+02:00
draft: false
---
This is the third installment in our series about dealing with enterprisey needs using rust.
* [part 1](/enterprisey) was about the initial setup with axum and postgres, logging and dates.
* [part 2](/enterprisey) was devoted to input validation on incoming Json objects.
### Reading environment variables
I realized that the code I put in for part 1 on github already uses the built-in ```std::env::var``` function, but I didn't mention it in the blog.
{{<highlight rust "linenos=table">}}
let db_connection_str = std::env::var("DATABASE_URL")
.unwrap_or_else(|_| "postgres://postgres:1234@localhost".to_string());
{{</highlight>}}
The ```std::env::var``` function returns a ```Result``` that either contains the environment variable, or in the case that it is not found, the default, supplied in ```unwrap_or_else```.
Check out more [documentation](https://doc.rust-lang.org/book/ch12-05-working-with-environment-variables.html).
### Dotenv
[Dotenv](https://crates.io/crates/dotenv) is another option. Upon executing ```dotenv().ok()``` it reads a file with called _.env_ (in the current directory, or any parent) and puts the contents in your environment as variables. This might be more convient depending on your circumstances.
```Cargo.toml
dotenv = "0.15.0"
```
The code is now:
{{<highlight rust "linenos=table">}}
dotenv::dotenv().ok();
let db_connection_str = std::env::var("DATABASE_URL").unwrap();
{{</highlight>}}
Note that the ```unwrap``` function will abort the program, in case the DATABASE_URL is not found, but I also added _.env_ to the project root, so the program will run just fine (using cargo in the project directory). Of course, when deployed you need to make sure the file can be found and you must supply a environment specific version of it (to the container).
```
DATABASE_URL=postgres://postgres:1234@localhost
```
Dotenv also allows variable substitution within _.env_ files, allowing for less duplication.