release v0.3.0

This commit is contained in:
MedzikUser 2022-08-04 16:35:33 +02:00
parent 093671ff8f
commit 45a9fe605d
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
10 changed files with 48 additions and 72 deletions

View File

@ -1,16 +1,25 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
<!-- next-header -->
## [Unreleased]
## Chore
## [0.3.0] - 2022-08-04
## **Breaking**
- **tcp**: use tcp from tokio instead of std
## Features
- **client**: added `peer_addr` function
- **server**: added `/help` command
- **api**: re-export `async_trait` so that it doesn't have to be added to dependencies in plugins
## Changed
- **server**: the `/help` command has been accelerated
- **cli**: moved to the `cli.rs` file
- **logger**: changed `log` to `tracing`
- **dependencies**: updated
- **cli**: deleted option `--disable-websocket` and added `--enable-websocket`
## [0.2.0] - 2022-06-26
### Features
- **plugins**: add `Result<()>` in `fn execute()` (Event and Command traits)
@ -32,6 +41,7 @@ You set custom host and port `./servers --host 0.0.0.0 --port 9999`
Show cli help `./servers --help`
<!-- next-url -->
[Unreleased]: https://github.com/MedzikUser/servers/compare/v0.2.0...HEAD
[Unreleased]: https://github.com/MedzikUser/servers/compare/v0.3.0...HEAD
[0.3.0]: https://github.com/MedzikUser/servers/commits/v0.3.0
[0.2.0]: https://github.com/MedzikUser/servers/commits/v0.2.0
[0.1.0]: https://github.com/MedzikUser/servers/commits/v0.1.0

2
Cargo.lock generated
View File

@ -563,7 +563,7 @@ checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]]
name = "servers"
version = "0.3.0-pre"
version = "0.3.0"
dependencies = [
"anyhow",
"async-trait",

View File

@ -1,12 +1,13 @@
[workspace]
members = ["plugin_test"]
resolver = "2"
[package]
name = "servers"
description = "Simple TCP server for clients written in Rust with plugins support"
version = "0.3.0-pre"
description = "Simple TCP server for clients written in Rust with plugins support."
version = "0.3.0"
license = "MIT"
authors = ["MedzikUser <nivua1fn@duck.com>"]
authors = ["MedzikUser <medzik@duck.com>"]
homepage = "https://github.com/MedzikUser/servers"
repository = "https://github.com/MedzikUser/servers.git"
edition = "2021"
@ -17,15 +18,15 @@ opt-level = 'z'
codegen-units = 1
[dependencies]
lazy_static = "1.4.0"
anyhow = "1.0.58"
async-trait = "0.1.56"
better-panic = "0.3.0"
lazy_static = "1.4.0"
libloading = "0.7.3"
tokio-tungstenite = "0.17.2"
tungstenite = "0.17.3"
tracing-subscriber = "0.3.15"
tracing = "0.1.36"
tracing-subscriber = "0.3.15"
tungstenite = "0.17.3"
clap = { version = "3.2.16", features = ["derive"] }
futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] }
tokio = { version = "1.20.1", features = ["rt-multi-thread", "macros", "net"] }

View File

@ -1,6 +1,7 @@
use servers::{
async_trait,
plugins::{Command, Event, Plugin, PluginManagerType, Registrar, Result},
tcp::Client, async_trait,
tcp::Client,
};
struct PluginTest;

View File

@ -2,9 +2,9 @@ use clap::Parser;
#[derive(Parser)]
#[clap(
name = "servers",
name = env!("CARGO_PKG_NAME"),
version = env!("CARGO_PKG_VERSION"),
about = "A simple TCP server for client which can be extended with plugins."
about = env!("CARGO_PKG_DESCRIPTION"),
)]
pub struct Cli {
#[clap(
@ -35,9 +35,9 @@ pub struct Cli {
pub ws_port: String,
#[clap(
long = "disable-websocket",
help = "Disable WebSocket proxy to Tcp",
long = "enable-websocket",
help = "Enable WebSocket proxy to Tcp [default disabled]",
display_order = 4
)]
pub ws_disable: bool,
pub ws_enable: bool,
}

View File

@ -23,7 +23,6 @@
pub mod commands;
pub mod logger;
mod macros;
pub mod plugins;
pub mod tcp;

View File

@ -1,39 +0,0 @@
/// Update value in the Mutex
///
/// ```
/// use std::sync::Mutex;
/// use servers::update_mutex;
///
/// // create a new Mutex
/// let mutex: Mutex<String> = Mutex::new(String::new());
///
/// // update the value in the Mutex
/// update_mutex!(mutex, "new value".to_string());
/// ```
#[macro_export]
macro_rules! update_mutex {
($mutex: expr, $($new_value:tt)+) => {
*$crate::lock_mutex!($mutex) = $($new_value)+;
};
}
/// Lock value in the Mutex
///
/// ```
/// use std::sync::Mutex;
/// use servers::lock_mutex;
///
/// // create a new Mutex
/// let mutex: Mutex<String> = Mutex::new("value".to_string());
///
/// // lock the Mutex
/// let value = lock_mutex!(mutex);
///
/// println!("{}", value);
/// ```
#[macro_export]
macro_rules! lock_mutex {
($mutex: expr) => {
$mutex.lock().expect("failed to lock mutex")
};
}

View File

@ -1,5 +1,6 @@
mod cli;
use clap::Parser;
use cli::Cli;
use servers::{
logger,
plugins::loader,
@ -8,7 +9,7 @@ use servers::{
use tokio::net::TcpListener;
use tracing::{error, info};
mod cli;
use crate::cli::Cli;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
@ -18,7 +19,7 @@ async fn main() -> anyhow::Result<()> {
let args = Cli::parse();
// if enabled start WebSocket server
if !args.ws_disable {
if args.ws_enable {
tokio::spawn(start_ws_server(
args.host.clone(),
args.ws_port,
@ -34,10 +35,10 @@ async fn main() -> anyhow::Result<()> {
/// Start tcp server
async fn start_tcp_server(host: String, port: String) -> anyhow::Result<()> {
// listen Tcp server
// listen TCP server
let listener = TcpListener::bind(format!("{host}:{port}")).await?;
info!("Tcp server started at: {}", listener.local_addr()?);
info!("TCP server started at: {}", listener.local_addr()?);
// load plugins, commands and events
let plugin_manager = loader()?;
@ -50,9 +51,7 @@ async fn start_tcp_server(host: String, port: String) -> anyhow::Result<()> {
// handle client connection in new thread
tokio::spawn(async move {
// get ip address of the client
let ip = client
.peer_addr()
.expect("failed to get peer address");
let ip = client.peer_addr().expect("failed to get peer address");
if let Err(e) = handle_connection(client, plugin_manager).await {
error!("Client {ip}: {e}")

View File

@ -1,6 +1,6 @@
//! Plugins loader
//!
//! # Writing a plugins
//! # Writing plugins
//!
//! Create a new project `cargo new --lib plugin`
//!

View File

@ -3,7 +3,7 @@ use std::net::SocketAddr;
use tokio::{
io::{self, AsyncReadExt, AsyncWriteExt},
net::{TcpStream},
net::TcpStream,
};
/// Max size of a TCP packet
@ -30,10 +30,15 @@ impl Client {
let len = self.stream.read(&mut buf).await?;
// select only used bytes from the buffer
let recv_buf = &buf[0..len];
let buf = &buf[0..len];
// encode buffer (&[u8]) to a String
let decoded = String::from_utf8(recv_buf.to_vec())?;
let mut decoded = String::from_utf8(buf.to_vec())?;
// remove new line characters
while decoded.ends_with("\n") || decoded.ends_with("\r") {
decoded.pop();
}
Ok(decoded)
}