diff --git a/backend/src/handlers.rs b/backend/src/handlers.rs index c2a2eb3..c8a2e64 100644 --- a/backend/src/handlers.rs +++ b/backend/src/handlers.rs @@ -1,9 +1,15 @@ use crate::{ws, Clients, Result}; use warp::Reply; -pub async fn ws_handler(ws: warp::ws::Ws, clients: Clients) -> Result -{ +pub async fn ws_handler(ws: warp::ws::Ws, clients: Clients) -> Result { println!("ws_handler"); //debug Ok(ws.on_upgrade(move |socket| ws::client_connection(socket, clients))) -} \ No newline at end of file +} + +pub async fn metrics_handler(clients: Clients) -> Result { + let metricsString = "cosmic_kube_clients{type=\"connected\"} ".to_string() + + &clients.lock().await.len().to_string() + + "\n"; + Ok(metricsString) +} diff --git a/backend/src/main.rs b/backend/src/main.rs index f983b50..5033bfa 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -18,22 +18,23 @@ type Result = std::result::Result; #[tokio::main] async fn main() { - //initialise a hashmap to store currently connected clients. We may want some more logic here if we want currently connected clients to be stored somewhere let clients: Clients = Arc::new(Mutex::new(HashMap::new())); - println!("configuring websocket route"); //debug + println!("Configuring websocket route"); //debug let ws_route = warp::path("ws") - .and(warp::ws()) - .and(with_clients(clients.clone())) - .and_then(handlers::ws_handler); + .and(warp::ws()) + .and(with_clients(clients.clone())) + .and_then(handlers::ws_handler) + .or(warp::path("metrics") + .and(with_clients(clients.clone())) + .and_then(handlers::metrics_handler)); let routes = ws_route.with(warp::cors().allow_any_origin()); - println!("starting server"); //debug - warp::serve(routes).run(([0, 0, 0, 0], 8000)).await; //PORT 8000 localhost + println!("Starting server on http://0.0.0.0:8000"); //debug + warp::serve(routes).run(([0, 0, 0, 0], 8000)).await; } fn with_clients(clients: Clients) -> impl Filter + Clone { warp::any().map(move || clients.clone()) } - diff --git a/kube_cache/aiStuff/ai.go b/kube_cache/aiStuff/ai.go index 33fd6be..a3777ff 100644 --- a/kube_cache/aiStuff/ai.go +++ b/kube_cache/aiStuff/ai.go @@ -135,6 +135,11 @@ func (ai *KubeAi) generateKubeRecipe(kubeName1, kubeName2 string) (string, error return string(body), nil } + if len(aiResponse.Choices) == 0 { + log.Printf("The silly server sent %s, this is very bad", body) + return string(body), nil + } + actualLegitMessage := aiResponse.Choices[0].Message.Content var aiResp2 aiResp diff --git a/kube_cache/aiStuff/dalle.go b/kube_cache/aiStuff/dalle.go index 9756ce0..5c0a4c3 100644 --- a/kube_cache/aiStuff/dalle.go +++ b/kube_cache/aiStuff/dalle.go @@ -81,8 +81,8 @@ func (ai *KubeAi) generateDalleForKube(kubeName string) ([]byte, error) { } if len(dalleResp.Data) == 0 { - log.Println("AI is a bitch") - return nil, errors.New("AI is a bitch") + log.Printf("The silly server sent %s, this is very bad", body) + return nil, errors.New("No data in response") } log.Println("Downloading dalle response")