-
-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5532520
commit c55e61d
Showing
9 changed files
with
199 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
/target/ | ||
**/*.rs.bk | ||
|
||
db | ||
config.yaml | ||
docker-compose.yml |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use num_cpus; | ||
use serde_yaml; | ||
use std::error::Error; | ||
use std::fs::File; | ||
use std::io; | ||
use std::io::prelude::*; | ||
use std::path::Path; | ||
|
||
use super::db::PostgresConnection; | ||
use super::source::{get_sources, Sources}; | ||
|
||
#[derive(Clone, Debug, Serialize)] | ||
pub struct Config { | ||
pub pool_size: u32, | ||
pub keep_alive: usize, | ||
pub worker_processes: usize, | ||
pub listen_addresses: String, | ||
pub sources: Sources, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct ConfigBuilder { | ||
pub pool_size: Option<u32>, | ||
pub keep_alive: Option<usize>, | ||
pub worker_processes: Option<usize>, | ||
pub listen_addresses: Option<String>, | ||
pub sources: Sources, | ||
} | ||
|
||
impl ConfigBuilder { | ||
pub fn finalize(self) -> Config { | ||
Config { | ||
pool_size: self.pool_size.unwrap_or(20), | ||
keep_alive: self.keep_alive.unwrap_or(75), | ||
worker_processes: self.worker_processes.unwrap_or(num_cpus::get()), | ||
listen_addresses: self.listen_addresses.unwrap_or("0.0.0.0:3000".to_string()), | ||
sources: self.sources, | ||
} | ||
} | ||
} | ||
|
||
pub fn build(config_filename: &str, conn: PostgresConnection) -> io::Result<Config> { | ||
if Path::new(config_filename).exists() { | ||
info!("Config found at {}", config_filename); | ||
let config = read_config(config_filename)?; | ||
return Ok(config); | ||
}; | ||
|
||
let sources = get_sources(conn)?; | ||
let config = generate_config(sources); | ||
|
||
// let _ = write_config(config_filename, config.clone()); | ||
|
||
Ok(config) | ||
} | ||
|
||
pub fn generate_config(sources: Sources) -> Config { | ||
let config = ConfigBuilder { | ||
pool_size: None, | ||
keep_alive: None, | ||
worker_processes: None, | ||
listen_addresses: None, | ||
sources: sources, | ||
}; | ||
|
||
config.finalize() | ||
} | ||
|
||
// pub fn write_config(file_name: &str, config: Config) -> io::Result<()> { | ||
// let mut file = File::create(file_name)?; | ||
// let yaml = serde_yaml::to_string(&config).unwrap(); | ||
// file.write_all(yaml.as_bytes())?; | ||
// Ok(()) | ||
// } | ||
|
||
pub fn read_config(file_name: &str) -> io::Result<Config> { | ||
let mut file = File::open(file_name)?; | ||
let mut contents = String::new(); | ||
file.read_to_string(&mut contents)?; | ||
|
||
let config: ConfigBuilder = serde_yaml::from_str(contents.as_str()) | ||
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.description()))?; | ||
|
||
Ok(config.finalize()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use actix::{Actor, Addr, SyncArbiter, System, SystemRunner}; | ||
use actix_web::server; | ||
|
||
use super::config::Config; | ||
use super::coordinator_actor; | ||
use super::db; | ||
use super::martin; | ||
|
||
pub fn new(config: Config, pool: db::PostgresPool) -> SystemRunner { | ||
let server = System::new("server"); | ||
let coordinator_addr: Addr<_> = coordinator_actor::CoordinatorActor::default().start(); | ||
let db_sync_arbiter = SyncArbiter::start(3, move || db::DbExecutor(pool.clone())); | ||
|
||
let keep_alive = config.keep_alive; | ||
let worker_processes = config.worker_processes; | ||
let listen_addresses = config.listen_addresses.clone(); | ||
|
||
let _addr = server::new(move || { | ||
martin::new( | ||
db_sync_arbiter.clone(), | ||
coordinator_addr.clone(), | ||
config.sources.clone(), | ||
) | ||
}).bind(listen_addresses.clone()) | ||
.expect(&format!("Can't bind to {}", listen_addresses)) | ||
.keep_alive(keep_alive) | ||
.shutdown_timeout(0) | ||
.workers(worker_processes) | ||
.start(); | ||
|
||
server | ||
} |
Oops, something went wrong.