From 74851fa5b23010c99e1a7ef308438084733433e0 Mon Sep 17 00:00:00 2001 From: Thomas Lohse <49527735+t-lohse@users.noreply.github.com> Date: Wed, 13 Mar 2024 17:28:49 +0100 Subject: [PATCH] SW5 (#2) * major refactor * major refactor * added submodule * fix naming in CI * Update Cargo.toml * Update Cargo.toml * Update Cargo.toml * Update Cargo.toml * Update build_artifacts.yml * Update build_artifacts.yml * Update build_artifacts.yml * Update build_artifacts.yml * Update build_artifacts.yml * Update build_artifacts.yml * Update build_artifacts.yml * Update build_artifacts.yml * Update build_artifacts.yml * Update check_format.yml * Update test.yml * Update main.rs * Update build_artifacts.yml * Update .gitignore * Delete .idea directory * Update .gitmodules * Update test.yml * Update test.yml * Update test.yml * Update .gitmodules --- .github/workflows/build_artifacts.yml | 36 +++++++++++++++++++ .github/workflows/check_format.yml | 30 ++++++++++++++++ .github/workflows/test.yml | 25 ++++++++++++++ .gitignore | 2 ++ .gitmodules | 4 +++ Cargo.toml | 36 +++++++++++++++---- Ecdar-ProtoBuf | 1 + ecdar-web-macros/Cargo.toml | 16 --------- ecdar-web/Cargo.toml | 15 -------- ecdar-web/src/configuration.rs | 50 --------------------------- ecdar-web/src/help.txt | 10 ------ ecdar-web/src/main.rs | 26 -------------- src/build.rs | 5 +++ {ecdar-web-macros/src => src}/lib.rs | 36 ++++++++++--------- src/main.rs | 34 ++++++++++++++++++ 15 files changed, 186 insertions(+), 140 deletions(-) create mode 100644 .github/workflows/build_artifacts.yml create mode 100644 .github/workflows/check_format.yml create mode 100644 .github/workflows/test.yml create mode 100644 .gitmodules create mode 160000 Ecdar-ProtoBuf delete mode 100644 ecdar-web-macros/Cargo.toml delete mode 100644 ecdar-web/Cargo.toml delete mode 100644 ecdar-web/src/configuration.rs delete mode 100644 ecdar-web/src/help.txt delete mode 100644 ecdar-web/src/main.rs create mode 100644 src/build.rs rename {ecdar-web-macros/src => src}/lib.rs (77%) create mode 100644 src/main.rs diff --git a/.github/workflows/build_artifacts.yml b/.github/workflows/build_artifacts.yml new file mode 100644 index 0000000..daa699e --- /dev/null +++ b/.github/workflows/build_artifacts.yml @@ -0,0 +1,36 @@ +name: Build Artifacts + +on: + workflow_dispatch: + push: + +jobs: + build: + strategy: + matrix: + os: [ ubuntu-latest, windows-latest, macos-latest ] + name: Build ${{ matrix.os }} + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v3 + with: + submodules: recursive + - uses: arduino/setup-protoc@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + - uses: dtolnay/rust-toolchain@stable + - uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true + - name: cargo build --release + uses: clechasseur/rs-cargo@v1 + with: + command: build + args: --release + - uses: actions/upload-artifact@v3 + with: + name: ecdar-web-${{ matrix.os }} + path: ${{ runner.os == 'Windows' && 'target/release/ecdar-web.exe' || 'target/release/ecdar-web' }} + if-no-files-found: error + retention-days: 7 diff --git a/.github/workflows/check_format.yml b/.github/workflows/check_format.yml new file mode 100644 index 0000000..6ec49d9 --- /dev/null +++ b/.github/workflows/check_format.yml @@ -0,0 +1,30 @@ +name: Check formatting + +on: + workflow_dispatch: + push: + +jobs: + fmt: + name: cargo fmt & Clippy lint and check + runs-on: ubuntu-latest + steps: + - run: sudo apt-get install llvm protobuf-compiler + - uses: actions/checkout@v3 + with: + submodules: recursive + - uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt + - uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true + - name: cargo fmt --all + uses: clechasseur/rs-cargo@v1 + with: + command: fmt + args: --all -- --check + - name: clippy --all-targets --all-features + uses: clechasseur/rs-clippy-check@v3 + with: + args: --all-targets --all-features -- -D warnings diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..a5b094e --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,25 @@ +name: Tests + +on: + workflow_dispatch: + push: + +jobs: + test: + strategy: + matrix: + os: [ ubuntu-latest, windows-latest, macos-latest ] + name: Test ${{ matrix.os }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + submodules: recursive + - uses: arduino/setup-protoc@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + - uses: dtolnay/rust-toolchain@stable + - uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true + - run: cargo test diff --git a/.gitignore b/.gitignore index 6985cf1..7aa7025 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + +.idea diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..86bce0b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "Ecdar-ProtoBuf"] + path = Ecdar-ProtoBuf + url = https://github.com/Ecdar/Ecdar-ProtoBuf + branch = SW5 diff --git a/Cargo.toml b/Cargo.toml index 860b492..02ea9cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,30 @@ -[workspace] -resolver = "2" -members =[ - "ecdar-web", - "ecdar-web-macros" -] +[package] +name = "ecdar_web" +version = "0.1.0" +edition = "2021" +build = "src/build.rs" +default-run = "ecdar_web" + + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +proc-macro = true + +[dependencies] +axum = { version = "0.6.20", features = ["json", "macros"]} +#ecdar-protobuf = { git = "https://github.com/ECDAR-AAU-SW-P5/Ecdar-ProtoBuf-rs", version = "0.1.0", features = ["client", "serde"] } +serde = { version = "1.0.190", features = ["derive"] } +tokio = { version = "1.33.0", features = ["rt", "macros", "rt-multi-thread"] } +tower-http = { version = "0.4.4", features = ["fs"] } +convert_case = "0.6.0" +ecdar-protobuf-transpiler = { git = "https://github.com/Ecdar/Ecdar-ProtoBuf-Transpiler-rs", version = "0.1.0" } +proc-macro2 = "1.0.69" +quote = "1.0.33" +tonic = "0.11.0" +clap = { version = "4.2.1", features = [ "derive", "string" ] } + + +[build-dependencies] +tonic-build = "0.11.0" + diff --git a/Ecdar-ProtoBuf b/Ecdar-ProtoBuf new file mode 160000 index 0000000..053d7e9 --- /dev/null +++ b/Ecdar-ProtoBuf @@ -0,0 +1 @@ +Subproject commit 053d7e9a5d3aae7cc7ee273a188d7890609d446d diff --git a/ecdar-web-macros/Cargo.toml b/ecdar-web-macros/Cargo.toml deleted file mode 100644 index b4a1d52..0000000 --- a/ecdar-web-macros/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "ecdar-web-macros" -version = "0.1.0" -edition = "2021" - -[lib] -proc-macro = true - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -convert_case = "0.6.0" -ecdar-protobuf-transpiler = { git = "https://github.com/ECDAR-AAU-SW-P5/Ecdar-ProtoBuf-Transpiler-rs", version = "0.1.0" } -proc-macro2 = "1.0.69" -quote = "1.0.33" - diff --git a/ecdar-web/Cargo.toml b/ecdar-web/Cargo.toml deleted file mode 100644 index fe461bb..0000000 --- a/ecdar-web/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "ecdar-web" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -axum = { version = "0.6.20", features = ["json", "macros"]} -ecdar-protobuf = { git = "https://github.com/ECDAR-AAU-SW-P5/Ecdar-ProtoBuf-rs", version = "0.1.0", features = ["client", "serde"] } -ecdar-web-macros = { path = "../ecdar-web-macros" } -serde = { version = "1.0.190", features = ["derive"] } -tokio = { version = "1.33.0", features = ["rt", "macros", "rt-multi-thread"] } -tower-http = { version = "0.4.4", features = ["fs"] } - diff --git a/ecdar-web/src/configuration.rs b/ecdar-web/src/configuration.rs deleted file mode 100644 index 78a1e0c..0000000 --- a/ecdar-web/src/configuration.rs +++ /dev/null @@ -1,50 +0,0 @@ -use std::{env::args, process::exit}; - -#[derive(Debug)] -pub struct Configuration { - pub root: String, - pub serve: String, -} - -impl Configuration { - pub fn create() -> Self { - let mut new = Self::default(); - let mut args = args(); - args.next().expect("Not running a program"); - - loop { - let option = match args.next() { - Some(option) => option, - None => break, - }; - - match option.as_str() { - "--root" | "-r" => { - let root = args.next().expect(r#"Missing a folder after "root""#); - new.root = root; - } - - "--serve" | "-s" => { - let ip = args.next().expect(r#"Missing an ip address after "serve"#); - new.serve = ip; - } - - "--help" | "-h" | _ => { - println!("{}", include_str!("./help.txt")); - exit(0); - } - } - } - - new - } -} - -impl Default for Configuration { - fn default() -> Self { - Self { - root: ".".into(), - serve: "0.0.0.0:3000".into(), - } - } -} diff --git a/ecdar-web/src/help.txt b/ecdar-web/src/help.txt deleted file mode 100644 index d69ac45..0000000 --- a/ecdar-web/src/help.txt +++ /dev/null @@ -1,10 +0,0 @@ -Usage: ecdar-web [Options] - --r OR --root -- folder being the root folder of the build of Ecdar-GUI-Web. - | Default = . - --s OR --serve -- where and what port to serve the server. - | Default = 0.0.0.0:3000 -- means localhost port 3000. - --h OR --help -- Opens this message - diff --git a/ecdar-web/src/main.rs b/ecdar-web/src/main.rs deleted file mode 100644 index a0352c7..0000000 --- a/ecdar-web/src/main.rs +++ /dev/null @@ -1,26 +0,0 @@ -pub mod configuration; -use configuration::Configuration; - -use axum::{http::*, routing::*, *}; -use std::result::Result; -use tower_http::services::ServeDir; - -ecdar_web_macros::add_endpoint_functions!(); - -#[tokio::main] -async fn main() { - let conf = Configuration::create(); - dbg!(&conf); - - let file_service = get_service(ServeDir::new(conf.root)); - - let app = Router::new().nest_service("/", file_service); - - let app = ecdar_web_macros::add_endpoints!(app); - - println!(r#"sever running on "{}""#, conf.serve); - Server::bind(&conf.serve.parse().unwrap()) - .serve(app.into_make_service()) - .await - .unwrap(); -} diff --git a/src/build.rs b/src/build.rs new file mode 100644 index 0000000..79a2f65 --- /dev/null +++ b/src/build.rs @@ -0,0 +1,5 @@ +fn main() { + tonic_build::compile_protos("Ecdar-ProtoBuf/services.proto").unwrap(); + // Tell cargo to invalidate the crate when the protobuf repository changes + println!("cargo:rerun-if-changed=Ecdar-ProtoBuf"); +} diff --git a/ecdar-web-macros/src/lib.rs b/src/lib.rs similarity index 77% rename from ecdar-web-macros/src/lib.rs rename to src/lib.rs index de0d152..283d61b 100644 --- a/ecdar-web-macros/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,11 @@ +use convert_case::*; use ecdar_protobuf_transpiler::CompileVariables; use proc_macro::TokenStream as TS; use proc_macro2::TokenStream; use quote::quote; -use convert_case::*; #[proc_macro] -pub fn add_endpoints(app_name: TS) -> TS{ +pub fn add_endpoints(app_name: TS) -> TS { let app_name = TokenStream::from(app_name); let endpoints = ecdar_protobuf_transpiler::compile(|var| { @@ -16,24 +16,26 @@ pub fn add_endpoints(app_name: TS) -> TS{ .. } = var; - let route = format!("/{}/{}", - service_name.to_string().to_case(Case::Snake), + let route = format!( + "/{}/{}", + service_name.to_string().to_case(Case::Snake), endpoint_name.to_string().to_case(Case::Snake) ); - quote!{ + quote! { route(#route, post(#fn_name)) } }); - quote!{ + quote! { #app_name.#(#endpoints).* - }.into() + } + .into() } #[proc_macro] -pub fn add_endpoint_functions(_: TS) -> TS{ - let functions = ecdar_protobuf_transpiler::compile(|var|{ +pub fn add_endpoint_functions(_: TS) -> TS { + let functions = ecdar_protobuf_transpiler::compile(|var| { let CompileVariables { in_struct, in_struct_name, @@ -46,12 +48,12 @@ pub fn add_endpoint_functions(_: TS) -> TS{ } = var; let payload_body = if in_struct_has_body { - quote!{ payload.body } - } else { - quote!{()} + quote! { payload.body } + } else { + quote! {()} }; - - quote!{ + + quote! { #in_struct pub async fn #fn_name(Json(payload) : Json<#in_struct_name>) -> Result, StatusCode> { @@ -67,8 +69,8 @@ pub fn add_endpoint_functions(_: TS) -> TS{ } }); - quote!{ + quote! { #(#functions)* - }.into() + } + .into() } - diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..fc4fac0 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,34 @@ +use axum::{routing::*, *}; +use clap::Parser; +use ecdar_web::{add_endpoint_functions, add_endpoints}; +use std::env::current_dir; +use std::path::PathBuf; +use std::result::Result; +use tower_http::services::ServeDir; + +add_endpoint_functions!(); + +#[tokio::main] // TODO: Maybe add some testing eventually +async fn main() -> Result<(), impl std::error::Error> { + let args = Args::parse(); + let file_service = get_service(ServeDir::new(args.root)); + + let app = Router::new().nest_service("/", file_service); + + let app = add_endpoints!(app); + + println!(r#"server running on \"{}\""#, args.serve); + Server::bind(&args.serve.parse().unwrap()) + .serve(app.into_make_service()) + .await +} + +#[derive(clap::Parser, Debug)] +#[command(author, version, about="TODO: Describe", long_about = None)] +pub struct Args { + #[arg(short, long, value_name = "PATH", default_value = current_dir().unwrap().into_os_string())] + root: PathBuf, + + #[arg(short, long, value_name = "IP:PORT", default_value_t = String::from("0.0.0.0:3000"))] + serve: String, +}