diff --git a/Cargo.toml b/Cargo.toml index 5bf8d84..dd2ab9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,6 @@ [workspace] members = ["libworterbuch", "worterbuch", "worterbuch-cli"] + +[patch.crates-io] +libworterbuch = { path = "./libworterbuch" } diff --git a/libworterbuch/Cargo.toml b/libworterbuch/Cargo.toml index 9ca70b5..f3e3c91 100644 --- a/libworterbuch/Cargo.toml +++ b/libworterbuch/Cargo.toml @@ -11,9 +11,7 @@ keywords = ["message", "broker", "data", "base", "pubsub"] categories = ["database"] [features] -default = ["client", "tcp"] -client = [] -server = [] +default = [] tcp = ["async", "tokio/net"] ws = ["async", "web"] graphql = ["async", "web", "futures-channel", "url"] @@ -22,7 +20,7 @@ web = ["futures-util", "tokio-tungstenite"] [dependencies] log = "0.4.17" -tokio = { version = "1.19.2" } +tokio = { version = "1.19.2", features = ["sync", "rt"] } serde = { version = "1.0.137", features = ["derive"] } serde_json = { version = "1.0.81" } diff --git a/libworterbuch/Makefile.toml b/libworterbuch/Makefile.toml index 0b6bc4f..f357d9d 100644 --- a/libworterbuch/Makefile.toml +++ b/libworterbuch/Makefile.toml @@ -1,35 +1,14 @@ -[tasks.test-server] +[tasks.test-all-async] command = "cargo" -args = ["test", "--no-default-features", "--features", "server,tcp,ws,graphql"] +args = ["test", "--no-default-features", "--features", "tcp,ws,graphql"] -[tasks.test-server-sync] +[tasks.test-sync] command = "cargo" -args = ["test", "--no-default-features", "--features", "server"] - -[tasks.test-client-tcp] -command = "cargo" -args = ["test", "--no-default-features", "--features", "client,tcp"] - -[tasks.test-client-ws] -command = "cargo" -args = ["test", "--no-default-features", "--features", "client,ws"] - -[tasks.test-client-graphql] -command = "cargo" -args = ["test", "--no-default-features", "--features", "client,graphql"] - -[tasks.test-client-sync] -command = "cargo" -args = ["test", "--no-default-features", "--features", "client"] +args = ["test", "--no-default-features"] [tasks.test-features] -dependencies = [ - "test-server", - "test-server-sync", - "test-client-tcp", - "test-client-ws", - "test-client-graphql", - "test-client-sync", -] +dependencies = ["test-all-async", "test-sync"] + +[tasks.docker] -[tasks.docker] \ No newline at end of file +[tasks.install] diff --git a/libworterbuch/src/client/config.rs b/libworterbuch/src/client/config.rs new file mode 100644 index 0000000..db1524d --- /dev/null +++ b/libworterbuch/src/client/config.rs @@ -0,0 +1,61 @@ +use crate::error::ConfigResult; +use std::env; + +#[derive(Debug, Clone, PartialEq)] +pub struct Config { + pub proto: String, + pub host_addr: String, + pub port: u16, +} + +impl Config { + pub fn load_env(&mut self) -> ConfigResult<()> { + if let Ok(val) = env::var("WORTERBUCH_PROTO") { + self.proto = val; + } + + if let Ok(val) = env::var("WORTERBUCH_HOST_ADDRESS") { + self.host_addr = val; + } + + if let Ok(val) = env::var("WORTERBUCH_PORT") { + self.port = val.parse()?; + } + + Ok(()) + } + + pub fn new() -> ConfigResult { + let mut config = Config::default(); + config.load_env()?; + Ok(config) + } +} + +impl Default for Config { + fn default() -> Self { + #[cfg(any(feature = "ws", feature = "graphql"))] + let _proto = "ws".to_owned(); + #[cfg(feature = "tcp")] + let _proto = "tcp".to_owned(); + #[cfg(not(any(feature = "tcp", feature = "ws", feature = "graphql")))] + let _proto = "".to_owned(); + + let host_addr = "localhost".to_owned(); + + #[cfg(feature = "graphql")] + let _port = 4243; + #[cfg(feature = "ws")] + let _port = 8080; + #[cfg(feature = "tcp")] + let _port = 4242; + #[cfg(not(any(feature = "tcp", feature = "ws", feature = "graphql")))] + let _port = 0; + + Config { + proto: _proto, + host_addr, + port: _port, + } + } +} diff --git a/libworterbuch/src/client/gql.rs b/libworterbuch/src/client/gql.rs index 9bcfbeb..fdebcc7 100644 --- a/libworterbuch/src/client/gql.rs +++ b/libworterbuch/src/client/gql.rs @@ -1,4 +1,4 @@ -use crate::{codec::ServerMessage as SM, error::ConnectionResult, client::Connection}; +use crate::{client::Connection, codec::ServerMessage as SM, error::ConnectionResult}; use futures_channel::mpsc::{self, UnboundedSender}; use futures_util::StreamExt; use serde_json::Value; diff --git a/libworterbuch/src/client/mod.rs b/libworterbuch/src/client/mod.rs index e0d7def..44ef362 100644 --- a/libworterbuch/src/client/mod.rs +++ b/libworterbuch/src/client/mod.rs @@ -1,3 +1,4 @@ +pub mod config; #[cfg(feature = "graphql")] pub mod gql; #[cfg(feature = "tcp")] @@ -5,10 +6,9 @@ pub mod tcp; #[cfg(feature = "ws")] pub mod ws; -use crate::error::ConnectionError; - use super::codec::ServerMessage; use super::error::ConnectionResult; +use crate::error::ConnectionError; use tokio::sync::broadcast; pub trait Connection { diff --git a/libworterbuch/src/error.rs b/libworterbuch/src/error.rs index d9eecd1..30fb866 100644 --- a/libworterbuch/src/error.rs +++ b/libworterbuch/src/error.rs @@ -2,7 +2,14 @@ use crate::codec::{ ErrorCode, KeyLength, MessageType, MetaData, MetaDataLength, NumKeyValuePairs, PathLength, RequestPattern, RequestPatternLength, ValueLength, }; +#[cfg(feature = "graphql")] +use futures_channel::mpsc::TrySendError; use std::{fmt, io, net::AddrParseError, num::ParseIntError, string::FromUtf8Error}; +use tokio::sync::mpsc::error::SendError; +#[cfg(feature = "web")] +use tokio_tungstenite::tungstenite; +#[cfg(feature = "graphql")] +use url::ParseError; #[derive(Debug)] pub enum DecodeError { @@ -146,6 +153,18 @@ impl fmt::Display for ConfigError { } } +impl From for ConfigError { + fn from(e: ParseIntError) -> Self { + ConfigError::InvalidPort(e) + } +} + +impl From for ConfigError { + fn from(e: AddrParseError) -> Self { + ConfigError::InvalidAddr(e) + } +} + pub type ConfigResult = std::result::Result; pub trait Context { @@ -205,100 +224,83 @@ impl Context> pub type WorterbuchResult = std::result::Result; -#[cfg(feature = "client")] -pub use client::*; -use tokio::sync::mpsc::error::SendError; -#[cfg(feature = "client")] -mod client { - use super::EncodeError; - use core::fmt; - #[cfg(feature = "graphql")] - use futures_channel::mpsc::TrySendError; - use std::{fmt::Debug, io}; - use tokio::sync::mpsc::error::SendError; +#[derive(Debug)] +pub enum ConnectionError { + IoError(io::Error), + EncodeError(EncodeError), + SendError(Box), #[cfg(feature = "web")] - use tokio_tungstenite::tungstenite; + WebsocketError(tungstenite::Error), + TrySendError(Box), #[cfg(feature = "graphql")] - use url::ParseError; - - #[derive(Debug)] - pub enum ConnectionError { - IoError(io::Error), - EncodeError(EncodeError), - SendError(Box), - #[cfg(feature = "web")] - WebsocketError(tungstenite::Error), - TrySendError(Box), - #[cfg(feature = "graphql")] - JsonError(serde_json::Error), - #[cfg(feature = "graphql")] - ParseError(ParseError), - } + JsonError(serde_json::Error), + #[cfg(feature = "graphql")] + ParseError(ParseError), +} - impl std::error::Error for ConnectionError {} - - impl fmt::Display for ConnectionError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::IoError(e) => fmt::Display::fmt(&e, f), - Self::EncodeError(e) => fmt::Display::fmt(&e, f), - Self::SendError(e) => fmt::Display::fmt(&e, f), - #[cfg(feature = "graphql")] - Self::JsonError(e) => fmt::Display::fmt(&e, f), - #[cfg(feature = "web")] - Self::WebsocketError(e) => fmt::Display::fmt(&e, f), - Self::TrySendError(e) => fmt::Display::fmt(&e, f), - #[cfg(feature = "graphql")] - Self::ParseError(e) => fmt::Display::fmt(&e, f), - } +impl std::error::Error for ConnectionError {} + +impl fmt::Display for ConnectionError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::IoError(e) => fmt::Display::fmt(&e, f), + Self::EncodeError(e) => fmt::Display::fmt(&e, f), + Self::SendError(e) => fmt::Display::fmt(&e, f), + #[cfg(feature = "graphql")] + Self::JsonError(e) => fmt::Display::fmt(&e, f), + #[cfg(feature = "web")] + Self::WebsocketError(e) => fmt::Display::fmt(&e, f), + Self::TrySendError(e) => fmt::Display::fmt(&e, f), + #[cfg(feature = "graphql")] + Self::ParseError(e) => fmt::Display::fmt(&e, f), } } +} - pub type ConnectionResult = std::result::Result; +pub type ConnectionResult = std::result::Result; - impl From for ConnectionError { - fn from(e: EncodeError) -> Self { - ConnectionError::EncodeError(e) - } +impl From for ConnectionError { + fn from(e: EncodeError) -> Self { + ConnectionError::EncodeError(e) } +} - impl From for ConnectionError { - fn from(e: io::Error) -> Self { - ConnectionError::IoError(e) - } +impl From for ConnectionError { + fn from(e: io::Error) -> Self { + ConnectionError::IoError(e) } +} - impl From> for ConnectionError { - fn from(e: SendError) -> Self { - ConnectionError::SendError(Box::new(e)) - } +impl From> for ConnectionError { + fn from(e: SendError) -> Self { + ConnectionError::SendError(Box::new(e)) } +} - #[cfg(feature = "web")] - impl From for ConnectionError { - fn from(e: tungstenite::Error) -> Self { - ConnectionError::WebsocketError(e) - } +#[cfg(feature = "web")] +impl From for ConnectionError { + fn from(e: tungstenite::Error) -> Self { + ConnectionError::WebsocketError(e) } +} - #[cfg(feature = "graphql")] - impl From for ConnectionError { - fn from(e: ParseError) -> Self { - ConnectionError::ParseError(e) - } +#[cfg(feature = "graphql")] +impl From for ConnectionError { + fn from(e: ParseError) -> Self { + ConnectionError::ParseError(e) } +} - #[cfg(feature = "graphql")] - impl From for ConnectionError { - fn from(e: serde_json::Error) -> Self { - ConnectionError::JsonError(e) - } +#[cfg(feature = "graphql")] +impl From for ConnectionError { + fn from(e: serde_json::Error) -> Self { + ConnectionError::JsonError(e) } +} - #[cfg(feature = "graphql")] - impl From> for ConnectionError { - fn from(e: TrySendError) -> Self { - ConnectionError::TrySendError(Box::new(e)) - } +#[cfg(feature = "graphql")] +impl From> for ConnectionError { + fn from(e: TrySendError) -> Self { + ConnectionError::TrySendError(Box::new(e)) } } diff --git a/libworterbuch/src/lib.rs b/libworterbuch/src/lib.rs index 7a09c1a..3b1e520 100644 --- a/libworterbuch/src/lib.rs +++ b/libworterbuch/src/lib.rs @@ -1,6 +1,3 @@ +pub mod client; pub mod codec; -pub mod config; pub mod error; - -#[cfg(feature = "client")] -pub mod client; diff --git a/worterbuch-cli/Cargo.toml b/worterbuch-cli/Cargo.toml index 5a93d1c..1da5ea8 100644 --- a/worterbuch-cli/Cargo.toml +++ b/worterbuch-cli/Cargo.toml @@ -17,7 +17,7 @@ ws = ["libworterbuch/ws"] graphql = ["libworterbuch/graphql"] [dependencies] -libworterbuch = { path = "../libworterbuch", features = ["client"] } +libworterbuch = { version = "0.1.1" } tokio = { version = "1.19.2", features = ["full"] } dotenv = "0.15.0" anyhow = "1.0.57" diff --git a/worterbuch-cli/src/bin/wbexp.rs b/worterbuch-cli/src/bin/wbexp.rs index 057494e..3375245 100644 --- a/worterbuch-cli/src/bin/wbexp.rs +++ b/worterbuch-cli/src/bin/wbexp.rs @@ -1,11 +1,11 @@ use anyhow::Result; use clap::Arg; #[cfg(feature = "graphql")] -use libworterbuch::client::gql::connect; +use libworterbuch::client::gql; #[cfg(feature = "tcp")] -use libworterbuch::client::tcp::connect; +use libworterbuch::client::tcp; #[cfg(feature = "ws")] -use libworterbuch::client::ws::connect; +use libworterbuch::client::ws; use libworterbuch::client::Connection; use worterbuch_cli::app; @@ -28,12 +28,15 @@ async fn main() -> Result<()> { let path = matches.get_one::("PATH").expect("path is required"); - let mut con = connect(&proto, &host_addr, port).await?; + #[cfg(feature = "tcp")] + let mut con = tcp::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "ws")] + let mut con = ws::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "graphql")] + let mut con = gql::connect(&proto, &host_addr, port).await?; let mut responses = con.responses(); - con.export(path)?; - responses.recv().await?; Ok(()) diff --git a/worterbuch-cli/src/bin/wbget.rs b/worterbuch-cli/src/bin/wbget.rs index d8189af..2d49f52 100644 --- a/worterbuch-cli/src/bin/wbget.rs +++ b/worterbuch-cli/src/bin/wbget.rs @@ -1,11 +1,11 @@ use anyhow::Result; use clap::Arg; #[cfg(feature = "graphql")] -use libworterbuch::client::gql::connect; +use libworterbuch::client::gql; #[cfg(feature = "tcp")] -use libworterbuch::client::tcp::connect; +use libworterbuch::client::tcp; #[cfg(feature = "ws")] -use libworterbuch::client::ws::connect; +use libworterbuch::client::ws; use libworterbuch::{client::Connection, codec::ServerMessage as SM}; use std::{ sync::{Arc, Mutex}, @@ -37,7 +37,12 @@ async fn main() -> Result<()> { let keys = matches.get_many::("KEYS"); - let mut con = connect(&proto, &host_addr, port).await?; + #[cfg(feature = "tcp")] + let mut con = tcp::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "ws")] + let mut con = ws::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "graphql")] + let mut con = gql::connect(&proto, &host_addr, port).await?; let mut trans_id = 0; let acked = Arc::new(Mutex::new(0)); diff --git a/worterbuch-cli/src/bin/wbimp.rs b/worterbuch-cli/src/bin/wbimp.rs index f2b2b3e..93aa845 100644 --- a/worterbuch-cli/src/bin/wbimp.rs +++ b/worterbuch-cli/src/bin/wbimp.rs @@ -1,11 +1,11 @@ use anyhow::Result; use clap::Arg; #[cfg(feature = "graphql")] -use libworterbuch::client::gql::connect; +use libworterbuch::client::gql; #[cfg(feature = "tcp")] -use libworterbuch::client::tcp::connect; +use libworterbuch::client::tcp; #[cfg(feature = "ws")] -use libworterbuch::client::ws::connect; +use libworterbuch::client::ws; use libworterbuch::client::Connection; use std::{ sync::{Arc, Mutex}, @@ -35,7 +35,12 @@ async fn main() -> Result<()> { .get_many::("PATHS") .expect("paths are required"); - let mut con = connect(&proto, &host_addr, port).await?; + #[cfg(feature = "tcp")] + let mut con = tcp::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "ws")] + let mut con = ws::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "graphql")] + let mut con = gql::connect(&proto, &host_addr, port).await?; let mut trans_id = 0; let acked = Arc::new(Mutex::new(0)); diff --git a/worterbuch-cli/src/bin/wbpget.rs b/worterbuch-cli/src/bin/wbpget.rs index 9bccf52..e39659a 100644 --- a/worterbuch-cli/src/bin/wbpget.rs +++ b/worterbuch-cli/src/bin/wbpget.rs @@ -1,11 +1,11 @@ use anyhow::Result; use clap::Arg; #[cfg(feature = "graphql")] -use libworterbuch::client::gql::connect; +use libworterbuch::client::gql; #[cfg(feature = "tcp")] -use libworterbuch::client::tcp::connect; +use libworterbuch::client::tcp; #[cfg(feature = "ws")] -use libworterbuch::client::ws::connect; +use libworterbuch::client::ws; use libworterbuch::{client::Connection, codec::ServerMessage as SM}; use std::{ sync::{Arc, Mutex}, @@ -37,7 +37,12 @@ async fn main() -> Result<()> { let patterns = matches.get_many::("PATTERNS"); - let mut con = connect(&proto, &host_addr, port).await?; + #[cfg(feature = "tcp")] + let mut con = tcp::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "ws")] + let mut con = ws::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "graphql")] + let mut con = gql::connect(&proto, &host_addr, port).await?; let mut trans_id = 0; let acked = Arc::new(Mutex::new(0)); diff --git a/worterbuch-cli/src/bin/wbpsub.rs b/worterbuch-cli/src/bin/wbpsub.rs index 3e87358..8f18e8d 100644 --- a/worterbuch-cli/src/bin/wbpsub.rs +++ b/worterbuch-cli/src/bin/wbpsub.rs @@ -1,11 +1,11 @@ use anyhow::Result; use clap::Arg; #[cfg(feature = "graphql")] -use libworterbuch::client::gql::connect; +use libworterbuch::client::gql; #[cfg(feature = "tcp")] -use libworterbuch::client::tcp::connect; +use libworterbuch::client::tcp; #[cfg(feature = "ws")] -use libworterbuch::client::ws::connect; +use libworterbuch::client::ws; use libworterbuch::{client::Connection, codec::ServerMessage as SM}; use std::time::Duration; use tokio::{ @@ -34,7 +34,13 @@ async fn main() -> Result<()> { let patterns = matches.get_many::("PATTERNS"); - let mut con = connect(&proto, &host_addr, port).await?; + #[cfg(feature = "tcp")] + let mut con = tcp::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "ws")] + let mut con = ws::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "graphql")] + let mut con = gql::connect(&proto, &host_addr, port).await?; + let mut responses = con.responses(); spawn(async move { diff --git a/worterbuch-cli/src/bin/wbsend.rs b/worterbuch-cli/src/bin/wbsend.rs index f919d3f..60d9ca4 100644 --- a/worterbuch-cli/src/bin/wbsend.rs +++ b/worterbuch-cli/src/bin/wbsend.rs @@ -1,11 +1,11 @@ use anyhow::Result; use clap::Arg; #[cfg(feature = "graphql")] -use libworterbuch::client::gql::connect; +use libworterbuch::client::gql; #[cfg(feature = "tcp")] -use libworterbuch::client::tcp::connect; +use libworterbuch::client::tcp; #[cfg(feature = "ws")] -use libworterbuch::client::ws::connect; +use libworterbuch::client::ws; use libworterbuch::client::Connection; use std::{ sync::{Arc, Mutex}, @@ -35,7 +35,12 @@ async fn main() -> Result<()> { let key = matches.get_one::("KEY").expect("key is required"); - let mut con = connect(&proto, &host_addr, port).await?; + #[cfg(feature = "tcp")] + let mut con = tcp::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "ws")] + let mut con = ws::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "graphql")] + let mut con = gql::connect(&proto, &host_addr, port).await?; let mut trans_id = 0; let acked = Arc::new(Mutex::new(0)); diff --git a/worterbuch-cli/src/bin/wbset.rs b/worterbuch-cli/src/bin/wbset.rs index 125ac6c..5840c55 100644 --- a/worterbuch-cli/src/bin/wbset.rs +++ b/worterbuch-cli/src/bin/wbset.rs @@ -1,11 +1,11 @@ use anyhow::Result; use clap::Arg; #[cfg(feature = "graphql")] -use libworterbuch::client::gql::connect; +use libworterbuch::client::gql; #[cfg(feature = "tcp")] -use libworterbuch::client::tcp::connect; +use libworterbuch::client::tcp; #[cfg(feature = "ws")] -use libworterbuch::client::ws::connect; +use libworterbuch::client::ws; use libworterbuch::{client::Connection, codec::KeyValuePair}; use std::{ sync::{Arc, Mutex}, @@ -46,7 +46,12 @@ async fn main() -> Result<()> { let key_value_pairs = matches.get_many::("KEY_VALUE_PAIRS"); let json = matches.is_present("JSON"); - let mut con = connect(&proto, &host_addr, port).await?; + #[cfg(feature = "tcp")] + let mut con = tcp::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "ws")] + let mut con = ws::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "graphql")] + let mut con = gql::connect(&proto, &host_addr, port).await?; let mut trans_id = 0; let acked = Arc::new(Mutex::new(0)); diff --git a/worterbuch-cli/src/bin/wbsub.rs b/worterbuch-cli/src/bin/wbsub.rs index 696988d..a4f9312 100644 --- a/worterbuch-cli/src/bin/wbsub.rs +++ b/worterbuch-cli/src/bin/wbsub.rs @@ -1,11 +1,11 @@ use anyhow::Result; use clap::Arg; #[cfg(feature = "graphql")] -use libworterbuch::client::gql::connect; +use libworterbuch::client::gql; #[cfg(feature = "tcp")] -use libworterbuch::client::tcp::connect; +use libworterbuch::client::tcp; #[cfg(feature = "ws")] -use libworterbuch::client::ws::connect; +use libworterbuch::client::ws; use libworterbuch::{client::Connection, codec::ServerMessage as SM}; use std::time::Duration; use tokio::{ @@ -34,7 +34,13 @@ async fn main() -> Result<()> { let keys = matches.get_many::("KEYS"); - let mut con = connect(&proto, &host_addr, port).await?; + #[cfg(feature = "tcp")] + let mut con = tcp::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "ws")] + let mut con = ws::connect(&proto, &host_addr, port).await?; + #[cfg(feature = "graphql")] + let mut con = gql::connect(&proto, &host_addr, port).await?; + let mut responses = con.responses(); spawn(async move { diff --git a/worterbuch-cli/src/lib.rs b/worterbuch-cli/src/lib.rs index aafaeda..4b446ae 100644 --- a/worterbuch-cli/src/lib.rs +++ b/worterbuch-cli/src/lib.rs @@ -2,7 +2,7 @@ use anyhow::Result; use clap::{App, Arg, ArgMatches}; use libworterbuch::{ codec::{Err, KeyValuePair, PState, State}, - config::Config, + client::config::Config, }; pub fn print_pstate(msg: &PState, json: bool) { @@ -56,6 +56,7 @@ pub fn app<'help>( include_json: bool, args: Vec>, ) -> Result<(ArgMatches, String, String, u16, bool)> { + let config = Config::new()?; let mut app = App::new(name) @@ -68,20 +69,9 @@ pub fn app<'help>( let matches = app.get_matches(); - #[cfg(feature = "web")] let default_proto = config.proto; - - #[cfg(not(feature = "web"))] - let default_proto = "".to_owned(); - let default_host_addr = config.host_addr; - - #[cfg(feature = "tcp")] - let default_port = config.tcp_port; - #[cfg(feature = "ws")] - let default_port = config.web_port; - #[cfg(feature = "graphql")] - let default_port = config.graphql_port; + let default_port = config.port; let proto = default_proto; let host_addr = matches diff --git a/worterbuch/Cargo.toml b/worterbuch/Cargo.toml index b2b2f2d..e3ff737 100644 --- a/worterbuch/Cargo.toml +++ b/worterbuch/Cargo.toml @@ -29,7 +29,7 @@ graphql = [ web = ["async", "warp", "openssl"] [dependencies] -libworterbuch = { path = "../libworterbuch", features = ["server"] } +libworterbuch = { version = "0.1.1" } tokio = { version = "1.19.2", features = ["full"] } log = "0.4.17" env_logger = "0.9.0" diff --git a/worterbuch/Makefile.toml b/worterbuch/Makefile.toml index 02310f3..df790ed 100644 --- a/worterbuch/Makefile.toml +++ b/worterbuch/Makefile.toml @@ -17,6 +17,8 @@ dependencies = ["test-multi-threaded", "test-single-threaded"] [tasks.install] command = "cargo" args = ["install", "--path", ".", "--features", "tcp,ws,graphql"] +dependencies = ["test-single-threaded"] + [tasks.docker-build] command = "docker" diff --git a/libworterbuch/src/config.rs b/worterbuch/src/config.rs similarity index 81% rename from libworterbuch/src/config.rs rename to worterbuch/src/config.rs index 8d1e3c7..c22b77e 100644 --- a/libworterbuch/src/config.rs +++ b/worterbuch/src/config.rs @@ -1,7 +1,8 @@ -use crate::error::{ConfigError, ConfigResult}; -use std::env; #[cfg(feature = "server")] use std::net::IpAddr; +use std::{env, net::IpAddr}; + +use libworterbuch::error::{ConfigError, ConfigResult}; #[derive(Debug, Clone, PartialEq)] pub struct Config { @@ -16,14 +17,11 @@ pub struct Config { pub web_port: u16, #[cfg(feature = "web")] pub proto: String, - #[cfg(feature = "server")] pub bind_addr: IpAddr, #[cfg(feature = "web")] pub cert_path: Option, #[cfg(feature = "web")] pub key_path: Option, - #[cfg(feature = "client")] - pub host_addr: String, } impl Config { @@ -47,30 +45,24 @@ impl Config { #[cfg(feature = "web")] if let Ok(val) = env::var("WORTERBUCH_WEB_PORT") { - self.web_port = val.parse().map_err(ConfigError::InvalidPort)?; + self.web_port = val.parse()?; } #[cfg(feature = "tcp")] if let Ok(val) = env::var("WORTERBUCH_TCP_PORT") { - self.tcp_port = val.parse().map_err(ConfigError::InvalidPort)?; + self.tcp_port = val.parse()?; } #[cfg(feature = "graphql")] if let Ok(val) = env::var("WORTERBUCH_GRAPHQL_PORT") { - self.graphql_port = val.parse().map_err(ConfigError::InvalidPort)?; + self.graphql_port = val.parse()?; } - #[cfg(feature = "server")] if let Ok(val) = env::var("WORTERBUCH_BIND_ADDRESS") { - let ip: IpAddr = val.parse().map_err(ConfigError::InvalidAddr)?; + let ip: IpAddr = val.parse()?; self.bind_addr = ip; } - #[cfg(feature = "client")] - if let Ok(val) = env::var("WORTERBUCH_HOST_ADDRESS") { - self.host_addr = val; - } - Ok(()) } @@ -95,14 +87,11 @@ impl Default for Config { web_port: 8080, #[cfg(feature = "web")] proto: "ws".to_owned(), - #[cfg(feature = "server")] bind_addr: [127, 0, 0, 1].into(), #[cfg(feature = "web")] cert_path: None, #[cfg(feature = "web")] key_path: None, - #[cfg(feature = "client")] - host_addr: "localhost".to_owned(), } } } diff --git a/worterbuch/src/main.rs b/worterbuch/src/main.rs index 73716d2..9bacc43 100644 --- a/worterbuch/src/main.rs +++ b/worterbuch/src/main.rs @@ -1,3 +1,4 @@ +mod config; #[cfg(not(feature = "docker"))] mod repl; mod server; @@ -7,10 +8,9 @@ mod worterbuch; #[cfg(not(feature = "docker"))] use crate::repl::repl; -use crate::worterbuch::Worterbuch; +use crate::{config::Config, worterbuch::Worterbuch}; use anyhow::Result; use clap::App; -use libworterbuch::config::Config; use std::sync::Arc; #[cfg(feature = "docker")] use tokio::signal::unix::{signal, SignalKind}; diff --git a/worterbuch/src/server/gql_warp.rs b/worterbuch/src/server/gql_warp.rs index 1b0b3ad..f437f9a 100644 --- a/worterbuch/src/server/gql_warp.rs +++ b/worterbuch/src/server/gql_warp.rs @@ -1,10 +1,10 @@ use crate::{ + config::Config, server::graphql::{self, Context}, worterbuch::Worterbuch, }; use juniper_graphql_ws::ConnectionConfig; use juniper_warp::subscriptions::serve_graphql_ws; -use libworterbuch::config::Config; use std::sync::Arc; use tokio::sync::RwLock; use warp::Filter; diff --git a/worterbuch/src/server/tcp.rs b/worterbuch/src/server/tcp.rs index 1c225e8..0c319a2 100644 --- a/worterbuch/src/server/tcp.rs +++ b/worterbuch/src/server/tcp.rs @@ -1,6 +1,6 @@ -use crate::worterbuch::Worterbuch; +use crate::{config::Config, worterbuch::Worterbuch}; use anyhow::Result; -use libworterbuch::{config::Config, error::WorterbuchResult}; +use libworterbuch::error::WorterbuchResult; use std::{net::SocketAddr, sync::Arc}; use tokio::{ io::AsyncWriteExt, diff --git a/worterbuch/src/server/ws.rs b/worterbuch/src/server/ws.rs index d02f0ae..82134af 100644 --- a/worterbuch/src/server/ws.rs +++ b/worterbuch/src/server/ws.rs @@ -1,8 +1,7 @@ use super::async_common::process_incoming_message; -use crate::worterbuch::Worterbuch; +use crate::{config::Config, worterbuch::Worterbuch}; use anyhow::Result; use futures::{sink::SinkExt, stream::StreamExt}; -use libworterbuch::config::Config; use std::{net::SocketAddr, sync::Arc}; use tokio::{ spawn, diff --git a/worterbuch/src/worterbuch.rs b/worterbuch/src/worterbuch.rs index 0b140f9..c09be32 100644 --- a/worterbuch/src/worterbuch.rs +++ b/worterbuch/src/worterbuch.rs @@ -1,10 +1,10 @@ use crate::{ + config::Config, store::{Store, StoreStats}, subscribers::{Subscriber, Subscribers}, }; use libworterbuch::{ codec::{KeyValuePair, KeyValuePairs, Path}, - config::Config, error::{Context, WorterbuchError, WorterbuchResult}, }; use serde::{Deserialize, Serialize};