diff --git a/src/main.rs b/src/main.rs index 87e9c1c..57e64a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,9 +17,11 @@ use std::error::Error; use std::env; use std::sync::{Arc, Mutex}; +use actix::ActorContext; use actix_web::{ - fs, middleware, server, App, HttpMessage, HttpRequest, HttpResponse + fs, ws, middleware, server, App, HttpMessage, HttpRequest, HttpResponse }; + use git2::Repository; struct WebHookConfig { @@ -43,8 +45,9 @@ fn redeploy_repo(url: &str, path: &Path, name: &str) -> Result<(), git2::Error> } } }; - repo.set_head("FETCH_HEAD")?; + repo.find_remote("origin")?.fetch(&["master"], None, None)?; + repo.set_head("FETCH_HEAD")?; repo.checkout_head(Some(git2::build::CheckoutBuilder::new().force().use_theirs(true)))?; Ok(()) @@ -162,6 +165,81 @@ fn webhook_handler((contents, req): (String, HttpRequest>>; + + /// Method is called on actor start. We start the heartbeat process here. + fn started(&mut self, ctx: &mut Self::Context) { + //self.hb(ctx); + } +} + +/// Handler for `ws::Message` +impl actix::prelude::StreamHandler for MyWebSocket { + fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { + // process websocket messages + println!("WS: {:?}", msg); + match msg { + ws::Message::Ping(msg) => { +// self.hb = Instant::now(); + ctx.pong(&msg); + } + ws::Message::Pong(_) => { +// self.hb = Instant::now(); + } + ws::Message::Text(text) => ctx.text(text), + ws::Message::Binary(bin) => ctx.binary(bin), + ws::Message::Close(_) => { + ctx.stop(); + } + } + } +} + +impl MyWebSocket { + fn new() -> Self { + Self { /*hb: Instant::now()*/ } + } + +/* + /// helper method that sends ping to client every second. + /// + /// also this method checks heartbeats from client + fn hb(&self, ctx: &mut ::Context) { + ctx.run_interval(HEARTBEAT_INTERVAL, |act, ctx| { + // check client heartbeats + if Instant::now().duration_since(act.hb) > CLIENT_TIMEOUT { + // heartbeat timed out + println!("Websocket Client heartbeat failed, disconnecting!"); + + // stop actor + ctx.stop(); + + // don't try to send a ping + return; + } + + ctx.ping(""); + }); + } + */ +} + +/// do websocket handshake and start `MyWebSocket` actor +fn ws_index(r: &HttpRequest>>) -> Result { + //ws::start(r, MyWebSocket::new()) + Ok(HttpResponse::Ok().body("go ahead")) +} + fn main() { env::set_var("RUST_LOG", "actix_web=debug"); env::set_var("RUST_BACKTRACE", "1"); @@ -185,6 +263,9 @@ fn main() { // enable logger .middleware(middleware::Logger::default()) + // websocket route + .resource("/socket.io/", |r| r.method(actix_web::http::Method::GET).f(ws_index)) + // Our webhook .resource("/webhook", |r| r.method(actix_web::http::Method::POST)