Commit 0c04e67c authored by Aljaž Srebrnič's avatar Aljaž Srebrnič

Move modifiers to separate file

parent d435c73f
Pipeline #554 passed with stages
in 14 minutes and 54 seconds
......@@ -9,75 +9,10 @@ extern crate r2d2_redis;
use spaceapi_server::SpaceapiServerBuilder;
use spaceapi_server::api;
use spaceapi_server::api::Keymaster;
use spaceapi_server::modifiers::StatusModifier;
use r2d2;
use redis::{Commands, RedisResult};
type RedisPool = r2d2::Pool<r2d2_redis::RedisConnectionManager>;
struct OpenStatusFromRedisModifier {
pool: RedisPool
}
impl OpenStatusFromRedisModifier {
fn new(pool: RedisPool) -> OpenStatusFromRedisModifier {
OpenStatusFromRedisModifier{pool}
}
}
impl StatusModifier for OpenStatusFromRedisModifier {
fn modify(&self, status: &mut api::Status) {
let conn = match self.pool.get() {
Err(e) => {
error!("Error getting redis pool: {:#?}", e);
return;
}
Ok(c) => c
};
let state : RedisResult<String> = conn.get("state_open");
status.state.open = match state {
Ok(v) => Some(v == "open"),
Err(_) => None
};
status.state.lastchange = conn.get("state_lastchange").ok();
status.state.trigger_person = conn.get("state_triggerperson").ok();
}
}
struct KeymastersFromRedisModifier {
pool: RedisPool
}
impl KeymastersFromRedisModifier {
fn new(pool: RedisPool) -> KeymastersFromRedisModifier {
KeymastersFromRedisModifier{pool}
}
}
impl StatusModifier for KeymastersFromRedisModifier {
fn modify(&self, status: &mut api::Status) {
let conn = match self.pool.get() {
Err(e) => {
error!("Error getting redis pool: {:#?}", e);
return;
}
Ok(c) => c
};
let km_redis : Vec<String> = conn.zrange("keys", 0, -1).unwrap();
let mut km : Vec<Keymaster> = Vec::new();
for k in km_redis {
let mut m = Keymaster::default();
m.irc_nick = Some(k);
km.push(m)
}
status.contact.keymasters = Some(km);
}
}
mod modifiers;
fn main() {
env_logger::init();
......@@ -110,7 +45,7 @@ fn main() {
let redis_connstring = var("REDIS_CONNSTRING").unwrap_or_else(|_| String::from("redis://localhost"));
let manager = r2d2_redis::RedisConnectionManager::new(&*redis_connstring).unwrap();
let pool = RedisPool::builder()
let pool = r2d2::Pool::builder()
.connection_timeout(Duration::from_secs(1))
.build(manager)
.unwrap();
......@@ -119,8 +54,8 @@ fn main() {
let server = SpaceapiServerBuilder::new(status)
// .add_redis_pool(pool) waiting for
.redis_connection_info(&*redis_connstring)
.add_status_modifier(OpenStatusFromRedisModifier::new(pool.clone()))
.add_status_modifier(KeymastersFromRedisModifier::new(pool.clone()))
.add_status_modifier(modifiers::OpenStatusFromRedisModifier::new(pool.clone()))
.add_status_modifier(modifiers::KeymastersFromRedisModifier::new(pool.clone()))
.build()
.expect("Could not initialize server");
......
use spaceapi_server::api;
use spaceapi_server::modifiers::StatusModifier;
use redis::{Commands, RedisResult};
type RedisPool = r2d2::Pool<r2d2_redis::RedisConnectionManager>;
pub struct OpenStatusFromRedisModifier {
pool: RedisPool
}
impl OpenStatusFromRedisModifier {
pub fn new(pool: RedisPool) -> OpenStatusFromRedisModifier {
OpenStatusFromRedisModifier{pool}
}
}
impl StatusModifier for OpenStatusFromRedisModifier {
fn modify(&self, status: &mut api::Status) {
let conn = match self.pool.get() {
Err(e) => {
error!("Error getting redis pool: {:#?}", e);
return;
}
Ok(c) => c
};
let state : RedisResult<String> = conn.get("state_open");
status.state.open = match state {
Ok(v) => Some(v == "open"),
Err(_) => None
};
status.state.lastchange = conn.get("state_lastchange").ok();
status.state.trigger_person = conn.get("state_triggerperson").ok();
}
}
pub struct KeymastersFromRedisModifier {
pool: RedisPool
}
impl KeymastersFromRedisModifier {
pub fn new(pool: RedisPool) -> KeymastersFromRedisModifier {
KeymastersFromRedisModifier{pool}
}
}
impl StatusModifier for KeymastersFromRedisModifier {
fn modify(&self, status: &mut api::Status) {
let conn = match self.pool.get() {
Err(e) => {
error!("Error getting redis pool: {:#?}", e);
return;
}
Ok(c) => c
};
let km_redis : Vec<String> = conn.zrange("keys", 0, -1).unwrap();
let mut km : Vec<api::Keymaster> = Vec::new();
for k in km_redis {
let mut m = api::Keymaster::default();
m.irc_nick = Some(k);
km.push(m)
}
status.contact.keymasters = Some(km);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment