Skip to content

Commit

Permalink
feat(multi-module): fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
l3r8yJ committed Jun 15, 2024
1 parent 775664b commit ebf9f10
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
4 changes: 3 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ name = "fakehub-cli"
path = "src/main.rs"

[dependencies]
server = { path = "../server" }
clap = "4.5.7"
server = { path = "../server" }
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] }
8 changes: 6 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
fn main() {
println!("Hello, world!");
use server::Server;

#[tokio::main]
async fn main() {
let server = Server::new(3000);
server.start().await.expect("Can't start the server");

Check warning on line 6 in cli/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/main.rs#L3-L6

Added lines #L3 - L6 were not covered by tests
}
2 changes: 1 addition & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "fakehub"
name = "server"
version = "0.0.0"
edition = "2021"
license = "MIT"
Expand Down
36 changes: 31 additions & 5 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use axum::Router;
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use axum::routing::get;
use tokio::net::TcpListener;

use crate::routes::home;
use crate::xml::storage::touch_storage;
Expand All @@ -29,13 +30,38 @@ mod routes;
mod xml;

#[derive(Default)]
pub struct Server {}
pub struct Server {
port: usize,
}

impl Server {
pub fn new(port: usize) -> Server {
Server { port }
}
}

impl Server {
pub async fn start() {
pub async fn start(self) -> anyhow::Result<()>{
touch_storage(Some("fakehub.xml"));
let app = Router::new().route("/", get(home::home));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
let app: Router = Router::new().route("/", get(home::home));
let addr: String = format!("0.0.0.0:{}", self.port);
let started: std::io::Result<TcpListener> = TcpListener::bind(addr.clone()).await;
match started {
Ok(listener) => axum::serve(listener, app).await?,
Err(err) => {
panic!("Can't bind address {}: '{}'", addr.clone(), err)

Check warning on line 52 in server/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

server/src/lib.rs#L44-L52

Added lines #L44 - L52 were not covered by tests
}
};
Ok(())

Check warning on line 55 in server/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

server/src/lib.rs#L55

Added line #L55 was not covered by tests
}
}

mod tests {

#[test]
fn creates_the_server() -> anyhow::Result<()> {
let server = crate::Server::new(1234);
assert_eq!(server.port, 1234);
Ok(())
}
}

0 comments on commit ebf9f10

Please sign in to comment.