75 lines
2.3 KiB
Rust
75 lines
2.3 KiB
Rust
mod db;
|
|
mod error;
|
|
mod handler;
|
|
|
|
type Result<T> = std::result::Result<T, Rejection>;
|
|
type DBCon = Connection<PgConnectionManager<NoTls>>;
|
|
type DBPool = Pool<PgConnectionManager<NoTls>>;
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
let db_pool = db::create_pool().expect("database pool can be created");
|
|
|
|
db::init_db(&db_pool)
|
|
.await
|
|
.expect("database can be initialized");
|
|
|
|
let pet = warp::path!("owner" / i32 / "pet");
|
|
let pet_param = warp::path!("owner" / i32 / "pet" / i32);
|
|
let owner = warp::path("owner");
|
|
|
|
let pet_routes = pet
|
|
.and(warp::get())
|
|
.and(with_db(db_pool.clone()))
|
|
.and_then(handler::list_pets_handler)
|
|
.or(pet
|
|
.and(warp::post())
|
|
.and(warp::body::json())
|
|
.and(with_db(db_pool.clone()))
|
|
.and_then(handler::create_pet_handler))
|
|
.or(pet_param
|
|
.and(warp::delete())
|
|
.and(with_db(db_pool.clone()))
|
|
.and_then(handler::delete_pet_handler));
|
|
|
|
let owner_routes = owner
|
|
.and(warp::get())
|
|
.and(warp::path::param())
|
|
.and(with_db(db_pool.clone()))
|
|
.and_then(handler::fetch_owner_handler)
|
|
.or(owner
|
|
.and(warp::get())
|
|
.and(with_db(db_pool.clone()))
|
|
.and_then(handler::list_owners_handler))
|
|
.or(owner
|
|
.and(warp::post())
|
|
.and(warp::body::json())
|
|
.and(with_db(db_pool.clone()))
|
|
.and_then(handler::create_owner_handler));
|
|
|
|
let routes = pet_routes
|
|
.or(owner_routes)
|
|
.recover(error::handle_rejection)
|
|
.with(
|
|
warp::cors()
|
|
.allow_credentials(true)
|
|
.allow_methods(&[
|
|
Method::OPTIONS,
|
|
Method::GET,
|
|
Method::POST,
|
|
Method::DELETE,
|
|
Method::PUT,
|
|
])
|
|
.allow_headers(vec![header::CONTENT_TYPE, header::ACCEPT])
|
|
.expose_headers(vec![header::LINK])
|
|
.max_age(300)
|
|
.allow_any_origin(),
|
|
);
|
|
|
|
warp::serve(routes).run(([127, 0, 0, 1], 8000)).await;
|
|
}
|
|
|
|
fn with_db(db_pool: DBPool) -> impl Filter<Extract = (DBPool,), Error = Infallible> + Clone {
|
|
warp::any().map(move || db_pool.clone())
|
|
}
|