API Key Handling and some more dynamic functions.
This commit is contained in:
parent
7c58f9db51
commit
56cca06199
1
.gitignore
vendored
1
.gitignore
vendored
@ -19,6 +19,7 @@ Cargo.lock
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
keys.txt
|
||||
|
||||
# Added by cargo
|
||||
|
||||
|
||||
@ -4,3 +4,5 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
base64 = "0.22.1"
|
||||
rand = "0.8"
|
||||
|
||||
56
src/main.rs
56
src/main.rs
@ -12,7 +12,7 @@ struct IncomingRequest {
|
||||
|
||||
// Listen on port and return response.
|
||||
fn main() {
|
||||
let run_local = false;
|
||||
let run_local = true;
|
||||
let mut listener = TcpListener::bind("127.0.0.1:6942").unwrap();
|
||||
|
||||
if !run_local {
|
||||
@ -44,15 +44,15 @@ fn handle_connection(mut stream: TcpStream) {
|
||||
let mut response = String::new();
|
||||
// If not a GET request, or missing API key, throw 404.
|
||||
if inc_request.req_type != "GET" || api_key.is_empty() {
|
||||
response = return_404();
|
||||
response = return_response("404");
|
||||
}
|
||||
else {
|
||||
// Check that the API key is in fact fact valuid.
|
||||
// Not really checking right now.
|
||||
response = return_200();
|
||||
let mut keyfile: Vec<String> = fs::read_to_string("./keys.txt").into_iter().collect();
|
||||
keyfile[0].pop().expect("Not a string").to_string();
|
||||
if api_key == keyfile[0] {
|
||||
response = return_response("200");
|
||||
}
|
||||
}
|
||||
// If not present, return 404.
|
||||
// Perhaps move both of those conditions to a new function.
|
||||
stream.write_all(response.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
@ -72,26 +72,28 @@ fn get_api_key(headers: Vec<String>) -> String {
|
||||
api_key
|
||||
}
|
||||
|
||||
// Return a 404.
|
||||
fn return_404() -> String {
|
||||
let status_line = "HTTP/1.1 404 Not Found";
|
||||
let contents = fs::read_to_string("./public/404.json").unwrap();
|
||||
let length = contents.len();
|
||||
#[allow(unused_assignments)]
|
||||
fn return_response(code: &str) -> String {
|
||||
let mut response = String::new();
|
||||
match code {
|
||||
"404" => {
|
||||
let status_line = "HTTP/1.1 404 Not Found";
|
||||
let contents = fs::read_to_string("./public/404.json").unwrap();
|
||||
let length = contents.len();
|
||||
|
||||
let response =
|
||||
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
|
||||
response =
|
||||
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
|
||||
},
|
||||
"200" => {
|
||||
let status_line = "HTTP/1.1 200 OK";
|
||||
let contents = fs::read_to_string("./public/200.json").unwrap();
|
||||
let length = contents.len();
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
// Return a 200 with the json.
|
||||
fn return_200() -> String {
|
||||
let status_line = "HTTP/1.1 200 OK";
|
||||
let contents = fs::read_to_string("./public/200.json").unwrap();
|
||||
let length = contents.len();
|
||||
|
||||
let response =
|
||||
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
|
||||
|
||||
response
|
||||
response =
|
||||
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
|
||||
},
|
||||
&_ => todo!()
|
||||
}
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user