Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Example to ntex 0.7.x #1

Merged
merged 3 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/application/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ edition = "2018"
workspace = "../"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
2 changes: 1 addition & 1 deletion examples/async-handlers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ version = "2.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
futures = "0.3.1"
bytes = "0.5"
2 changes: 1 addition & 1 deletion examples/databases/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
diesel = { version = "2", features = ["sqlite", "r2d2"] }
serde = { version = "1", features = ["derive"] }
uuid = { version = "1", features = ["v4"] }
2 changes: 1 addition & 1 deletion examples/either/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
2 changes: 1 addition & 1 deletion examples/errors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
derive_more = "0.99"
env_logger = "0.7"
log = "0.4"
2 changes: 1 addition & 1 deletion examples/extractors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
serde = "1.0"
serde_json = "1.0"
2 changes: 1 addition & 1 deletion examples/flexible-responders/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
serde = "1.0"
2 changes: 1 addition & 1 deletion examples/getting-started/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ edition = "2018"
workspace = "../"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
2 changes: 1 addition & 1 deletion examples/http2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio", "openssl"] }
ntex = { version = "0.7.2", features = ["tokio", "openssl"] }
openssl = { version = "0.10", features = ["v110", "vendored"] }
2 changes: 1 addition & 1 deletion examples/main-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
6 changes: 3 additions & 3 deletions examples/middleware/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio", "cookie"] }
ntex-session = { version = "0.2.0" }
ntex = { version = "0.7.2", features = ["tokio", "cookie"] }
ntex-session = { version = "0.3.0" }
futures-util = { version = "0.3.7", default-features = false, features = ["std"] }
env_logger = "0.7"
env_logger = "0.10.0"
6 changes: 3 additions & 3 deletions examples/middleware/src/errorhandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// <error-handler>
use ntex::http::header;
use ntex::service::{Middleware, Service};
use ntex::service::{Middleware, Service, ServiceCtx};
use ntex::util::BoxFuture;
use ntex::web;

Expand Down Expand Up @@ -31,9 +31,9 @@ where

ntex::forward_poll_ready!(service);

fn call(&self, req: web::WebRequest<Err>) -> Self::Future<'_> {
fn call<'a>(&'a self, req: web::WebRequest<Err>, ctx: ServiceCtx<'a, Self>) -> Self::Future<'_> {
Box::pin(async move {
self.service.call(req).await.map(|mut res| {
ctx.call(&self.service, req).await.map(|mut res| {
let status = res.status();
if status.is_client_error() || status.is_server_error() {
res.headers_mut().insert(
Expand Down
7 changes: 3 additions & 4 deletions examples/middleware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod logger;
pub mod user_sessions;

// <simple>
use ntex::service::{Middleware, Service};
use ntex::service::{Middleware, Service, ServiceCtx};
use ntex::util::BoxFuture;
use ntex::web;

Expand Down Expand Up @@ -37,11 +37,10 @@ where

ntex::forward_poll_ready!(service);

fn call(&self, req: web::WebRequest<Err>) -> Self::Future<'_> {
fn call<'a>(&'a self, req: web::WebRequest<Err>, ctx: ServiceCtx<'a, Self>) -> Self::Future<'_> {
println!("Hi from start. You requested: {}", req.path());

let fut = self.service.call(req);

let fut = ctx.call(&self.service, req);
Box::pin(async move {
let res = fut.await?;

Expand Down
2 changes: 1 addition & 1 deletion examples/powerful-extractors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
serde = "1.0"
2 changes: 1 addition & 1 deletion examples/request-handlers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
2 changes: 1 addition & 1 deletion examples/request-routing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
4 changes: 2 additions & 2 deletions examples/requests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ edition = "2018"
[dependencies]
serde = "1.0"
serde_json = "1.0"
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
futures = "0.3.1"
ntex-multipart = "0.2.0"
ntex-multipart = "0.3.0"
2 changes: 1 addition & 1 deletion examples/responses/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio", "compress"] }
ntex = { version = "0.7.2", features = ["tokio", "compress"] }
serde = "1.0"
futures = "0.3.1"
bytes = "0.5"
2 changes: 1 addition & 1 deletion examples/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ workspace = "../"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio", "openssl"] }
ntex = { version = "0.7.2", features = ["tokio", "openssl"] }
futures = "0.3"
openssl = { version = "0.10", features = ["vendored"] }
tokio = { version = "1.16.1", features = ["full"] }
4 changes: 2 additions & 2 deletions examples/static-files/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex-files = "0.2.0"
ntex = { version = "0.7.2", features = ["tokio"] }
ntex-files = "0.3.0"
mime = "0.3"
2 changes: 1 addition & 1 deletion examples/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "1.0.0"
edition = "2018"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
futures = "0.3"
futures-util = "0.3"
bytes = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion examples/url-dispatch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2018"
workspace = "../"

[dependencies]
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
futures = "0.3.1"
openssl = "0.10"
serde = "1.0"
2 changes: 1 addition & 1 deletion examples/websockets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ edition = "2018"

[dependencies]
futures = "0.3.27"
ntex = { version = "0.6.5", features = ["tokio"] }
ntex = { version = "0.7.2", features = ["tokio"] }
33 changes: 16 additions & 17 deletions examples/websockets/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// <websockets>
use std::{cell::RefCell, io, rc::Rc, time::Duration, time::Instant};

use futures::future::{ready, select, Either};
use ntex::service::{fn_factory_with_config, fn_shutdown, Service};
use ntex::util::Bytes;
use ntex::web;
use ntex::web::ws;
use ntex::util::Bytes;
use ntex::{fn_service, chain};
use ntex::{channel::oneshot, rt, time};
use ntex::{fn_service, pipeline};
use futures::future::{ready, select, Either};
use ntex::service::{fn_factory_with_config, fn_shutdown, Service};

/// How often heartbeat pings are sent
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
Expand All @@ -22,9 +21,9 @@ struct WsState {

/// WebSockets service factory
async fn ws_service(
sink: ws::WsSink,
sink: web::ws::WsSink,
) -> Result<
impl Service<ws::Frame, Response = Option<ws::Message>, Error = io::Error>,
impl Service<web::ws::Frame, Response = Option<web::ws::Message>, Error = io::Error>,
web::Error,
> {
let state = Rc::new(RefCell::new(WsState { hb: Instant::now() }));
Expand All @@ -39,22 +38,22 @@ async fn ws_service(
let service = fn_service(move |frame| {
let item = match frame {
// update heartbeat
ws::Frame::Ping(msg) => {
web::ws::Frame::Ping(msg) => {
state.borrow_mut().hb = Instant::now();
Some(ws::Message::Pong(msg))
Some(web::ws::Message::Pong(msg))
}
// update heartbeat
ws::Frame::Pong(_) => {
web::ws::Frame::Pong(_) => {
state.borrow_mut().hb = Instant::now();
None
}
// send message back
ws::Frame::Text(text) => Some(ws::Message::Text(
web::ws::Frame::Text(text) => Some(web::ws::Message::Text(
String::from_utf8(Vec::from(text.as_ref())).unwrap().into(),
)),
ws::Frame::Binary(bin) => Some(ws::Message::Binary(bin)),
web::ws::Frame::Binary(bin) => Some(web::ws::Message::Binary(bin)),
// close connection
ws::Frame::Close(reason) => Some(ws::Message::Close(reason)),
web::ws::Frame::Close(reason) => Some(web::ws::Message::Close(reason)),
// ignore other frames
_ => None,
};
Expand All @@ -67,13 +66,13 @@ async fn ws_service(
});

// pipe our service with on_shutdown callback
Ok(pipeline(service).and_then(on_shutdown))
Ok(chain(service).and_then(on_shutdown))
}

/// helper method that sends ping to client every heartbeat interval
async fn heartbeat(
state: Rc<RefCell<WsState>>,
sink: ws::WsSink,
sink: web::ws::WsSink,
mut rx: oneshot::Receiver<()>,
) {
loop {
Expand All @@ -88,7 +87,7 @@ async fn heartbeat(

// send ping
if sink
.send(ws::Message::Ping(Bytes::default()))
.send(web::ws::Message::Ping(Bytes::default()))
.await
.is_err()
{
Expand All @@ -105,7 +104,7 @@ async fn heartbeat(

/// do websocket handshake and start web sockets service
async fn ws_index(req: web::HttpRequest) -> Result<web::HttpResponse, web::Error> {
ws::start(req, fn_factory_with_config(ws_service)).await
web::ws::start(req, fn_factory_with_config(ws_service)).await
}

#[ntex::main]
Expand Down