initial http server
This commit is contained in:
commit
f6817bcd33
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
**/*.rs.bk
|
|
@ -0,0 +1,4 @@
|
|||
[[package]]
|
||||
name = "httpserve"
|
||||
version = "0.1.0"
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "httpserve"
|
||||
version = "0.1.0"
|
||||
authors = ["AJ ONeal <coolaj86@gmail.com>"]
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Hello!</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello!</h1>
|
||||
<p>Hi from Rust</p>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,27 @@
|
|||
use std::io::prelude::*;
|
||||
use std::net::TcpStream;
|
||||
use std::net::TcpListener;
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
|
||||
|
||||
for stream in listener.incoming() {
|
||||
let stream = stream.unwrap();
|
||||
|
||||
handle_connection(stream);
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_connection(mut stream: TcpStream) {
|
||||
let mut buffer = [0; 512];
|
||||
|
||||
stream.read(&mut buffer).unwrap();
|
||||
|
||||
println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
|
||||
|
||||
let response = "HTTP/1.1 200 OK\r\n\r\n";
|
||||
|
||||
stream.write(response.as_bytes()).unwrap();
|
||||
stream.flush().unwrap();
|
||||
}
|
Loading…
Reference in New Issue