DEV Community

loading...

Practical Rust Web Development - CI Travis

werner profile image Werner Echezuría ・2 min read

Tools like Travis can be really helpful to figure it out the stability of our software, specially when we have a huge codebase that requires a lot of time to compile and test.

In this post I'll show how to integrate the Travis CI for our project, because it's open source we can use the Travis free account.

We need to perform a couple of things, first we need to enable our project for Travis CI in the repositories page. Then we add a .travis.yml file in the root with the next configuration:

.travis.yml

language: rust
rust:
  - stable
  - beta
  - nightly
matrix:
  allow_failures:
    - rust: nightly
  fast_finish: true
cache: cargo
services:
  - postgresql
addons:
  postgresql: "10"
  apt:
    packages:
      - postgresql-10
      - postgresql-client-10
env:
  global:
    - PGPORT: 5433
before_script:
  - sudo sed -i -e '/local.*peer/s/postgres/all/' -e 's/peer\|md5/trust/g' /etc/postgresql/*/main/pg_hba.conf
  - sudo service postgresql restart
  - sleep 1
  - sudo apt-get update
  - sudo apt-get install -y postgresql-10-rum
  - psql -c 'create database mystore_test;' -U postgres
  - psql -c 'CREATE EXTENSION rum;' -d mystore_test -U postgres
script:
  - cargo install diesel_cli --quiet || true
  - cargo build --verbose --all
  - DATABASE_URL=postgres://postgres:@localhost/mystore_test diesel setup
  - cargo test --verbose --all

Although we are targeting stable, we might want to test it against all the compiler targets and see if our code does not break some new functionality.

We enable postgres v10 and write the scripts we're gonna need previous the test.

One last thing is add badge to the README, so people can see if our software is stable.

README.md:

# practical-rust-web
[![Build Status](https://travis-ci.org/practical-rust-web-development/mystore.svg?branch=master)](https://travis-ci.org/practical-rust-web-development/mystore)

You can take a look at the source code here

Discussion (12)

pic
Editor guide
Collapse
laynath242 profile image
LayNath242 • Edited

hello teacher.i have user table and role table.

select fullname, roles.title from users inner join roles on roles.id = users.role_id;

how to write this in rust.
thank you.

Collapse
aka_dude profile image
Collapse
laynath242 profile image
LayNath242

thank you, but i'm so beginner i still don't understand.

Thread Thread
aka_dude profile image
Andrew Andersen

Do you know Rust?

Thread Thread
laynath242 profile image
LayNath242

know but not much.

Collapse
deciduously profile image
Ben Lovy

Check out include_sql. Diesel is a full featured ORM. Not all projects need it, you can of course write SQL directly.

Collapse
laynath242 profile image
LayNath242

thank you.

Collapse
laynath242 profile image
LayNath242 • Edited

Hello. I do this and got all data. but i only need only username and title. How to do this.Thank you.

[derive(Serialize, Deserialize)]

pub struct UserRoleList(pub Vec<(Role, User)>);

impl UserRoleList {
pub fn list(connection: &PgConnection) -> Self {
use crate::schema::{roles,users};
use diesel::RunQueryDsl;
use diesel::QueryDsl;

    let result = roles::table.inner_join(users::table)
                .load::<(Role, User)>(connection)
                .expect("Error loading ");
        UserRoleList(result)
}

}

Collapse
werner profile image
Werner Echezuría Author

Hi, like SQL you can use a select clause, for example: github.com/practical-rust-web-deve...

Collapse
laynath242 profile image
LayNath242

thank you

Collapse
leekheang profile image
LEE KHEANG • Edited

hi teacher, I have a follower ur code, first auth successfully I get data, but now I can't get data after I log in ( i test with postman )

Collapse
werner profile image
Werner Echezuría Author

Hi, you need to register an user, for example:

➜  mystore git:(master) curl -i --request POST \                                                                                          <<<
  --url http://localhost:8088/register \
  --header 'content-type: application/json' \
  --data '{
        "email": "name@domain.com",
        "company": "My Company",
        "password":"password",
        "password_confirmation": "password"
}'
HTTP/1.1 200 OK
content-length: 92
content-type: application/json
date: Tue, 06 Aug 2019 12:38:34 GMT

{"email":"name@domain.com","company":"My Company","created_at":"2019-08-06T09:38:41.833136"}%                                                 

Then you need to login:

➜  mystore git:(master) curl -i --request POST \                                                                                          <<<
  --url http://localhost:8088/auth \
  --header 'content-type: application/json' \
  --data '{
        "email": "name@domain.com",
        "password":"password"
}'
HTTP/1.1 200 OK
content-length: 92
content-type: application/json
x-csrf-token: 41130a33c627781f7dd0f81d95a33ca4223b8d85b322386793aacb97aa13833115b85913951dafceead03516e870f682d077e300c0ceb030371984d9133ca002991aec8fad22c391
set-cookie: mystorejwt=4LKhublDMvdz0/tU/lZVLQAwj8pSqNLsRl8heiOM8LMyH3pekc7PSdEit09j4eXYFCMcLt+2h+Wxf2U1aoDN2/8J+3hVt/61z3EXTBnGNe/oig3qh0Ftz2mt6OBXjwot5EjogsXtFEbje4TSMby0zE3yy+xOLhejEseBdABz0F6YhNls8zPw2UA7bH+RZ6Fa471Ph83IU0XVhqHDcgz9uQAtlgTs/BnX6EorDK2xTmIDgiWRWr6DCNB005sf76rT4emFbV6DlORe95s8Hh702Q==; HttpOnly; Path=/; Domain=localhost; Max-Age=86400
date: Tue, 06 Aug 2019 12:40:04 GMT

{"email":"name@domain.com","company":"My Company","created_at":"2019-08-06T09:38:41.833136"}%                                                 

You copy the cookie and csrf token and pass them through the request:

curl -v --cookie "mystorejwt=4LKhublDMvdz0/tU/lZVLQAwj8pSqNLsRl8heiOM8LMyH3pekc7PSdEit09j4eXYFCMcLt+2h+Wxf2U1aoDN2/8J+3hVt/61z3EXTBnGNe/oig3qh0Ftz2mt6OBXjwot5EjogsXtFEbje4TSMby0zE3yy+xOLhejEseBdABz0F6YhNls8zPw2UA7bH+RZ6Fa471Ph83IU0XVhqHDcgz9uQAtlgTs/BnX6EorDK2xTmIDgiWRWr6DCNB005sf76rT4emFbV6DlORe95s8Hh702Q==; Path=/; Domain=localhost; Max-Age=86400" --header "x-csrf-token: 41130a33c627781f7dd0f81d95a33ca4223b8d85b322386793aacb97aa13833115b85913951dafceead03516e870f682d077e300c0ceb030371984d9133ca002991aec8fad22c391" -H "Content-Type: application/json"  http://localhost:8088/products\?search\="products"