nautilus_demo/
http_server.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::net::{SocketAddr, TcpListener};
17
18use axum::{Router, routing::get, serve};
19
20fn get_unique_port() -> u16 {
21    // Create a temporary TcpListener to get an available port
22    let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind temporary TcpListener");
23    let port = listener.local_addr().unwrap().port();
24
25    // Close the listener to free up the port
26    drop(listener);
27
28    port
29}
30
31pub async fn start_positive_stream_http_server()
32-> Result<SocketAddr, Box<dyn std::error::Error + Send + Sync>> {
33    let port = get_unique_port();
34    let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{port}"))
35        .await
36        .unwrap();
37    let addr = listener.local_addr().unwrap();
38
39    tokio::spawn(async move {
40        serve(listener, create_positive_stream_router())
41            .await
42            .unwrap();
43    });
44
45    Ok(addr)
46}
47
48fn create_positive_stream_router() -> Router {
49    // Create a counter state that will be shared across requests
50    let counter = std::sync::Arc::new(std::sync::atomic::AtomicI32::new(0));
51
52    // Clone the counter for the handler
53    let counter_clone = counter.clone();
54    let counter_clone_2 = counter;
55
56    Router::new()
57        .route(
58            "/get",
59            get(async move || {
60                // Increment the counter and return the new value
61                let value = counter_clone.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
62                format!("{value}")
63            }),
64        )
65        .route(
66            "/skip",
67            get(async move || {
68                // Increment the counter and return the new value
69                let value = counter_clone_2.fetch_add(5, std::sync::atomic::Ordering::SeqCst);
70                format!("{value}")
71            }),
72        )
73}