Skip to content

Commit

Permalink
* Updated the README.md
Browse files Browse the repository at this point in the history
* Updated documentation
* Fixed mess with std::io vs tokio::io
  • Loading branch information
Roman Emreis authored and Roman Emreis committed Oct 4, 2024
1 parent c355ef4 commit 7ce1fc0
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 164 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "volga"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
authors = ["Roman Emreis"]
license = "MIT"
Expand Down
34 changes: 14 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tokio = "1.40.0"
use volga::{App, Results, AsyncEndpointsMapping};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn main() -> tokio::io::Result<()> {
// Start the server
let mut server = App::build("127.0.0.1:7878").await?;

Expand All @@ -29,17 +29,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Results::text("Hello World!")
}).await;

server.run().await?;

Ok(())
server.run().await
}
```
### Synchronous handler:
```rust
use volga::{App, Results, SyncEndpointsMapping};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn main() -> tokio::io::Result<()> {
// Start the server
let mut server = App::build("127.0.0.1:7878").await?;

Expand All @@ -48,17 +46,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Results::text("Hello World!")
}).await;

server.run().await?;

Ok(())
server.run().await
}
```
### Custom middleware:
```rust
use volga::{App, Results, AsyncEndpointsMapping, AsyncMiddlewareMapping};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn main() -> tokio::io::Result<()> {
// Start the server
let mut server = App::build("127.0.0.1:7878").await?;

Expand All @@ -78,17 +74,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Results::text("Hello World!")
}).await;

server.run().await?;

Ok(())
server.run().await
}
```
### Reading query parameters
```rust
use volga::{App, AsyncEndpointsMapping, Results, Params};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn main() -> tokio::io::Result<()> {
let mut app = App::build("127.0.0.1:7878").await?;

// GET /test?id=11
Expand All @@ -99,15 +93,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Results::text("Pass!")
}).await;

Ok(())
app.run().await
}
```
### Reading route parameters
```rust
use volga::{App, AsyncEndpointsMapping, Results, Params};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn main() -> tokio::io::Result<()> {
let mut app = App::build("127.0.0.1:7878").await?;

// GET /test/11
Expand All @@ -118,7 +112,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Results::text("Pass!")
}).await;

Ok(())
app.run().await
}
```
### Reading JSON payload
Expand All @@ -133,7 +127,7 @@ struct User {
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn main() -> tokio::io::Result<()> {
let mut app = App::build("127.0.0.1:7878").await?;

// POST /test
Expand All @@ -144,7 +138,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Results::text("Pass!")
}).await;

Ok(())
app.run().await
}
```
### Returning a JSON
Expand All @@ -159,7 +153,7 @@ struct User {
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn main() -> tokio::io::Result<()> {
let mut app = App::build("127.0.0.1:7878").await?;

app.map_get("/test", |req| async move {
Expand All @@ -171,7 +165,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Results::json(&user) // { name: "John", age: 35 }
}).await;

Ok(())
app.run().await
}
```

25 changes: 12 additions & 13 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use bytes::Bytes;
use httparse::EMPTY_HEADER;
use http::{Request, Response};
use std::{io, sync::Arc};
use std::sync::Arc;

use tokio::{
net::{TcpListener, TcpStream},
io::{AsyncReadExt, AsyncWriteExt},
io::{self,AsyncReadExt, AsyncWriteExt},
sync::{broadcast, Mutex},
signal
};
Expand All @@ -27,16 +27,14 @@ pub mod mapping;
///
/// # Examples
/// ```no_run
/// use volga::App;
///use volga::App;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let mut app = App::build("127.0.0.1:7878").await?;
///
/// app.run().await?;
///
/// Ok(())
/// }
///#[tokio::main]
///async fn main() -> tokio::io::Result<()> {
/// let mut app = App::build("127.0.0.1:7878").await?;
///
/// app.run().await
///}
/// ```
pub struct App {
middlewares: Arc<Mutex<Middlewares>>,
Expand All @@ -59,9 +57,9 @@ impl HttpContext {
}

impl App {
pub async fn build(socket: &str) -> Result<App, Box<dyn std::error::Error + Send + Sync>> {
pub async fn build(socket: &str) -> io::Result<App> {
if socket.is_empty() {
return Err("An empty socket has been provided.".into());
return Err(io::Error::new(io::ErrorKind::InvalidData, "An empty socket has been provided."));
}

let tcp_listener = TcpListener::bind(socket).await?;
Expand Down Expand Up @@ -93,6 +91,7 @@ impl App {
};

println!("Start listening: {socket}");

Ok(server)
}

Expand Down
100 changes: 50 additions & 50 deletions src/app/endpoints/mapping/asynchronous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ pub trait AsyncEndpointsMapping {
/// ```no_run
///use volga::{App, AsyncEndpointsMapping, Results};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let mut app = App::build("127.0.0.1:7878").await?;
///
/// app.map_get("/test", |_req| async {
/// Results::text("Pass!")
/// }).await;
///
/// Ok(())
/// }
///#[tokio::main]
///async fn main() -> tokio::io::Result<()> {
/// let mut app = App::build("127.0.0.1:7878").await?;
///
/// app.map_get("/test", |_req| async {
/// Results::text("Pass!")
/// }).await;
///
/// app.run().await
///}
/// ```
fn map_get<F, Fut>(&mut self, pattern: &str, handler: F) -> impl Future<Output = ()> + Send
where
Expand All @@ -38,16 +38,16 @@ pub trait AsyncEndpointsMapping {
/// ```no_run
///use volga::{App, AsyncEndpointsMapping, Results};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let mut app = App::build("127.0.0.1:7878").await?;
///
/// app.map_post("/test", |_req| async {
/// Results::text("Pass!")
/// }).await;
///
/// Ok(())
/// }
///#[tokio::main]
///async fn main() -> tokio::io::Result<()> {
/// let mut app = App::build("127.0.0.1:7878").await?;
///
/// app.map_post("/test", |_req| async {
/// Results::text("Pass!")
/// }).await;
///
/// app.run().await
///}
/// ```
fn map_post<F, Fut>(&mut self, pattern: &str, handler: F) -> impl Future<Output = ()> + Send
where
Expand All @@ -60,16 +60,16 @@ pub trait AsyncEndpointsMapping {
/// ```no_run
///use volga::{App, AsyncEndpointsMapping, Results};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let mut app = App::build("127.0.0.1:7878").await?;
///
/// app.map_put("/test", |_req| async {
/// Results::text("Pass!")
/// }).await;
///
/// Ok(())
/// }
///#[tokio::main]
///async fn main() -> tokio::io::Result<()> {
/// let mut app = App::build("127.0.0.1:7878").await?;
///
/// app.map_put("/test", |_req| async {
/// Results::text("Pass!")
/// }).await;
///
/// app.run().await
///}
/// ```
fn map_put<F, Fut>(&mut self, pattern: &str, handler: F) -> impl Future<Output = ()> + Send
where
Expand All @@ -82,16 +82,16 @@ pub trait AsyncEndpointsMapping {
/// ```no_run
///use volga::{App, AsyncEndpointsMapping, Results};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let mut app = App::build("127.0.0.1:7878").await?;
///
/// app.map_delete("/test", |_req| async {
/// Results::text("Pass!")
/// }).await;
///
/// Ok(())
/// }
///#[tokio::main]
///async fn main() -> tokio::io::Result<()> {
/// let mut app = App::build("127.0.0.1:7878").await?;
///
/// app.map_delete("/test", |_req| async {
/// Results::text("Pass!")
/// }).await;
///
/// app.run().await
///}
/// ```
fn map_delete<F, Fut>(&mut self, pattern: &str, handler: F) -> impl Future<Output = ()> + Send
where
Expand All @@ -104,16 +104,16 @@ pub trait AsyncEndpointsMapping {
/// ```no_run
///use volga::{App, AsyncEndpointsMapping, Results};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let mut app = App::build("127.0.0.1:7878").await?;
///
/// app.map_patch("/test", |_req| async {
/// Results::text("Pass!")
/// }).await;
///
/// Ok(())
/// }
///#[tokio::main]
///async fn main() -> tokio::io::Result<()> {
/// let mut app = App::build("127.0.0.1:7878").await?;
///
/// app.map_patch("/test", |_req| async {
/// Results::text("Pass!")
/// }).await;
///
/// app.run().await
///}
/// ```
fn map_patch<F, Fut>(&mut self, pattern: &str, handler: F) -> impl Future<Output = ()> + Send
where
Expand Down
Loading

0 comments on commit 7ce1fc0

Please sign in to comment.