-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from charley04310/feat/api-support-grpc-client
Feat(api): add support grpc client
- Loading branch information
Showing
16 changed files
with
204 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[workspace] | ||
members = [ "src/api","src/vmm", "src/cli"] | ||
members = ["src/api", "src/vmm", "src/cli"] | ||
resolver = "2" |
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,32 @@ | ||
syntax = "proto3"; | ||
|
||
package vmmorchestrator; | ||
|
||
enum Language { | ||
PYTHON = 0; | ||
NODE = 1; | ||
RUST = 2; | ||
} | ||
|
||
enum LogLevel { | ||
DEBUG = 0; | ||
INFO = 1; | ||
WARN = 2; | ||
ERROR = 3; | ||
} | ||
|
||
service VmmService { | ||
rpc Run (RunVmmRequest) returns (RunVmmResponse); | ||
} | ||
|
||
message RunVmmRequest { | ||
|
||
Language language = 1; | ||
string code = 2; | ||
string env = 3; | ||
LogLevel log_level = 4; | ||
|
||
} | ||
|
||
message RunVmmResponse { | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
tonic_build::compile_protos("../../proto/vmm.proto")?; | ||
Ok(()) | ||
} |
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,26 @@ | ||
use tonic::transport::Channel; | ||
use vmmorchestrator::vmm_service_client::VmmServiceClient; | ||
|
||
pub mod vmmorchestrator { | ||
tonic::include_proto!("vmmorchestrator"); | ||
} | ||
|
||
pub struct VmmClient { | ||
client: VmmServiceClient<Channel>, | ||
} | ||
|
||
impl VmmClient { | ||
pub async fn new() -> Result<Self, tonic::transport::Error> { | ||
let client = VmmServiceClient::connect("http://[::1]:50051") | ||
.await | ||
.expect("Failed to connect to VMM service"); | ||
|
||
Ok(VmmClient { client }) | ||
} | ||
|
||
pub async fn run_vmm(&mut self, request: vmmorchestrator::RunVmmRequest) { | ||
let request = tonic::Request::new(request); | ||
let response = self.client.run(request).await.unwrap(); | ||
println!("RESPONSE={:?}", response); | ||
} | ||
} |
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 +1,2 @@ | ||
pub mod client; | ||
pub mod services; |
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,20 +1,13 @@ | ||
use actix_web::{App, HttpServer}; | ||
use api::services::{configuration, logs, metrics, run, shutdown}; | ||
use api::services::{run, shutdown}; | ||
|
||
#[actix_web::main] | ||
async fn main() -> std::io::Result<()> { | ||
let port = 3000; | ||
|
||
println!("Starting server on port: {}", port); | ||
HttpServer::new(|| { | ||
App::new() | ||
.service(configuration) | ||
.service(run) | ||
.service(logs) | ||
.service(metrics) | ||
.service(shutdown) | ||
}) | ||
.bind(("127.0.0.1", port))? | ||
.run() | ||
.await | ||
HttpServer::new(|| App::new().service(run).service(shutdown)) | ||
.bind(("127.0.0.1", port))? | ||
.run() | ||
.await | ||
} |
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 was deleted.
Oops, something went wrong.
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,39 @@ | ||
use crate::utils::ConfigFileHandler; | ||
use reqwest::Client; | ||
use shared_models::{CloudletDtoRequest, YamlClientConfigFile}; | ||
use std::error::Error; | ||
pub struct CloudletClient {} | ||
|
||
impl CloudletClient { | ||
pub fn new_cloudlet_config(config: YamlClientConfigFile) -> CloudletDtoRequest { | ||
let code: String = ConfigFileHandler::read_file(&config.code_path) | ||
.expect("Error while reading the code file"); | ||
let env = ConfigFileHandler::read_file(&config.env_path) | ||
.expect("Error while reading the environment file"); | ||
let language = config.language; | ||
let log_level = config.log_level; | ||
CloudletDtoRequest { | ||
language, | ||
env, | ||
code, | ||
log_level, | ||
} | ||
} | ||
|
||
pub async fn run(request: CloudletDtoRequest) -> Result<(), Box<dyn Error>> { | ||
let client = Client::new(); | ||
let json = serde_json::to_string(&request)?; | ||
println!("REQUEST : {:?}", request); | ||
let res = client | ||
.post("http://127.0.0.1:3000/run") | ||
.header(reqwest::header::CONTENT_TYPE, "application/json") | ||
.body(json) | ||
.send() | ||
.await?; | ||
|
||
match res.status().as_u16() { | ||
200 => Ok(()), | ||
_ => Err("Error while making the request".into()), | ||
} | ||
} | ||
} |
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,20 +1,24 @@ | ||
use crate::types::Config; | ||
use shared_models::YamlClientConfigFile; | ||
use std::fs::File; | ||
use std::io::{self, Read}; | ||
use std::path::PathBuf; | ||
|
||
pub fn load_config(config_path: &PathBuf) -> io::Result<Config> { | ||
let mut file = File::open(config_path)?; | ||
let mut contents = String::new(); | ||
file.read_to_string(&mut contents)?; | ||
let config: Config = serde_yaml::from_str(&contents) | ||
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; | ||
Ok(config) | ||
} | ||
pub struct ConfigFileHandler {} | ||
|
||
impl ConfigFileHandler { | ||
pub fn load_config(config_path: &PathBuf) -> io::Result<YamlClientConfigFile> { | ||
let mut file = File::open(config_path)?; | ||
let mut contents = String::new(); | ||
file.read_to_string(&mut contents)?; | ||
let config: YamlClientConfigFile = serde_yaml::from_str(&contents) | ||
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; | ||
Ok(config) | ||
} | ||
|
||
pub fn read_file(file_path: &str) -> io::Result<String> { | ||
let mut file = File::open(file_path)?; | ||
let mut contents = String::new(); | ||
file.read_to_string(&mut contents)?; | ||
Ok(contents) | ||
pub fn read_file(file_path: &PathBuf) -> io::Result<String> { | ||
let mut file = File::open(file_path)?; | ||
let mut contents = String::new(); | ||
file.read_to_string(&mut contents)?; | ||
Ok(contents) | ||
} | ||
} |
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,16 @@ | ||
[package] | ||
name = "shared_models" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
clap = { version = "4.5.3", features = ["derive"] } | ||
serde = { version = "1.0.197", features = ["derive"] } | ||
serde_yaml = "0.9.34" | ||
serde_json = "1.0.115" | ||
|
||
[lib] | ||
name = "shared_models" | ||
path = "src/lib.rs" |
Oops, something went wrong.