actix: get git working and do port

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2019-01-11 08:50:40 +08:00
parent 881599d031
commit 4940f2d601
1 changed files with 83 additions and 2 deletions

View File

@ -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<Arc<Mutex<WebHookConfig
HttpResponse::Ok().body("Ok") // <- send response
}
/// websocket connection is long running connection, it easier
/// to handle with an actor
struct MyWebSocket {
// Client must send ping at least once per 10 seconds (CLIENT_TIMEOUT),
// otherwise we drop connection.
//hb: Instant,
}
impl actix::prelude::Actor for MyWebSocket {
type Context = ws::WebsocketContext<Self, Arc<Mutex<WebHookConfig>>>;
/// 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<ws::Message, ws::ProtocolError> 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 <Self as actix::prelude::Actor>::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<Arc<Mutex<WebHookConfig>>>) -> Result<HttpResponse, actix_web::Error> {
//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)