From f14c4bc7fc74d39b31b049c10421609e6e048a69 Mon Sep 17 00:00:00 2001 From: necrashter Date: Sat, 23 Apr 2022 15:09:43 +0300 Subject: Support for multiple configs/routes --- src/main.rs | 25 +++++++++++++++++++++---- src/routes.rs | 11 ++++------- 2 files changed, 25 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 6edd67c..65b4430 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,20 +36,37 @@ use db::Db; use lazy_static::lazy_static; use std::fs; use crate::config::Config; +use warp::{Filter}; +use log::{error}; #[tokio::main] async fn main() { log4rs::init_file("log.conf.yml", log4rs::config::Deserializers::default()).unwrap(); - let config = match Config::read("config.yaml") { - Some(c) => c, + let configs = vec!["config.yaml"]; + + let combined_routes = configs.into_iter() + .filter_map(|filename| { + match Config::read(filename) { + Some(config) => Some(routes::network(Db::new(config))), + None => None, + } + }) + .reduce(|routes, route| routes.or(route).unify().boxed()); + + let routes = match combined_routes { + Some(r) => r, None => { - println!("Could not read config file, exiting."); + // Exit the program if there's no successfully loaded config file. + error!("Failed to load any config files!"); return; }, }; - let api = routes::application(Db::new(config)); + // gradecoin-site (zola) outputs a public/, we serve it here + let static_route = warp::any().and(warp::fs::dir("public")); + + let api = routes.or(static_route); // Start the server let point = ([127, 0, 0, 1], 8080); diff --git a/src/routes.rs b/src/routes.rs index cdbfc08..f53a20c 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -3,20 +3,17 @@ use crate::custom_filters; use crate::handlers; use crate::Db; -use warp::{Filter, Rejection, Reply}; - -/// Every route combined -pub fn application(db: Db) -> impl Filter + Clone { - // gradecoin-site (zola) outputs a public/, we serve it here - let static_route = warp::any().and(warp::fs::dir("public")); +use warp::{Filter, filters::BoxedFilter, Rejection, Reply}; +/// Every route combined for a single network +pub fn network(db: Db) -> BoxedFilter<(impl Reply,)> { transaction_list(db.clone()) .or(register_user(db.clone())) .or(auth_transaction_propose(db.clone())) .or(auth_block_propose(db.clone())) .or(list_users(db.clone())) .or(block_list(db)) - .or(static_route) + .boxed() } /// GET /user warp route -- cgit v1.2.3-70-g09d2