Skip to content

Commit

Permalink
Merge pull request #42 from h1alexbel/13
Browse files Browse the repository at this point in the history
feat(multi-module): splitted to modules
  • Loading branch information
h1alexbel authored Jun 23, 2024
2 parents 73a71f9 + 2c673fa commit f8bde4e
Show file tree
Hide file tree
Showing 16 changed files with 271 additions and 43 deletions.
2 changes: 1 addition & 1 deletion clippy.toml → .clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# SOFTWARE.
13 changes: 7 additions & 6 deletions .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ concurrency:
env:
RUSTFLAGS: "-Dwarnings"
jobs:
clippy:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- run: cargo clippy --all-targets --all-features
- run: cargo fmt --check
build:
strategy:
fail-fast: false
Expand All @@ -42,11 +48,6 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- run: cargo clean
- run: cargo test
- run: cargo test --release
- run: cargo fmt --check
clippy:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- run: cargo clippy --all-targets --all-features
28 changes: 6 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,9 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

[package]
name = "fakehub"
version = "0.0.0"
edition = "2021"
license = "MIT"
description = """
GitHub API Server Stub. Fully functional fake version of a GitHub API that
supports all the features and works locally, with no connection to GitHub at
all.
"""
authors = ["Aliaksei Bialíauski <[email protected]>", "Ivanchuk Ivan <[email protected]>"]

[dependencies]
anyhow = "1.0.86"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros", "fs"] }
axum = "0.7.5"
log = { version = "0.4.21", features = [] }
env_logger = "0.11.3"
tempdir = "0.3.7"
[workspace]
members = [
"server",
"cli",
]
resolver = "1"
39 changes: 39 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# The MIT License (MIT)
#
# Copyright (c) 2024 Aliaksei Bialiauski
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
[package]
name = "cli"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "cli"
path = "src/main.rs"

[dev-dependencies]
assert_cmd = "2.0.14"

[dependencies]
anyhow = "1.0.86"
clap = { version = "4.5.7", features = ["derive"] }
server = { path = "../server" }
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] }
log = "0.4.21"
33 changes: 33 additions & 0 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Aliaksei Bialiauski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use clap::Parser;

// @todo #13:15min Handle port argument
// We should process the port argument and
// pass it to the server on `start` command.
// Start command should be added also with clap
#[derive(Parser, Debug)]
pub(crate) struct Args {
/// The port to run
#[arg(short, long, default_value_t = 3000)]
pub(crate) port: usize,
}
19 changes: 7 additions & 12 deletions src/main.rs → cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use axum::routing::get;
use axum::Router;
mod routes;
mod xml;
use crate::routes::home;
use crate::xml::storage::touch_storage;
use clap::Parser;

#[tokio::main]
async fn main() {
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();
use crate::args::Args;

mod args;

fn main() {
let _ = Args::parse();
}
36 changes: 36 additions & 0 deletions cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Aliaksei Bialiauski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use std::str;

use anyhow::Result;
use assert_cmd::Command;

#[test]
fn should_output_help() -> Result<()> {
env!("CARGO_BIN_EXE_cli");
let assertion = Command::cargo_bin("cli")?.arg("--help").assert();
let bytes = assertion.get_output().stdout.as_slice();
let output = str::from_utf8(bytes)?;
assert!(output.contains("The port to run"));
assert!(output.contains("3000"));
Ok(())
}
22 changes: 22 additions & 0 deletions cli/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Aliaksei Bialiauski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
mod integration_test;
45 changes: 45 additions & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# The MIT License (MIT)
#
# Copyright (c) 2024 Aliaksei Bialiauski
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
[package]
name = "server"
version = "0.0.0"
edition = "2021"
license = "MIT"
description = """
GitHub API Server Stub. Fully functional fake version of a GitHub API that
supports all the features and works locally, with no connection to GitHub at
all.
"""
authors = ["Aliaksei Bialíauski <[email protected]>", "Ivanchuk Ivan <[email protected]>"]

[lib]
path = "src/lib.rs"

[dependencies]
anyhow = "1.0.86"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros", "fs"] }
axum = "0.7.5"
log = { version = "0.4.21", features = [] }
env_logger = "0.11.3"
tempdir = "0.3.7"
File renamed without changes.
72 changes: 72 additions & 0 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Aliaksei Bialiauski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use std::io;

use anyhow::Result;
use axum::routing::get;
use axum::Router;
use tokio::net::TcpListener;

use crate::routes::home;
use crate::xml::storage::touch_storage;

mod routes;
mod xml;

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

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

impl Server {
pub async fn start(self) -> Result<()> {
touch_storage(Some("fakehub.xml"));
let app: Router = Router::new().route("/", get(home::home));
let addr: String = format!("0.0.0.0:{}", self.port);
let started: 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)
}
};
Ok(())
}
}

#[cfg(test)]
mod tests {
use anyhow::Result;

#[test]
fn creates_the_server() -> Result<()> {
let server = crate::Server::new(1234);
assert_eq!(server.port, 1234);
Ok(())
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions src/xml/storage.rs → server/src/xml/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ pub fn touch_storage(path: Option<&str>) -> File {

#[cfg(test)]
mod tests {
use anyhow::Result;
use tempdir::TempDir;

use crate::xml::storage::touch_storage;

#[test]
fn creates_xml_storage() -> anyhow::Result<()> {
fn creates_xml_storage() -> Result<()> {
let temp = TempDir::new("temp")?;
let path = temp.path().join("fakehub.xml");
let storage = path.to_str();
Expand All @@ -58,7 +59,7 @@ mod tests {
}

#[test]
fn creates_xml_storage_with_different_name() -> anyhow::Result<()> {
fn creates_xml_storage_with_different_name() -> Result<()> {
let temp = TempDir::new("temp")?;
let path = temp.path().join("test.xml");
let storage = path.to_str();
Expand Down

1 comment on commit f8bde4e

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on f8bde4e Jun 23, 2024

Choose a reason for hiding this comment

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

Puzzle 13-97a91bb1 discovered in cli/src/args.rs) and submitted as #43. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.