1use std::fmt::Display;
2use std::{fmt::Debug, str::FromStr};
3use std::sync::Arc;
4use std::collections::HashMap;
5use std::time::Instant;
6use in_memory_db::KeyValueStore;
7use tokio::net::TcpListener;
8
9use crate::connection::handle_connection;
10
11pub mod in_memory_db;
12pub mod connection;
13
14pub async fn start_server<T>(store: KeyValueStore<T>) -> Result<(), Box<dyn std::error::Error>>
15where
16 T: Send + Sync + Clone + FromStr + Debug + Display + 'static,
17 T::Err: std::fmt::Debug,
18{
19 let listener = TcpListener::bind("127.0.0.1:4000").await?;
20 println!("Server listening on port 4000");
21
22 loop {
23 let (socket, _) = listener.accept().await?;
24 let store = Arc::clone(&store);
25
26 tokio::spawn(async move {
27 if let Err(e) = handle_connection(socket, store).await {
28 println!("Failed to handle connection: {:?}", e);
29 }
30 });
31 }
32}