45 lines
1.3 KiB
Rust
45 lines
1.3 KiB
Rust
use std::fs;
|
|
use std::io::prelude::*;
|
|
use std::net::TcpListener;
|
|
use std::net::TcpStream;
|
|
|
|
use async_std::task;
|
|
use std::time::Duration;
|
|
|
|
// TODO: must be set in a conf file
|
|
const SERVER_URL: &str = "127.0.0.1:9000";
|
|
|
|
// switch to an asynchronous main function
|
|
#[async_std::main]
|
|
async fn main() {
|
|
let listener = TcpListener::bind(SERVER_URL).unwrap();
|
|
println!("server is listening at {}", SERVER_URL);
|
|
for stream in listener.incoming() {
|
|
let stream = stream.unwrap();
|
|
handle_connection(stream).await;
|
|
}
|
|
}
|
|
|
|
// TODO: must properly handle the request
|
|
async fn handle_connection(mut stream: TcpStream) {
|
|
let mut buffer = [0; 1024];
|
|
stream.read(&mut buffer).unwrap();
|
|
|
|
let get = b"GET / HTTP/1.1\r\n";
|
|
|
|
// TODO: request musy be parsed correctly
|
|
let status_line = if buffer.starts_with(get) {
|
|
"HTTP/1.1 200 OK\r\n"
|
|
} else {
|
|
"HTTP/1.1 404 NOT FOUND\r\n"
|
|
};
|
|
|
|
let content_type = "Content-Type: application/json\r\n\r\n";
|
|
let json = r#"{"access_token":"AAAAAAAAAAAA.BBBBBBBBBB.CCCCCCCCCC","refresh_token": "DDDDDDDDDDD.EEEEEEEEEEE.FFFFF"}"#;
|
|
let response = format!("{status_line}{content_type}{json}\n");
|
|
|
|
println!("return status code : {}", status_line);
|
|
stream.write(response.as_bytes()).unwrap();
|
|
stream.flush().unwrap();
|
|
}
|