diff options
author | necrashter | 2022-04-23 15:09:43 +0300 |
---|---|---|
committer | Yigit Sever | 2022-04-23 18:10:12 +0300 |
commit | f14c4bc7fc74d39b31b049c10421609e6e048a69 (patch) | |
tree | 89e69fd693042701c732e812ec7a688f8ff779f3 | |
parent | e21f7b96747d851d3e28282d18f316a48a6be51d (diff) | |
download | gradecoin-f14c4bc7fc74d39b31b049c10421609e6e048a69.tar.gz gradecoin-f14c4bc7fc74d39b31b049c10421609e6e048a69.tar.bz2 gradecoin-f14c4bc7fc74d39b31b049c10421609e6e048a69.zip |
Support for multiple configs/routes
-rw-r--r-- | src/main.rs | 25 | ||||
-rw-r--r-- | src/routes.rs | 11 |
2 files changed, 25 insertions, 11 deletions
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; | |||
36 | use lazy_static::lazy_static; | 36 | use lazy_static::lazy_static; |
37 | use std::fs; | 37 | use std::fs; |
38 | use crate::config::Config; | 38 | use crate::config::Config; |
39 | use warp::{Filter}; | ||
40 | use log::{error}; | ||
39 | 41 | ||
40 | #[tokio::main] | 42 | #[tokio::main] |
41 | async fn main() { | 43 | async fn main() { |
42 | log4rs::init_file("log.conf.yml", log4rs::config::Deserializers::default()).unwrap(); | 44 | log4rs::init_file("log.conf.yml", log4rs::config::Deserializers::default()).unwrap(); |
43 | 45 | ||
44 | let config = match Config::read("config.yaml") { | 46 | let configs = vec!["config.yaml"]; |
45 | Some(c) => c, | 47 | |
48 | let combined_routes = configs.into_iter() | ||
49 | .filter_map(|filename| { | ||
50 | match Config::read(filename) { | ||
51 | Some(config) => Some(routes::network(Db::new(config))), | ||
52 | None => None, | ||
53 | } | ||
54 | }) | ||
55 | .reduce(|routes, route| routes.or(route).unify().boxed()); | ||
56 | |||
57 | let routes = match combined_routes { | ||
58 | Some(r) => r, | ||
46 | None => { | 59 | None => { |
47 | println!("Could not read config file, exiting."); | 60 | // Exit the program if there's no successfully loaded config file. |
61 | error!("Failed to load any config files!"); | ||
48 | return; | 62 | return; |
49 | }, | 63 | }, |
50 | }; | 64 | }; |
51 | 65 | ||
52 | let api = routes::application(Db::new(config)); | 66 | // gradecoin-site (zola) outputs a public/, we serve it here |
67 | let static_route = warp::any().and(warp::fs::dir("public")); | ||
68 | |||
69 | let api = routes.or(static_route); | ||
53 | 70 | ||
54 | // Start the server | 71 | // Start the server |
55 | let point = ([127, 0, 0, 1], 8080); | 72 | 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 @@ | |||
3 | use crate::custom_filters; | 3 | use crate::custom_filters; |
4 | use crate::handlers; | 4 | use crate::handlers; |
5 | use crate::Db; | 5 | use crate::Db; |
6 | use warp::{Filter, Rejection, Reply}; | 6 | use warp::{Filter, filters::BoxedFilter, Rejection, Reply}; |
7 | |||
8 | /// Every route combined | ||
9 | pub fn application(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { | ||
10 | // gradecoin-site (zola) outputs a public/, we serve it here | ||
11 | let static_route = warp::any().and(warp::fs::dir("public")); | ||
12 | 7 | ||
8 | /// Every route combined for a single network | ||
9 | pub fn network(db: Db) -> BoxedFilter<(impl Reply,)> { | ||
13 | transaction_list(db.clone()) | 10 | transaction_list(db.clone()) |
14 | .or(register_user(db.clone())) | 11 | .or(register_user(db.clone())) |
15 | .or(auth_transaction_propose(db.clone())) | 12 | .or(auth_transaction_propose(db.clone())) |
16 | .or(auth_block_propose(db.clone())) | 13 | .or(auth_block_propose(db.clone())) |
17 | .or(list_users(db.clone())) | 14 | .or(list_users(db.clone())) |
18 | .or(block_list(db)) | 15 | .or(block_list(db)) |
19 | .or(static_route) | 16 | .boxed() |
20 | } | 17 | } |
21 | 18 | ||
22 | /// GET /user warp route | 19 | /// GET /user warp route |