@@ -25,13 +25,14 @@ use crate::event;
2525use crate :: query:: Query ;
2626use crate :: response:: QueryResponse ;
2727use crate :: s3:: S3 ;
28- use crate :: utils:: header_parsing:: collect_labelled_headers;
28+ use crate :: utils:: header_parsing:: { collect_labelled_headers, ParseHeaderError } ;
2929use crate :: utils:: { self , flatten_json_body, merge} ;
3030
3131use self :: error:: { PostError , QueryError } ;
3232
3333const PREFIX_TAGS : & str = "x-p-tag-" ;
3434const PREFIX_META : & str = "x-p-meta-" ;
35+ const STREAM_NAME_HEADER_KEY : & str = "x-p-stream-name" ;
3536const SEPARATOR : char = '^' ;
3637
3738pub async fn query ( _req : HttpRequest , json : web:: Json < Value > ) -> Result < HttpResponse , QueryError > {
@@ -48,12 +49,38 @@ pub async fn query(_req: HttpRequest, json: web::Json<Value>) -> Result<HttpResp
4849 . map_err ( |e| e. into ( ) )
4950}
5051
52+ pub async fn ingest (
53+ req : HttpRequest ,
54+ body : web:: Json < serde_json:: Value > ,
55+ ) -> Result < HttpResponse , PostError > {
56+ if let Some ( ( _, stream_name) ) = req
57+ . headers ( )
58+ . iter ( )
59+ . find ( |& ( key, _) | key == STREAM_NAME_HEADER_KEY )
60+ {
61+ push_logs ( stream_name. to_str ( ) . unwrap ( ) . to_owned ( ) , req, body) . await ?;
62+
63+ Ok ( HttpResponse :: Ok ( ) . finish ( ) )
64+ } else {
65+ Err ( PostError :: Header ( ParseHeaderError :: MissingStreamName ) )
66+ }
67+ }
68+
5169pub async fn post_event (
5270 req : HttpRequest ,
5371 body : web:: Json < serde_json:: Value > ,
5472) -> Result < HttpResponse , PostError > {
5573 let stream_name: String = req. match_info ( ) . get ( "logstream" ) . unwrap ( ) . parse ( ) . unwrap ( ) ;
74+ push_logs ( stream_name, req, body) . await ?;
5675
76+ Ok ( HttpResponse :: Ok ( ) . finish ( ) )
77+ }
78+
79+ async fn push_logs (
80+ stream_name : String ,
81+ req : HttpRequest ,
82+ body : web:: Json < serde_json:: Value > ,
83+ ) -> Result < ( ) , PostError > {
5784 let tags = HashMap :: from ( [ (
5885 "p_tags" . to_string ( ) ,
5986 collect_labelled_headers ( & req, PREFIX_TAGS , SEPARATOR ) ?,
@@ -89,7 +116,7 @@ pub async fn post_event(
89116 event. process ( ) . await ?;
90117 }
91118
92- Ok ( HttpResponse :: Ok ( ) . finish ( ) )
119+ Ok ( ( ) )
93120}
94121
95122pub mod error {
0 commit comments