+ // Read the first 16 characters from the incoming stream
+ let mut buffer = [0; 16];
+ assert!(stream.read(&mut buffer).await.unwrap() >= 16);
+ // First 4 characters are used to detect HTTP method
+ let method_type = match str::from_utf8(&buffer[0..4]) {
+ Ok(v) => v,
+ Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
+ };
+
+ let contents = match method_type {
+ "GET " => {
+ // todo: return real balance
+ format!("{{\"balance\": {}}}", 0.0)
+ }
+ "POST" => {
+ // Take characters after 'POST /' until whitespace is detected.
+ let input: String = buffer[6..16]
+ .iter()
+ .take_while(|x| **x != 32u8)
+ .map(|x| *x as char)
+ .collect();
+ let balance_update = input.parse::<f32>().unwrap();
+ // todo: add balance update handling
+ format!("{{\"balance\": {}}}", balance_update)
+ }
+ _ => {
+ panic!("Invalid HTTP method!")
+ }
+ };