Skip to content

Commit

Permalink
Merge pull request #219 from dcSpark/nico/add-static-page
Browse files Browse the repository at this point in the history
add static server
  • Loading branch information
nicarq authored Jan 26, 2024
2 parents f4e0b1b + 63bf05d commit 10cb5ee
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 1 deletion.
4 changes: 4 additions & 0 deletions scripts/run_node1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export UNSTRUCTURED_SERVER_URL="https://internal.shinkai.com/x-unstructured-api/
export TELEMETRY_ENDPOINT="https://apm-node-b1.shinkai.com/api/default"
export TELEMETRY_AUTH_HEADER="Basic xxx"

export STATIC_SERVER_PORT="9554"
export STATIC_SERVER_IP="0.0.0.0"
export STATIC_SERVER_FOLDER="./static_server_example"

export INITIAL_AGENT_NAMES="my_gpt,my_gpt_vision"
export INITIAL_AGENT_URLS="https://api.openai.com,https://api.openai.com"
export INITIAL_AGENT_MODELS="openai:gpt-4-1106-preview,openai:gpt-4-vision-preview"
Expand Down
4 changes: 4 additions & 0 deletions scripts/run_node1_shinkai_backend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export NO_SECRET_FILE="true"
export EMBEDDINGS_SERVER_URL="https://internal.shinkai.com/x-embed-api/"
export UNSTRUCTURED_SERVER_URL="https://internal.shinkai.com/x-unstructured-api/"

export STATIC_SERVER_PORT="9554"
export STATIC_SERVER_IP="0.0.0.0"
export STATIC_SERVER_FOLDER="./static_server_example"

export INITIAL_AGENT_NAMES="premium_gpt,standard_mixtral,gpt_vision"
export INITIAL_AGENT_URLS="https://dev-backend-hosting.shinkai.com,https://dev-backend-hosting.shinkai.com,https://dev-backend-hosting.shinkai.com"
export INITIAL_AGENT_MODELS="shinkai-backend:PREMIUM_TEXT_INFERENCE,shinkai-backend:STANDARD_TEXT_INFERENCE,shinkai-backend:PREMIUM_VISION_INFERENCE"
Expand Down
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ use shinkai_message_primitives::shinkai_utils::signatures::{
};
use shinkai_vector_resources::embedding_generator::RemoteEmbeddingGenerator;
use shinkai_vector_resources::unstructured::unstructured_api::UnstructuredAPI;
use utils::static_server::start_static_server;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use std::{env, fs};
use tokio::runtime::Runtime;
use tokio::sync::Mutex;
use utils::environment::NodeEnvironment;
use utils::environment::{fetch_static_server_env, NodeEnvironment};
use utils::open_telemetry::init_telemetry_tracing;

mod agent;
Expand Down Expand Up @@ -209,6 +210,17 @@ async fn main() {
);
}

// Fetch static server environment variables
if let Some(static_server_env) = fetch_static_server_env() {
// Start the static server if the environment variables are set
let _static_server = start_static_server(
static_server_env.ip,
static_server_env.port,
static_server_env.folder_path,
)
.await;
}

// Setup API Server task
let api_listen_address = node_env.clone().api_listen_address;
let api_server = tokio::spawn(async move {
Expand Down
30 changes: 30 additions & 0 deletions src/utils/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ pub struct NodeEnvironment {
pub embeddings_server_api_key: Option<String>,
}

#[derive(Debug, Clone)]
pub struct StaticServerEnvironment {
pub ip: IpAddr,
pub port: u16,
pub folder_path: String,
}

pub fn fetch_agent_env(global_identity: String) -> Vec<SerializedAgent> {
let initial_agent_names: Vec<String> = env::var("INITIAL_AGENT_NAMES")
.unwrap_or_else(|_| "".to_string())
Expand Down Expand Up @@ -168,3 +175,26 @@ pub fn fetch_node_environment() -> NodeEnvironment {
embeddings_server_api_key,
}
}

pub fn fetch_static_server_env() -> Option<StaticServerEnvironment> {
let port = env::var("STATIC_SERVER_PORT")
.ok()
.and_then(|p| p.parse::<u16>().ok());
let ip = env::var("STATIC_SERVER_IP")
.ok()
.and_then(|ip| ip.parse::<IpAddr>().ok());
let folder_path = env::var("STATIC_SERVER_FOLDER").ok();

eprintln!("Static server env: {:?} {:?} {:?}", ip, port, folder_path);

match (ip, port, folder_path) {
(Some(ip), Some(port), Some(folder_path)) => {
Some(StaticServerEnvironment {
ip,
port,
folder_path,
})
},
_ => None,
}
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ pub mod logging_helpers;
pub mod printer;
pub mod qr_code_setup;
pub mod update_global_identity;
pub mod static_server;
#[cfg(feature = "telemetry")]
pub mod open_telemetry;
13 changes: 13 additions & 0 deletions src/utils/static_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::net::IpAddr;

use shinkai_message_primitives::shinkai_utils::shinkai_logging::{shinkai_log, ShinkaiLogLevel, ShinkaiLogOption};

pub async fn start_static_server(ip: IpAddr, port: u16, folder_path: String) -> tokio::task::JoinHandle<()> {
shinkai_log(
ShinkaiLogOption::Node,
ShinkaiLogLevel::Info,
format!("Starting static server on {}:{}", ip, port).as_str(),
);
let warp_server = warp::serve(warp::fs::dir(folder_path)).run((ip, port));
tokio::spawn(warp_server)
}
12 changes: 12 additions & 0 deletions static_server_example/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}

h1 {
color: #333;
}

p {
color: #666;
}
11 changes: 11 additions & 0 deletions static_server_example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<h1>Welcome to my web page!</h1>
<p>This is a simple web page served by a Rust server using Warp.</p>
</body>
</html>

0 comments on commit 10cb5ee

Please sign in to comment.