Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
feat: add a crit log_check to autopush_rs
Browse files Browse the repository at this point in the history
Closes #1167
  • Loading branch information
pjenvey committed Apr 13, 2018
1 parent acb26ea commit 6a15e11
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
2 changes: 2 additions & 0 deletions autopush_rs/src/server/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct Dispatch {
pub enum RequestType {
Websocket,
Status,
LogCheck,
}

impl Dispatch {
Expand Down Expand Up @@ -75,6 +76,7 @@ impl Future for Dispatch {
} else {
match req.path {
Some(ref path) if path.starts_with("/status") => RequestType::Status,
Some(ref path) if path.starts_with("/v1/err/crit") => RequestType::LogCheck,
_ => {
debug!("unknown http request {:?}", req);
return Err("unknown http request".into());
Expand Down
67 changes: 51 additions & 16 deletions autopush_rs/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ use cadence::StatsdClient;
use futures::sync::oneshot;
use futures::task;
use futures::{Stream, Future, Sink, Async, Poll, AsyncSink, StartSend};
use hyper;
use hyper::header;
use hyper::server::Http;
use hyper::{self, header, StatusCode};
use libc::c_char;
use openssl::ssl::SslAcceptor;
use reqwest;
Expand Down Expand Up @@ -405,6 +404,7 @@ impl Server {
let client = request.and_then(move |(socket, request)| -> MyFuture<_> {
match request {
RequestType::Status => write_status(socket),
RequestType::LogCheck => write_log_check(socket),
RequestType::Websocket => {
// Perform the websocket handshake on each
// connection, but don't let it take too long.
Expand Down Expand Up @@ -927,21 +927,56 @@ where
}

fn write_status(socket: WebpushIo) -> MyFuture<()> {
let data = json!({
"status": "OK",
"version": env!("CARGO_PKG_VERSION"),
}).to_string();
let data = format!("\
HTTP/1.1 200 Ok\r\n\
Server: webpush\r\n\
Date: {date}\r\n\
Content-Length: {len}\r\n\
\r\n\
{data}\
",
write_json(
socket,
StatusCode::Ok,
json!({
"status": "OK",
"version": env!("CARGO_PKG_VERSION"),
}),
)
}

fn write_log_check(socket: WebpushIo) -> MyFuture<()> {
let status = StatusCode::ImATeapot;
let code: u16 = status.into();

error!("Test Critical Message";
"status_code" => code,
"errno" => 0,
);
thread::spawn(|| {
panic!("LogCheck");
});

write_json(
socket,
StatusCode::ImATeapot,
json!({
"code": code,
"errno": 999,
"error": "Test Failure",
"mesage": "FAILURE:Success",
}),
)
}

fn write_json(socket: WebpushIo, status: StatusCode, body: serde_json::Value) -> MyFuture<()> {
let body = body.to_string();
let data = format!(
"\
HTTP/1.1 {status}\r\n\
Server: webpush\r\n\
Date: {date}\r\n\
Content-Length: {len}\r\n\
Content-Type: application/json\r\n\
\r\n\
{body}\
",
status = status,
date = time::at(time::get_time()).rfc822(),
len = data.len(),
data = data,
len = body.len(),
body = body,
);
Box::new(
tokio_io::io::write_all(socket, data.into_bytes())
Expand Down

0 comments on commit 6a15e11

Please sign in to comment.