Skip to content

Commit

Permalink
SW5 (#2)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
t-lohse authored Mar 13, 2024
1 parent 9b6bbc4 commit 74851fa
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 140 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/build_artifacts.yml
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions .github/workflows/check_format.yml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

.idea
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "Ecdar-ProtoBuf"]
path = Ecdar-ProtoBuf
url = https://github.com/Ecdar/Ecdar-ProtoBuf
branch = SW5
36 changes: 30 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"

1 change: 1 addition & 0 deletions Ecdar-ProtoBuf
Submodule Ecdar-ProtoBuf added at 053d7e
16 changes: 0 additions & 16 deletions ecdar-web-macros/Cargo.toml

This file was deleted.

15 changes: 0 additions & 15 deletions ecdar-web/Cargo.toml

This file was deleted.

50 changes: 0 additions & 50 deletions ecdar-web/src/configuration.rs

This file was deleted.

10 changes: 0 additions & 10 deletions ecdar-web/src/help.txt

This file was deleted.

26 changes: 0 additions & 26 deletions ecdar-web/src/main.rs

This file was deleted.

5 changes: 5 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
36 changes: 19 additions & 17 deletions ecdar-web-macros/src/lib.rs → src/lib.rs
Original file line number Diff line number Diff line change
@@ -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| {
Expand All @@ -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,
Expand All @@ -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<Json<#rtn_struct>, StatusCode> {
Expand All @@ -67,8 +69,8 @@ pub fn add_endpoint_functions(_: TS) -> TS{
}
});

quote!{
quote! {
#(#functions)*
}.into()
}
.into()
}

34 changes: 34 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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!();

Check failure on line 9 in src/main.rs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

proc macro panicked

#[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);

Check failure on line 18 in src/main.rs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

proc macro panicked

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,
}

0 comments on commit 74851fa

Please sign in to comment.