-
-
Notifications
You must be signed in to change notification settings - Fork 253
/
lib.rs
66 lines (55 loc) · 1.46 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::thread;
mod process;
mod request;
mod router;
mod server;
mod types;
use server::Server;
use std::io::prelude::*;
use std::net::TcpStream;
use std::time::Duration;
// pyO3 module
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use std::future::Future;
#[pyfunction]
pub fn start_server() {
let _listener = Server::new();
}
#[pymodule]
pub fn robyn(py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(start_server))?;
m.add_class::<Server>()?;
pyo3_asyncio::try_init(py).unwrap();
pyo3::prepare_freethreaded_python();
Ok(())
}
// let mut contents = String::new();
pub fn handle_connection<'a, F>(
mut stream: TcpStream,
runtime: tokio::runtime::Runtime,
contents: &'a mut String,
test: &dyn Fn(&'a mut String, String, String, TcpStream) -> F,
) where
F: Future<Output = ()> + 'a,
{
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();
let get = b"GET / HTTP/1.1\r\n";
let sleep = b"GET /sleep HTTP/1.1\r\n";
let (status_line, filename) = if buffer.starts_with(get) {
("HTTP/1.1 200 OK", "hello.html")
} else if buffer.starts_with(sleep) {
thread::sleep(Duration::from_secs(5));
("HTTP/1.1 200 OK", "hello.html")
} else {
("HTTP/1.1 404 NOT FOUND", "404.html")
};
let future = test(
contents,
String::from(filename),
String::from(status_line),
stream,
);
runtime.block_on(future);
}