Skip to content

Instantly share code, notes, and snippets.

@leonbreedt
Created April 25, 2019 02:19
Show Gist options
  • Save leonbreedt/f81aed68fae0eaf81dd32ed516a3958d to your computer and use it in GitHub Desktop.
Save leonbreedt/f81aed68fae0eaf81dd32ed516a3958d to your computer and use it in GitHub Desktop.
main.rs - no extracted function
mod api;
mod db;
mod models;
use pretty_env_logger;
use std;
use std::net::SocketAddr;
use warp::{self, Filter};
fn main() {
pretty_env_logger::init();
let listen_port = std::env::var("PORT").unwrap_or("8080".to_owned());
let listen_addr: SocketAddr = format!("127.0.0.1:{}", listen_port)
.parse()
.expect("invalid listen address");
let database = db::connect().expect("failed to connect to database");
let database = warp::any().map(move || database.clone());
//let all_routes = api::customers(&database);
let customers = warp::path("customers");
let customers_index = customers.and(warp::path::end());
let customer_by_id = customers
.and(warp::path::param::<i64>())
.and(warp::path::end());
let customer_list = warp::get2()
.and(customers_index)
.and(database.clone())
.and_then(api::list_customers);
let customer_read = warp::get2()
.and(customer_by_id)
.and(database.clone())
.and_then(api::read_customer);
let api = customer_list.or(customer_read).recover(api::handle_errors);
let routes = api.with(warp::log("repo"));
warp::serve(routes).run(listen_addr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment