Tests pass
This commit is contained in:
119
httpret/src/channelarchiver.rs
Normal file
119
httpret/src/channelarchiver.rs
Normal file
@@ -0,0 +1,119 @@
|
||||
use crate::response;
|
||||
use err::Error;
|
||||
use http::{header, Method, Request, Response, StatusCode};
|
||||
use hyper::Body;
|
||||
use netpod::{log::*, NodeConfigCached, APP_JSON_LINES};
|
||||
|
||||
pub struct ListIndexFilesHttpFunction {}
|
||||
|
||||
impl ListIndexFilesHttpFunction {
|
||||
pub fn prefix() -> &'static str {
|
||||
"/api/4/channelarchiver/list/indexfiles"
|
||||
}
|
||||
|
||||
pub fn name() -> &'static str {
|
||||
"ListIndexFilesHttpFunction"
|
||||
}
|
||||
|
||||
pub fn should_handle(path: &str) -> Option<Self> {
|
||||
if path.starts_with(Self::prefix()) {
|
||||
Some(Self {})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle(&self, req: Request<Body>, node_config: &NodeConfigCached) -> Result<Response<Body>, Error> {
|
||||
if req.method() != Method::GET {
|
||||
return Ok(response(StatusCode::NOT_ACCEPTABLE).body(Body::empty())?);
|
||||
}
|
||||
info!("{} handle uri: {:?}", Self::name(), req.uri());
|
||||
let conf = node_config
|
||||
.node
|
||||
.channel_archiver
|
||||
.as_ref()
|
||||
.ok_or(Error::with_msg_no_trace(
|
||||
"this node is not configured as channel archiver",
|
||||
))?;
|
||||
let s = archapp_wrap::archapp::archeng::list_index_files(conf);
|
||||
let s = futures_util::stream::unfold(s, |mut st| async move {
|
||||
use futures_util::StreamExt;
|
||||
let x = st.next().await;
|
||||
match x {
|
||||
Some(x) => match x {
|
||||
Ok(x) => {
|
||||
let mut x = serde_json::to_vec(&x).unwrap();
|
||||
x.push(b'\n');
|
||||
Some((Ok::<_, Error>(x), st))
|
||||
}
|
||||
Err(e) => {
|
||||
error!("{:?}", e);
|
||||
None
|
||||
}
|
||||
},
|
||||
None => None,
|
||||
}
|
||||
});
|
||||
Ok(response(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, APP_JSON_LINES)
|
||||
.body(Body::wrap_stream(s))?)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ListChannelsHttpFunction {}
|
||||
|
||||
impl ListChannelsHttpFunction {
|
||||
pub fn prefix() -> &'static str {
|
||||
"/api/4/channelarchiver/list/channels"
|
||||
}
|
||||
|
||||
pub fn name() -> &'static str {
|
||||
"ListChannelsHttpFunction"
|
||||
}
|
||||
|
||||
pub fn should_handle(path: &str) -> Option<Self> {
|
||||
if path.starts_with(Self::prefix()) {
|
||||
Some(Self {})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle(&self, req: Request<Body>, node_config: &NodeConfigCached) -> Result<Response<Body>, Error> {
|
||||
if req.method() != Method::GET {
|
||||
return Ok(response(StatusCode::NOT_ACCEPTABLE).body(Body::empty())?);
|
||||
}
|
||||
info!("{} handle uri: {:?}", Self::name(), req.uri());
|
||||
let conf = node_config
|
||||
.node
|
||||
.channel_archiver
|
||||
.as_ref()
|
||||
.ok_or(Error::with_msg_no_trace(
|
||||
"this node is not configured as channel archiver",
|
||||
))?;
|
||||
|
||||
let s = archapp_wrap::archapp::archeng::list_all_channels(conf);
|
||||
let s = futures_util::stream::unfold(s, |mut st| async move {
|
||||
use futures_util::StreamExt;
|
||||
let x = st.next().await;
|
||||
match x {
|
||||
Some(x) => match x {
|
||||
Ok(x) => {
|
||||
let mut x = serde_json::to_vec(&x).unwrap();
|
||||
x.push(b'\n');
|
||||
Some((Ok::<_, Error>(x), st))
|
||||
}
|
||||
Err(e) => {
|
||||
//Some((Err(e), st))
|
||||
error!("{:?}", e);
|
||||
None
|
||||
}
|
||||
},
|
||||
None => None,
|
||||
}
|
||||
});
|
||||
Ok(response(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, APP_JSON_LINES)
|
||||
.body(Body::wrap_stream(s))?)
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ use tracing::Instrument;
|
||||
use url::Url;
|
||||
|
||||
pub mod api1;
|
||||
pub mod channelarchiver;
|
||||
pub mod gather;
|
||||
pub mod proxy;
|
||||
pub mod pulsemap;
|
||||
@@ -273,6 +274,10 @@ async fn http_service_try(req: Request<Body>, node_config: &NodeConfigCached) ->
|
||||
pulsemap::MapPulseHistoHttpFunction::handle(req, &node_config).await
|
||||
} else if pulsemap::MapPulseHttpFunction::path_matches(path) {
|
||||
pulsemap::MapPulseHttpFunction::handle(req, &node_config).await
|
||||
} else if let Some(h) = channelarchiver::ListIndexFilesHttpFunction::should_handle(path) {
|
||||
h.handle(req, &node_config).await
|
||||
} else if let Some(h) = channelarchiver::ListChannelsHttpFunction::should_handle(path) {
|
||||
h.handle(req, &node_config).await
|
||||
} else if path.starts_with("/api/1/requestStatus/") {
|
||||
info!("{}", path);
|
||||
Ok(response(StatusCode::OK).body(Body::from("{}"))?)
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
use crate::response;
|
||||
use err::Error;
|
||||
use http::header;
|
||||
use hyper::{Body, Request, Response, StatusCode};
|
||||
use netpod::log::*;
|
||||
use netpod::{log::*, APP_JSON};
|
||||
use netpod::{ChannelSearchQuery, NodeConfigCached};
|
||||
use url::Url;
|
||||
|
||||
pub async fn channel_search(req: Request<Body>, node_config: &NodeConfigCached) -> Result<Response<Body>, Error> {
|
||||
let (head, _body) = req.into_parts();
|
||||
match head.headers.get("accept") {
|
||||
Some(v) if v == "application/json" => {
|
||||
match head.headers.get(header::ACCEPT) {
|
||||
Some(v) if v == APP_JSON => {
|
||||
let s1 = format!("dummy:{}", head.uri);
|
||||
info!("try to parse {:?}", s1);
|
||||
let url = Url::parse(&s1)?;
|
||||
|
||||
Reference in New Issue
Block a user