Fully working docker setup, switch to sqlx migrate

This commit is contained in:
Tyler Hallada 2020-11-11 17:21:50 -05:00
parent e44aa9ddfc
commit 9410cbf6a5
6 changed files with 46 additions and 26 deletions

1
.gitignore vendored
View File

@ -3,5 +3,4 @@
.env
.env.docker
Session.vim
src/db/refinery.toml
tags

View File

@ -36,7 +36,32 @@ Related projects:
- [`BazaarRealmMod`](https://github.com/thallada/BazaarRealmMod): Papyrus
scripts, ESP plugin, and all other resources for the mod
## Development Setup
## Docker Setup
The easiest way to get the server up and running is using Docker.
1. Download and install [Docker Desktop](https://www.docker.com/get-started)
2. Git clone this repo into a folder of your choosing: `git clone https://github.com/thallada/BazaarRealmAPI.git`
3. Create a new file `.env.docker` in the checked out `bazaar_realm_api`
folder with the contents (replacing `<password>` with a secure generated
password):
```
DATABASE_URL="postgresql://bazaarrealm:<password>@db/bazaarrealm"
RUST_LOG="bazaar_realm_api=debug,warp=info"
HOST="http://localhost:3030"
POSTGRES_DB=bazaarrealm
POSTGRES_USER=bazaarrealm
POSTGRES_PASSWORD=<password>
```
3. In the checked out repo, run: `docker-compose build`
4. Once that completes, run: `docker-compose up`
## Manual Development Setup
If you would prefer to run the server outside Docker on your host machine, do
the following steps to get everything setup.
1. Install and run postgres.
2. Create postgres user and database (and add uuid extension while you're there
@ -69,21 +94,13 @@ RUST_LOG="bazaar_realm_api=debug"
HOST="http://localhost:3030"
```
4. Create a new file at `src/db/refinery.toml` with the contents:
```
[main]
db_type = "Postgres"
db_host = "localhost"
db_port = "5432"
db_user = "bazaarrealm"
db_pass = "<database-password-here>"
db_name = "bazaarrealm"
```
5. Install `refinery_cli` with `cargo install refinery_cli` and run `refinery migrate -c ./src/db/refinery.toml files -p ./src/db/migrations/` which will
run the database migrations.
6. Run `./devserver.sh` to run the dev server (by default it listens at
4. Install
[`sqlx_cli`](https://github.com/launchbadge/sqlx/tree/master/sqlx-cli) with
`cargo install --version=0.1.0-beta.1 sqlx-cli --no-default-features --features postgres`
5. `cd db` to enter the `db` sub-directory of this repo.
6. Run `sqlx migrate run` which will run all the database migrations.
7. `cd ..` to return to the top-level directory of this repo.
8. Run `./devserver.sh` to run the dev server (by default it listens at
`127.0.0.1:3030`).
## Testing Data

View File

@ -1,4 +1,4 @@
version: "2.0"
version: "3.8"
services:
app:
@ -10,6 +10,12 @@ services:
- .env.docker
ports:
- "3030:3030"
depends_on:
- db
db:
image: postgres:alpine
env_file:
- .env.docker
volumes:
cargo: {}

View File

@ -8,7 +8,7 @@ use hyper::server::Server;
use listenfd::ListenFd;
use serde::{de::DeserializeOwned, Serialize};
use sqlx::postgres::PgPoolOptions;
use sqlx::{Pool, Postgres};
use sqlx::{migrate, Pool, Postgres};
use std::convert::Infallible;
use std::env;
use tracing_subscriber::fmt::format::FmtSpan;
@ -68,15 +68,11 @@ async fn main() -> Result<()> {
dotenv().ok();
let env_log_filter =
env::var("RUST_LOG").unwrap_or_else(|_| "warp=info,bazaar_realm_api=info".to_owned());
let file_appender = tracing_appender::rolling::hourly(
env::var("LOG_DIR").unwrap_or_else(|_| ".".to_owned()),
"bazaarrealm.log",
);
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
tracing_subscriber::fmt()
.with_env_filter(env_log_filter)
.with_span_events(FmtSpan::CLOSE)
.with_writer(non_blocking)
.with_writer(std::io::stdout)
.init();
let host = env::var("HOST").expect("`HOST` environment variable not defined");
@ -84,6 +80,8 @@ async fn main() -> Result<()> {
let api_url = host_url.join("/v1/")?;
let env = Environment::new(api_url).await?;
migrate!("db/migrations").run(&env.db).await?;
let status_handler = warp::path::path("status")
.and(warp::path::end())
.and(warp::get())
@ -407,7 +405,7 @@ async fn main() -> Result<()> {
let server = if let Some(l) = listenfd.take_tcp_listener(0)? {
Server::from_tcp(l)?
} else {
Server::bind(&([127, 0, 0, 1], 3030).into())
Server::bind(&([0, 0, 0, 0], 3030).into())
};
// warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;