xref: /xiu/application/http-server/src/main.rs (revision 06685550)
1 use axum::{
2     routing::{get, post},
3     Router,
4 };
5 
6 use std::net::SocketAddr;
7 use std::env;
8 
9 #[tokio::main]
main()10 async fn main() {
11     env::set_var("RUST_LOG", "info");
12     env_logger::init();
13     let app = Router::new()
14         .route("/", get(root))
15         .route("/on_publish", post(on_publish))
16         .route("/on_unpublish", post(on_unpublish))
17         .route("/on_play", post(on_play))
18         .route("/on_stop", post(on_stop));
19 
20     let addr = SocketAddr::from(([127, 0, 0, 1], 3001));
21     log::info!("http server listen on: {}", 3001);
22     axum::Server::bind(&addr)
23         .serve(app.into_make_service())
24         .await
25         .unwrap();
26 }
27 
28 // basic handler that responds with a static string
root() -> &'static str29 async fn root() -> &'static str {
30     "Hello, World!"
31 }
32 
on_publish(body: String)33 async fn on_publish(body: String) {
34     log::info!("on_publish body: {}", body);
35 }
36 
on_unpublish(body: String)37 async fn on_unpublish(body: String) {
38     log::info!("on_unpublish body: {}", body);
39 }
40 
on_play(body: String)41 async fn on_play(body: String) {
42     log::info!("on_play body: {}", body);
43 }
44 
on_stop(body: String)45 async fn on_stop(body: String) {
46     log::info!("on_stop body: {}", body);
47 }
48