Skip to content

Commit

Permalink
feat: add config support
Browse files Browse the repository at this point in the history
  • Loading branch information
stepankuzmin committed Jul 25, 2018
1 parent 5532520 commit c55e61d
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/target/
**/*.rs.bk

db
config.yaml
docker-compose.yml
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ r2d2_postgres = "0.14"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
serde_yaml = "0.7"
tilejson = "0.1"
85 changes: 85 additions & 0 deletions src/config.rs
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())
}
2 changes: 1 addition & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use actix::prelude::*;
use r2d2_postgres::{PostgresConnectionManager, TlsMode};
use r2d2::{Pool, PooledConnection};
use r2d2_postgres::{PostgresConnectionManager, TlsMode};
use std::error::Error;
use std::io;

Expand Down
55 changes: 15 additions & 40 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extern crate actix;
extern crate actix_web;
extern crate env_logger;
extern crate futures;
extern crate postgres;
#[macro_use]
extern crate log;
extern crate mapbox_expressions_to_sql;
Expand All @@ -12,42 +13,31 @@ extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate serde_yaml;
extern crate tilejson;

use actix::{Actor, Addr, SyncArbiter};
use actix_web::server;
use std::env;
use std::error::Error;
use std::io;

mod config;
mod coordinator_actor;
mod db;
mod martin;
mod messages;
mod server;
mod source;
mod utils;
mod worker_actor;

static CONFIG_FILENAME: &str = "config.yaml";

fn main() {
env_logger::init();

let pool_size = 20; // TODO: get pool_size from config
let conn_string: String = env::var("DATABASE_URL").expect("DATABASE_URL must be set");

let pool_size = env::var("DATABASE_POOL_SIZE")
.ok()
.and_then(|pool_size| pool_size.parse::<u32>().ok())
.unwrap_or(20);

let worker_processes = env::var("WORKER_PROCESSES")
.ok()
.and_then(|worker_processes| worker_processes.parse::<usize>().ok())
.unwrap_or(num_cpus::get());

let keep_alive = env::var("KEEP_ALIVE")
.ok()
.and_then(|keep_alive| keep_alive.parse::<usize>().ok())
.unwrap_or(75);

info!("Connecting to {}", conn_string);
let pool = match db::setup_connection_pool(&conn_string, pool_size) {
Ok(pool) => {
Expand All @@ -60,36 +50,21 @@ fn main() {
}
};

let sources = match pool.get()
let config = match pool.get()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.description()))
.and_then(|conn| source::get_sources(conn))
.and_then(|conn| config::build(CONFIG_FILENAME, conn))
{
Ok(sources) => sources,
Ok(config) => config,
Err(error) => {
error!("Can't load sources: {}", error);
error!("Can't build config: {}", error);
std::process::exit(-1);
}
};

let server = actix::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 port = 3000;
let bind_addr = format!("0.0.0.0:{}", port);
let _addr = server::new(move || {
martin::new(
db_sync_arbiter.clone(),
coordinator_addr.clone(),
sources.clone(),
)
}).bind(bind_addr.clone())
.expect(&format!("Can't bind to {}", bind_addr))
.keep_alive(keep_alive)
.shutdown_timeout(0)
.workers(worker_processes)
.start();
let listen_addresses = config.listen_addresses.clone();

let server = server::new(config, pool);
let _ = server.run();
info!("Server has been started on {}.", bind_addr);

info!("Server has been started on {}.", listen_addresses);
}
2 changes: 1 addition & 1 deletion src/scripts/get_tile.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
WITH bounds AS (SELECT {mercator_bounds} as mercator, {original_bounds} as original)
SELECT ST_AsMVT(tile, '{id}', {extent}, 'geom') FROM (
SELECT ST_AsMVT(tile, '{id}', {extent}, 'geom' {id_column}) FROM (
SELECT
ST_AsMVTGeom({geometry_column_mercator}, bounds.mercator, {extent}, {buffer}, {clip_geom}) AS geom {properties}
FROM {id}, bounds
Expand Down
32 changes: 32 additions & 0 deletions src/server.rs
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
}
Loading

0 comments on commit c55e61d

Please sign in to comment.