1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00
examples/async_pg
2020-09-12 16:49:45 +01:00
..
sql Remove shell script and add more verbose instructions to README 2020-01-16 19:29:37 +01:00
src v3 examples (#364) 2020-09-12 16:49:45 +01:00
.gitignore Add dotenv and config crate to async_pg example 2020-01-16 18:45:11 +01:00
Cargo.toml v3 examples (#364) 2020-09-12 16:49:45 +01:00
README.md enhanced, renamed shutdown example and modified async_pg readme 2020-01-31 07:10:56 -05:00

async_pg example

This example illustrates

  • tokio_postgres
  • use of tokio_pg_mapper for postgres data mapping
  • deadpool_postgres for connection pooling
  • dotenv + config for configuration

Instructions

  1. Create database user

    createuser -P test_user
    

    Enter a password of your choice. The following instructions assume you used testing as password.

    This step is optional and you can also use an existing database user for that. Just make sure to replace test_user by the database user of your choice in the following steps and change the .env file containing the configuration accordingly.

  2. Create database

    createdb -O test_user testing_db
    
  3. Initialize database

    psql -f sql/schema.sql testing_db
    

    This step can be repeated and clears the database as it drops and recreates the schema testing which is used within the database.

  4. Create .env file:

    SERVER_ADDR=127.0.0.1:8080
    PG.USER=test_user
    PG.PASSWORD=testing
    PG.HOST=127.0.0.1
    PG.PORT=5432
    PG.DBNAME=testing_db
    PG.POOL.MAX_SIZE=16
    
  5. Run the server:

    cargo run
    
  6. Using a different terminal send an HTTP POST request to the running server:

    echo '{"email": "ferris@thecrab.com", "first_name": "ferris", "last_name": "crab", "username": "ferreal"}' | http -f --json --print h POST http://127.0.0.1:8080/users
    

    ...or using curl...

    curl -d '{"email": "ferris@thecrab.com", "first_name": "ferris", "last_name": "crab", "username": "ferreal"}' -H 'Content-Type: application/json' http://127.0.0.1:8080/users
    

    A unique constraint exists for username, so sending this request twice will return an internal server error (HTTP 500).