* Normalized cache data structures a bit

* Also changed up the naming scheme to be more clear
* Config -> ConfigFile
- Removed ConfigFile::server_url method
! The ConfigFile struct is now pretty much entirely used for disk operations
* update_jwt method also take in correct parameters
This commit is contained in:
shockrah 2021-04-04 14:13:37 -07:00
parent 3f1dfbf824
commit 1c85c88f96
2 changed files with 52 additions and 37 deletions

View File

@ -12,11 +12,13 @@
* *
*/ */
use crate::config::{ ServerMeta, UserConfig }; use crate::config::ServerMeta;
use crate::api_types::{Channel, Message}; use crate::api_types::{Channel, Message};
use crate::command::Command; use crate::command::Command;
use crate::config::ConfigFile;
use std::collections::HashMap; use std::collections::HashMap;
use hyper::Request;
struct ChannelCache { struct ChannelCache {
meta: Channel, meta: Channel,
@ -25,20 +27,32 @@ struct ChannelCache {
struct ServerCache { struct ServerCache {
meta: ServerMeta, meta: ServerMeta,
user: UserConfig,
channels: Vec<ChannelCache> channels: Vec<ChannelCache>
} }
pub struct Cache { pub struct Cache {
// Hostname -> Cache // url -> Cache
servers: HashMap<String, ServerCache>, servers: HashMap<String, ServerCache>,
active_server: Option<String> active_server: Option<ServerMeta>
} }
impl Default for Cache { impl ServerCache {
fn default() -> Cache { pub fn from(meta: ServerMeta, channels: Vec<ChannelCache>) -> Self {
ServerCache { meta, channels }
}
}
impl Cache {
pub fn from(config: &ConfigFile) -> Cache {
let mut servers = HashMap::new();
for cfg in &config.servers {
let i_server_cache = ServerCache::from(cfg.server.clone(), Vec::new());
let url = i_server_cache.meta.url.clone();
servers.insert(url, i_server_cache);
}
Cache { Cache {
servers: HashMap::new(), servers,
active_server: None active_server: None
} }
} }
@ -61,10 +75,26 @@ impl Cache {
} }
pub async fn send_message(&mut self, id: &str) -> Command { pub async fn send_message(&mut self, id: &str) -> Command {
if let Some(host) = self.active_server.as_ref() { use hyper::body::HttpBody as _;
use hyper::Client;
if let Some(server) = self.active_server.as_ref() {
let url = format!("{}/message/send", server.url);
let request = Request::builder()
.method("POST")
.uri(url).body(()).unwrap();
Command::Failure(format!("Not implemented yet!")) Command::Failure(format!("Not implemented yet!"))
} else { } else {
Command::Failure(format!("No active server+channel to send message to")) Command::Failure(format!("No active server+channel to send message to"))
} }
} }
pub fn list_hosts(&self) -> Command {
let mut hosts = String::from("Hosts: ");
for (host, _) in self.servers.iter() {
hosts.push_str(format!("{}, ", host).as_str());
}
Command::Text(hosts)
}
} }

View File

@ -2,63 +2,46 @@ use serde::{Serialize, Deserialize};
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerMeta { pub struct ServerMeta {
pub protocol: String, pub url: String, // Should never end with a '/'
pub hostname: String,
pub port: Option<u16>,
pub description: String, pub description: String,
pub name: String, pub name: String,
} }
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerConfig {
pub user: UserConfig,
pub server: ServerMeta
}
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct UserConfig { pub struct UserMeta {
pub id: u64, pub id: u64,
pub secret: String, pub secret: String,
pub jwt: Option<String>, pub jwt: Option<String>,
pub permissions: u64, pub permissions: u64,
pub joindate: i64,
pub status: i32, pub status: i32,
} }
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerConfig {
pub user: UserMeta,
pub server: ServerMeta
}
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Config { pub struct ConfigFile {
// Global option
pub username: Option<String>, pub username: Option<String>,
pub servers: Vec<ServerConfig> pub servers: Vec<ServerConfig>
} }
impl Config { impl ConfigFile {
pub fn update_jwt(&mut self, hostname: String, jwt: String) { pub fn update_jwt(&mut self, url: String, jwt: String) {
for servermeta in self.servers.iter_mut() { for servermeta in self.servers.iter_mut() {
if servermeta.server.hostname == hostname { if servermeta.server.url == url {
servermeta.user.jwt = Some(jwt); servermeta.user.jwt = Some(jwt);
break break
} }
} }
} }
pub fn server_url(&self, hostname: &str) -> Option<String> {
// Finds the base-url for the given hostname, assuming it exists
let mut url: Option<String> = None;
for meta in self.servers.iter() {
if meta.server.hostname == hostname {
url = match meta.server.port {
Some(p) => Some(format!("{}://{}:{}", meta.server.protocol, hostname, p)),
None => Some(format!("{}://{}", meta.server.protocol, hostname))
};
break
}
}
return url;
}
pub fn save(&self, path: String) -> std::io::Result<()>{ pub fn save(&self, path: String) -> std::io::Result<()>{
use std::io::prelude::Write; use std::io::prelude::Write;
use std::fs::File; use std::fs::File;
@ -70,3 +53,5 @@ impl Config {
Ok(()) Ok(())
} }
} }