Add docker-compose and .env

This commit is contained in:
Matt Paul 2020-11-25 17:18:19 +00:00 committed by Ryan Leckey
parent b5504b27cd
commit 33521d6b74
4 changed files with 39 additions and 3 deletions

View file

@ -0,0 +1 @@
DATABASE_URL="postgres://postgres:password@localhost/todos"

View file

@ -6,19 +6,27 @@ This example is based on the ideas in [this blog post](https://medium.com/better
## Setup
1. Declare the database URL
1. Run `docker-compose up -d` to run Postgres in the background.
2. Declare the database URL, either by exporting it:
```
export DATABASE_URL="postgres://postgres:password@localhost/todos"
```
2. Create the database.
or by making a `.env` file:
```
cp .env.example .env
```
3. Create the database.
```
$ sqlx db create
```
3. Run sql migrations
4. Run sql migrations
```
$ sqlx migrate run
@ -43,3 +51,11 @@ List all todos
```
cargo run
```
## Cleanup
To destroy the Postgres database, run:
```
docker-compose down --volumes
```

View file

@ -0,0 +1,17 @@
version: "3"
services:
database:
image: "postgres"
ports:
- "5432:5432"
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
- POSTGRES_DB=todos
volumes:
- database_data:/var/lib/postgresql/data
volumes:
database_data:
driver: local

View file

@ -1,4 +1,5 @@
use async_trait::async_trait;
use dotenv;
use sqlx::postgres::PgPool;
use sqlx::Done;
use std::{env, io::Write, sync::Arc};
@ -19,6 +20,7 @@ enum Command {
#[async_std::main]
#[paw::main]
async fn main(args: Args) -> anyhow::Result<()> {
dotenv::dotenv().ok();
let pool = PgPool::connect(&env::var("DATABASE_URL")?).await?;
let todo_repo = PostgresTodoRepo::new(pool);
let mut writer = std::io::stdout();