@@ -63,6 +63,8 @@ struct HttpgConfig {
6363 index_sql : String ,
6464 #[ conf( long, env) ]
6565 login_query : String ,
66+ #[ conf( long, env) ]
67+ listen : bool ,
6668 #[ conf( long, env, default_value="3000" ) ]
6769 port : u16 ,
6870 #[ conf( flatten) ]
@@ -73,13 +75,18 @@ struct HttpgConfig {
7375 pg : PostgresConfig ,
7476}
7577
78+ #[ derive( Clone ) ]
79+ struct Listen {
80+ tx : Sender < Notification > ,
81+ client : Arc < Client > ,
82+ }
83+
7684#[ derive( Clone ) ]
7785struct AppState {
7886 read_pool : Pool ,
7987 write_pool : Pool ,
8088 config : HttpgConfig ,
81- tx : Sender < Notification > ,
82- client : Arc < Client > ,
89+ listen : Option < Listen > ,
8390}
8491
8592#[ tokio:: main]
@@ -97,40 +104,42 @@ async fn main() -> Result<(), HttpgError> {
97104 let read_pool = httpg_config. pg . read_pool ( ) ?;
98105 let write_pool = httpg_config. pg . write_pool ( ) ?;
99106
100- let ( client, mut conn) = httpg_config. pg . connect ( ) . await ?;
101- let ( tx, _rx) = tokio:: sync:: broadcast:: channel :: < Notification > ( 16 ) ;
102- let mut stream = futures:: stream:: poll_fn ( move |cx| conn. poll_message ( cx) ) ;
103- let wrapped_tx = tx. clone ( ) ;
104- tokio:: spawn ( async move {
105- while let Some ( Ok ( m) ) = stream. next ( ) . await {
106- match m {
107- tokio_postgres:: AsyncMessage :: Notice ( n) => {
108- tracing:: info!( "{n:#?}" ) ;
109- } ,
110- tokio_postgres:: AsyncMessage :: Notification ( n) => {
111- wrapped_tx. send ( n) . map_err ( Box :: new) ?;
107+ let listen = if httpg_config. listen {
108+ let ( client, mut conn) = httpg_config. pg . connect ( ) . await ?;
109+ let ( tx, _rx) = tokio:: sync:: broadcast:: channel :: < Notification > ( 16 ) ;
110+ let mut stream = futures:: stream:: poll_fn ( move |cx| conn. poll_message ( cx) ) ;
111+ let wrapped_tx = tx. clone ( ) ;
112+ tokio:: spawn ( async move {
113+ while let Some ( Ok ( m) ) = stream. next ( ) . await {
114+ match m {
115+ tokio_postgres:: AsyncMessage :: Notice ( n) => {
116+ tracing:: info!( "{n:#?}" ) ;
117+ } ,
118+ tokio_postgres:: AsyncMessage :: Notification ( n) => {
119+ wrapped_tx. send ( n) . map_err ( Box :: new) ?;
120+ }
121+ _ => { }
112122 }
113- _ => { }
114123 }
115- }
116- Ok :: < _ , HttpgError > ( ( ) )
117- } ) ;
124+ Ok :: < _ , HttpgError > ( ( ) )
125+ } ) ;
126+ Some ( Listen { tx, client : Arc :: new ( client) } )
127+ } else {
128+ None
129+ } ;
118130
119131 let state = AppState {
120132 read_pool,
121133 write_pool,
122134 config : httpg_config. to_owned ( ) ,
123- tx,
124- client : Arc :: new ( client) ,
135+ listen,
125136 } ;
126137
127- let app = Router :: new ( )
138+ let mut app = Router :: new ( )
128139 . route ( "/" , get ( index) )
129140 . route ( "/{path}/" , get ( index) )
130141 . route ( "/logout" , get ( logout) . post ( logout) )
131142 . route ( "/{path}/logout" , get ( logout) . post ( logout) )
132- . route ( "/sse/{channel}" , get ( sse_query) )
133- . route ( "/{path}/sse/{channel}" , get ( sse_query) )
134143 . route ( "/query" , get ( stream_query) . post ( post_query) )
135144 . route ( "/{path}/query" , get ( stream_query) . post ( post_query) )
136145 . route ( "/{path}/query/{cursor}" , post ( post_query) )
@@ -142,6 +151,14 @@ async fn main() -> Result<(), HttpgError> {
142151 . route ( "/{path}/webpush" , get ( web_push) . post ( web_push) )
143152 . route ( "/login" , get ( login) . post ( login) )
144153 . route ( "/{path}/login" , get ( login) . post ( login) )
154+ ;
155+ if httpg_config. listen {
156+ app = app
157+ . route ( "/listen/{channel}" , get ( listen_query) )
158+ . route ( "/{path}/listen/{channel}" , get ( listen_query) )
159+ ;
160+ }
161+ let app = app
145162 . fallback_service ( ServeDir :: new ( httpg_config. public_dir ) )
146163 . with_state ( state. to_owned ( ) )
147164 . layer ( ServiceBuilder :: new ( )
@@ -156,42 +173,6 @@ async fn main() -> Result<(), HttpgError> {
156173 )
157174 ;
158175
159- // tokio::spawn(async move {
160- // let (client, mut conn) = cfg.connect().await?;
161-
162- // let mut stream = futures::stream::poll_fn(move |cx| conn.poll_message(cx));
163-
164- // let state = axum::extract::State(state);
165-
166- // client.simple_query("listen web_push").await?;
167- // // client.simple_query("listen job").await?;
168-
169- // while let Some(Ok(m)) = stream.next().await {
170- // match m {
171- // tokio_postgres::AsyncMessage::Notice(n) => tracing::info!("{n:#?}"),
172- // tokio_postgres::AsyncMessage::Notification(n) => {
173- // match n.channel() {
174- // "web_push" => {
175- // let res = web_push(
176- // state.to_owned(),
177- // None,
178- // Query::default()
179- // )
180- // .await?
181- // .into_response()
182- // ;
183- // dbg!(&res);
184- // }
185- // _ => todo!("{n:#?}")
186- // }
187- // },
188- // _ => todo!(),
189- // }
190- // }
191-
192- // Ok::<(), HttpgError>(())
193- // });
194-
195176 let addr = SocketAddr :: from ( (
196177 [ 0 , 0 , 0 , 0 ] ,
197178 httpg_config. port ,
@@ -538,11 +519,12 @@ async fn stream_query(
538519}
539520
540521#[ debug_handler]
541- async fn sse_query (
542- State ( AppState { tx , client , ..} ) : State < AppState > ,
522+ async fn listen_query (
523+ State ( AppState { listen , ..} ) : State < AppState > ,
543524 Path ( channel) : Path < String > ,
544525) -> Result < impl IntoResponse , HttpgError > {
545526
527+ let Listen { tx, client } = listen. ok_or ( HttpgError :: anyhow ( "no listen" ) ) ?;
546528 client. execute ( & format ! ( "listen {channel}" ) , & [ ] ) . await ?;
547529
548530 Ok ( Sse :: new (
0 commit comments