initial http server
This commit is contained in:
commit
f6817bcd33
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/target
|
||||||
|
**/*.rs.bk
|
4
Cargo.lock
generated
Normal file
4
Cargo.lock
generated
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
[[package]]
|
||||||
|
name = "httpserve"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "httpserve"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["AJ ONeal <coolaj86@gmail.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
11
public/index.html
Normal file
11
public/index.html
Normal file
@ -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>
|
27
src/main.rs
Normal file
27
src/main.rs
Normal file
@ -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…
x
Reference in New Issue
Block a user