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

Added connection info as a second parameter #3

Merged
merged 3 commits into from
Mar 27, 2023

Conversation

fatfingers23
Copy link
Contributor

This is to resolve #2 . I implemented it as best as possible, following the issue's details. There is now a second parameter that passes along with the request. It is connection_info, which holds info I got from the TcpSocket connection, like the request IP. I am still learning Rust, so please let me know if there is something I need to do differently. I was wondering if I should be unwrapping the local_addr or peer_addr like I am. Also, with the way I passed the new struct connection info from the reactor.

Copy link
Owner

@ibraheemdev ibraheemdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! Looks good overall, just a couple notes.

README.md Outdated
@@ -11,7 +11,7 @@ use astra::{Body, Response, Server};

fn main() {
Server::bind("localhost:3000")
.serve(|_req| Response::new(Body::new("Hello World!")))
.serve(|_req, _connection_info| Response::new(Body::new("Hello World!")))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this (as well as in other examples) to be named _info, a little less noisy in examples.

@@ -0,0 +1,18 @@
use astra::{Body, Request, Response, Server, ConnectionInfo};
use std::sync::atomic::{Ordering};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need this import in this example.

.expect("serve failed");
}

fn route(router: Arc<Router>, mut req: Request) -> Response {
// Try to find the handler for the requested path
// Try to find the handler for the requested path`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a typo?

@@ -34,12 +34,12 @@ fn main() {

Server::bind("localhost:3000")
// Pass the router to `route`, along with the request
.serve(move |req| route(router.clone(), req))
.serve(move |req, _| route(router.clone(), req))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably keep this consistent with _info as well.

src/net.rs Outdated
@@ -42,7 +42,12 @@ impl Reactor {
pub fn register(&self, sys: sys::TcpStream) -> io::Result<TcpStream> {
sys.set_nonblocking(true)?;
let mut sys = mio::net::TcpStream::from_std(sys);

let local_addr = sys.local_addr().unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really see the point of astra exposing the local_addr.

src/net.rs Outdated

let local_addr = sys.local_addr().unwrap();
let peer_addr = sys.peer_addr().unwrap();
let connection_info = ConnectionInfo {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to store this on the TcpStream. We can just get it in the server loop.

src/net.rs Outdated
@@ -42,7 +42,12 @@ impl Reactor {
pub fn register(&self, sys: sys::TcpStream) -> io::Result<TcpStream> {
sys.set_nonblocking(true)?;
let mut sys = mio::net::TcpStream::from_std(sys);

let local_addr = sys.local_addr().unwrap();
let peer_addr = sys.peer_addr().unwrap();
Copy link
Owner

@ibraheemdev ibraheemdev Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can expose an Option<SocketAddr> to handle failures.

@fatfingers23
Copy link
Contributor Author

Thank you for the feedback! I've made the changes you asked for. Please let me know if I missed anything or if something needs to be changed.

@ibraheemdev
Copy link
Owner

This looks great. Thanks! I'll try to get a release out soon.

@ibraheemdev ibraheemdev merged commit 910f995 into ibraheemdev:master Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature suggestion: client address of requests
2 participants