Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
name = "omnect-ui"
readme = "README.md"
repository = "[email protected]:omnect/omnect-ui.git"
version = "0.9.2"
version = "0.9.3"
build = "src/build.rs"

[dependencies]
Expand Down
3 changes: 2 additions & 1 deletion build-and-run-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ docker build \
docker run --rm \
-v $(pwd)/temp:/cert \
-v /tmp:/socket \
-v $(pwd)/temp/data:/data \
-u $(id -u):$(id -g) \
-e RUST_LOG=debug \
-e UI_PORT=1977 \
-e SOCKET_PATH=/socket/api.sock \
-e CENTRIFUGO_ADMIN_ENABLED=true \
-e CENTRIFUGO_ADMIN_PASSWORD=123 \
-e CENTRIFUGO_ADMIN_SECRET=123 \
-e DATA_DIR_PATH=$(pwd)/temp/data \
-e DATA_DIR_PATH=/data \
-p "${omnect_ui_port}":"${omnect_ui_port}" \
-p "${centrifugo_port}":"${centrifugo_port}" \
omnect-ui-x86:"local_${omnect_ui_version}"
28 changes: 25 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::fs::File;
use std::io::Write;
use std::{fs, path::Path};
use tokio::process::Command;
use tokio::signal::unix::{signal, SignalKind};
use uuid::Uuid;

const UPLOAD_LIMIT_BYTES: usize = 250 * 1024 * 1024;
Expand Down Expand Up @@ -185,8 +186,6 @@ async fn main() {
let index_html =
std::fs::canonicalize("static/index.html").expect("static/index.html not found");

send_publish_endpoint(&centrifugo_http_api_key, &ods_socket_path).await;

fs::exists(&ods_socket_path).unwrap_or_else(|_| {
panic!(
"omnect device service socket file {} does not exist",
Expand All @@ -197,8 +196,10 @@ async fn main() {
fs::exists(&update_os_path!())
.unwrap_or_else(|_| panic!("path {} for os update does not exist", &update_os_path!()));

send_publish_endpoint(&centrifugo_http_api_key, &ods_socket_path).await;

let api_config = Api {
ods_socket_path,
ods_socket_path: ods_socket_path.clone(),
update_os_path: update_os_path!(),
centrifugo_client_token_hmac_secret_key,
index_html,
Expand Down Expand Up @@ -280,9 +281,17 @@ async fn main() {

debug!("centrifugo pid: {}", centrifugo.id().unwrap());

let mut sigterm = signal(SignalKind::terminate()).expect("Failed to install SIGTERM handler");

tokio::select! {
_ = tokio::signal::ctrl_c() => {
debug!("ctrl-c");
delete_publish_endpoint(&ods_socket_path).await;
server_handle.stop(true).await;
},
_ = sigterm.recv() => {
debug!("SIGTERM received");
delete_publish_endpoint(&ods_socket_path).await;
server_handle.stop(true).await;
},
_ = server_task => {
Expand Down Expand Up @@ -391,6 +400,19 @@ async fn send_publish_endpoint(
HttpResponse::Ok().finish()
}

async fn delete_publish_endpoint(ods_socket_path: &str) -> impl Responder {
let path = format!(
"/publish-endpoint/v1/{}",
String::from(env!("CARGO_PKG_NAME"))
);
if let Err(e) = socket_client::delete_with_empty_body(&path, ods_socket_path).await {
error!("deleting publish endpoint failed: {e:#}");
HttpResponse::InternalServerError().finish();
}

HttpResponse::Ok().finish()
}

pub fn validate_password(password: &str) -> Result<()> {
if password.is_empty() {
error!("password is empty");
Expand Down
17 changes: 14 additions & 3 deletions src/socket_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn post_with_json_body(
.body(serde_json::to_string(&json).unwrap_or_default())
.context("build request failed")?;

post(request, socket_path).await
send_request(request, socket_path).await
}

pub async fn post_with_empty_body(path: &str, socket_path: &str) -> Result<HttpResponse> {
Expand All @@ -38,10 +38,21 @@ pub async fn post_with_empty_body(path: &str, socket_path: &str) -> Result<HttpR
.body(String::new())
.context("build request failed")?;

post(request, socket_path).await
send_request(request, socket_path).await
}

async fn post(request: Request<String>, socket_path: &str) -> Result<HttpResponse> {
pub async fn delete_with_empty_body(path: &str, socket_path: &str) -> Result<HttpResponse> {
let request = Request::builder()
.uri(path)
.method("DELETE")
.header("Host", "localhost")
.body(String::new())
.context("build request failed")?;

send_request(request, socket_path).await
}

async fn send_request(request: Request<String>, socket_path: &str) -> Result<HttpResponse> {
let mut sender = match sender(socket_path).await {
Err(e) => {
error!("error creating request sender: {e}. socket might be broken. exit application");
Expand Down
Loading