Skip to content

Commit

Permalink
Merge branch 'dev' into kill_process
Browse files Browse the repository at this point in the history
  • Loading branch information
isaidsari authored May 20, 2024
2 parents 1f460ae + 66db1dd commit 3148786
Show file tree
Hide file tree
Showing 27 changed files with 930 additions and 111 deletions.
14 changes: 14 additions & 0 deletions .github/build-and-test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ runs:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: 📦 Install protoc
shell: bash
run: |
if [ "${{ runner.os }}" = "Linux" ]; then
sudo apt-get install -y protobuf-compiler
elif [ "${{ runner.os }}" = "macOS" ]; then
brew install protobuf
elif [ "${{ runner.os }}" = "Windows" ]; then
choco install protoc
else
echo "Unsupported OS"
exit 1
fi
- name: 🔨 Build
uses: actions-rs/cargo@v1
Expand Down
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

## [Unreleased] - 19-04-2024

### Added

- v0.1.1 CHANGELOG.md added
- v0.1.1 versioning started
- v0.1.1 gRPC added for other services to notify their status
- v0.1.1 proto file(s) introduced for gRPC. its a simple for now but will be extended
- v0.1.1 build.rs added to compile proto files

### Changed

- v0.1.1 tokio crate updated to latest version
- v0.1.1 Duration::seconds() is now deprecated, changed to try_seconds()

## [0.1.0]

### Added

- v0.1.0 initial

### Fixed

### Changed

### Removed
14 changes: 11 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "remon-server"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
base32 = "0.4.0"
base64 = "0.21.5"
chrono = "0.4.31"
chrono = "0.4.35"
dotenv = "0.15.0"
fast_qr = "0.11.0"
hyper = { version = "0.14.27", features = ["full"] }
Expand All @@ -21,7 +21,6 @@ serde_json = "1.0.107"
thiserror = "1.0.50"
sqlx = { version = "0.7.2", features = ["sqlite", "runtime-tokio"] }
sysinfo = "0.30.3"
tokio = { version = "1.33.0", features = ["full"] }
totp-rs = "5.4.0"
env_logger = "0.10.0"
log = "0.4.14"
Expand All @@ -31,6 +30,15 @@ maplit = "1.0.2"
lazy_static = "1.4.0"
async_once = "0.2.6"
fcm = { git = "https://github.com/rj76/fcm-rust.git", branch = "main" }
strum = "0.26.2"
strum_macros = "0.26.2"
tokio = { version = "1.36.0", features = ["full"] }
tonic = "0.11.0"
tonic-reflection = "0.11.0"
prost = "0.12.3"

[build-dependencies]
tonic-build = "0.11.0"

[dev-dependencies]
ctor = "0.2.6"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The mobile app provides a user-friendly interface for configuring what events tr
to run on linux, you should have the following packages installed
1. pkg-config: `sudo apt install pkg-config`
2. libssl-dev: `sudo apt install libssl-dev`
3. proto-compiler: `apt-get install protobuf-compiler`

## Setup
1. create a copy of the `.env.example` file in the root directory of the project, and name it `.env`
Expand Down
12 changes: 12 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::{env, error::Error, path::PathBuf};

fn main() -> Result<(), Box<dyn Error>> {
let out_dir = PathBuf::from(env::var("OUT_DIR")?);

tonic_build::configure()
.protoc_arg("--experimental_allow_proto3_optional")
.file_descriptor_set_path(out_dir.join("remonproto_descriptor.bin"))
.compile(&["./proto/notification.proto"], &["proto"])?;

Ok(())
}
29 changes: 29 additions & 0 deletions proto/notification.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
syntax = "proto3";

package remonproto;

service NotificationService {
rpc SendNotification(NotificationRequest) returns (NotificationResponse) {}
rpc Log(LogRequest) returns (LogResponse) {}
}

message NotificationRequest {
string title = 1;
string body = 2;
}

message NotificationResponse {
bool success = 1;
optional string message = 2;
}

message LogRequest {
string level = 1;
string target = 2;
string message = 3;
}

message LogResponse {
bool success = 1;
optional string message = 2;
}
5 changes: 5 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ pub mod validate_token_test;
pub mod get_processes;
pub mod kill_process;

pub mod logs {
pub mod get_app_ids;
pub mod get_app_logs;
}

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
Expand Down
6 changes: 6 additions & 0 deletions src/api/get_processes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use hyper::{Body, Request, Response};
use log::debug;
use serde::Serialize;
use std::convert::Infallible;

use crate::{api::authenticate, monitor::get_process_list};

#[derive(Serialize)]
struct ResponseData {
processes: Vec<ProcessInfo>,

Check failure on line 10 in src/api/get_processes.rs

View workflow job for this annotation

GitHub Actions / Build and test on Ubuntu

cannot find type `ProcessInfo` in this scope

Check failure on line 10 in src/api/get_processes.rs

View workflow job for this annotation

GitHub Actions / Build and test on Ubuntu

cannot find type `ProcessInfo` in this scope

Check failure on line 10 in src/api/get_processes.rs

View workflow job for this annotation

GitHub Actions / Build and test on Windows

cannot find type `ProcessInfo` in this scope

Check failure on line 10 in src/api/get_processes.rs

View workflow job for this annotation

GitHub Actions / Build and test on macOS

cannot find type `ProcessInfo` in this scope
}

pub async fn get_processes(req: Request<Body>) -> Result<Response<Body>, Infallible> {
match authenticate(&req) {
Ok(val) => val,
Expand Down
108 changes: 108 additions & 0 deletions src/api/logs/get_app_ids.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use hyper::{Body, Request, Response};
use serde_derive::Serialize;
use std::{collections::HashMap, convert::Infallible};

use crate::{
api::{authenticate, ResponseBody},
logs::{self, models::get_app_ids::GetAppIdsRequest},
};

#[derive(Serialize)]
struct GepAppIdsResponse {
ids: Vec<String>,
}

pub async fn get_app_ids(req: Request<Body>) -> Result<Response<Body>, Infallible> {
match authenticate(&req) {
Ok(val) => val,
Err(err) => {
return Ok(err);
}
};

let req = match req.uri().query() {
None => GetAppIdsRequest {
start_time: None,
end_time: None,
},
Some(query_str) => {
let query_params: Vec<&str> = query_str.split("&").collect();

// convert query params to GetAppIdsRequest
// first, split '=' from query params
// and get them as key-value pair (HashMap)
let spl: HashMap<String, String> = query_params
.iter()
.map(|x| x.split("=").collect::<Vec<&str>>())
.map(|x| (x[0].to_string(), x[1].to_string()))
.collect();

// then, convert HashMap to GetAppIdsRequest
let req = GetAppIdsRequest {
start_time: match spl.get("start_time") {
Some(val) => {
let par = val.parse::<i64>();

match par {
Ok(val) => Some(val),
Err(_) => None,
}
}
None => None,
},
end_time: match spl.get("end_time") {
Some(val) => {
let par = val.parse::<i64>();

match par {
Ok(val) => Some(val),
Err(_) => None,
}
}
None => None,
},
};

req
}
};

let start_time = req.start_time;
let end_time = req.end_time;

let err_res_builder = |err: String| {
Response::builder()
.status(hyper::StatusCode::BAD_REQUEST)
.header("Content-Type", "application/json")
.body(Body::from(
serde_json::to_string(&ResponseBody::Error(err)).unwrap(),
))
.unwrap()
};

let ids = match logs::persistence::get_app_ids(start_time, end_time).await {
Ok(val) => val,
Err(err) => {
let bod = serde_json::to_string(&ResponseBody::Error(err.to_string())).unwrap();

let response = err_res_builder(bod);

return Ok(response);
}
};

let res_model = GepAppIdsResponse { ids };

let res_json = serde_json::to_string(&res_model);

let response = match res_json {
Ok(res_json) => Response::builder()
.status(hyper::StatusCode::OK)
.header("Content-Type", "application/json")
.body(Body::from(res_json))
.unwrap(),
Err(err) => err_res_builder(err.to_string()),
};

Ok(response)
}
Loading

0 comments on commit 3148786

Please sign in to comment.