-
-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): node's http server is enabled by default
- Loading branch information
1 parent
e53d859
commit bb2c2e6
Showing
27 changed files
with
275 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
implementations/rust/ockam/ockam_api/src/nodes/models/transport/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
mod common; | ||
mod json; | ||
mod port; | ||
mod request; | ||
mod response; | ||
|
||
pub use common::*; | ||
pub use json::*; | ||
pub use port::*; | ||
pub use request::*; | ||
pub use response::*; |
47 changes: 47 additions & 0 deletions
47
implementations/rust/ockam/ockam_api/src/nodes/models/transport/port.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::Result; | ||
use std::fmt::Display; | ||
use tokio::net::TcpListener; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Port { | ||
TryExplicitOrRandom(u16), | ||
Explicit(u16), | ||
} | ||
|
||
impl Port { | ||
pub async fn bind_to_tcp_listener(&self) -> Result<TcpListener> { | ||
match self { | ||
Self::TryExplicitOrRandom(port) => { | ||
// Try to bind to the explicit port | ||
match TcpListener::bind(&format!("127.0.0.1:{port}")).await { | ||
Ok(listener) => Ok(listener), | ||
Err(err) => { | ||
// If the port is already in use, bind to a random port | ||
if err.kind() == std::io::ErrorKind::AddrInUse { | ||
Ok(TcpListener::bind("127.0.0.1:0").await?) | ||
} else { | ||
Err(err.into()) | ||
} | ||
} | ||
} | ||
} | ||
// If explicit, try to bind to that port or fail | ||
Self::Explicit(port) => Ok(TcpListener::bind(&format!("127.0.0.1:{port}")).await?), | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<u16> for Port { | ||
fn as_ref(&self) -> &u16 { | ||
match self { | ||
Self::TryExplicitOrRandom(port) => port, | ||
Self::Explicit(port) => port, | ||
} | ||
} | ||
} | ||
|
||
impl Display for Port { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.as_ref()) | ||
} | ||
} |
Oops, something went wrong.