Add setup for chat core, add proper logging

pull/5/head
~erin 2021-07-17 14:45:52 -04:00
parent fedc00cd58
commit c70940c649
Signed by: erin
GPG Key ID: DA70E064A8C70F44
6 changed files with 194 additions and 37 deletions

126
Cargo.lock generated
View File

@ -1,5 +1,14 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "async-stream"
version = "0.3.2"
@ -100,6 +109,20 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"libc",
"num-integer",
"num-traits",
"serde",
"time 0.1.43",
"winapi",
]
[[package]]
name = "const_fn"
version = "0.4.8"
@ -119,7 +142,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffdf8865bac3d9a3bde5bde9088ca431b11f5d37c7a578b8086af77248b76627"
dependencies = [
"percent-encoding",
"time",
"time 0.2.27",
"version_check",
]
@ -190,6 +213,19 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "env_logger"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "figment"
version = "0.10.6"
@ -402,6 +438,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6456b8a6c8f33fee7d958fcd1b60d55b11940a79e63ae87013e6d22e26034440"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "0.14.10"
@ -565,6 +607,25 @@ dependencies = [
"winapi",
]
[[package]]
name = "num-integer"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.13.0"
@ -660,10 +721,14 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
name = "pogchat"
version = "0.2.0"
dependencies = [
"chrono",
"env_logger",
"log",
"rocket",
"serde",
"serde_json",
"sha1",
"uuid",
]
[[package]]
@ -784,6 +849,23 @@ dependencies = [
"syn",
]
[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
@ -822,7 +904,7 @@ dependencies = [
"serde",
"state",
"tempfile",
"time",
"time 0.2.27",
"tokio",
"tokio-stream",
"tokio-util",
@ -870,7 +952,7 @@ dependencies = [
"smallvec",
"stable-pattern",
"state",
"time",
"time 0.2.27",
"tokio",
"uncased",
]
@ -1125,6 +1207,25 @@ dependencies = [
"winapi",
]
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]]
name = "time"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "time"
version = "0.2.27"
@ -1306,6 +1407,16 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "uuid"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
dependencies = [
"getrandom",
"serde",
]
[[package]]
name = "version_check"
version = "0.9.3"
@ -1398,6 +1509,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -11,3 +11,7 @@ rocket = "0.5.0-rc.1"
serde = "1.0.126"
serde_json = "1.0"
sha1 = "0.6.0"
uuid = { version = "0.8.1", features = ["serde", "v4"] }
log = "0.4.0"
env_logger = "0.8.4"
chrono = { version = "0.4.11", features = ["serde"] }

View File

@ -1,18 +1,12 @@
extern crate log;
use crate::user::User;
extern crate sha1;
use serde::{Deserialize, Serialize};
use serde_json::Result;
use std::fs::{File, OpenOptions};
use std::io::prelude::*;
use std::io::{self, BufRead};
use std::path::Path;
// Struct to store basic user data
#[derive(Clone, Serialize, Deserialize)]
struct User {
name: String,
pin_hashed: String,
}
#[get("/")]
pub fn index() -> &'static str {
"API Info:
@ -46,8 +40,7 @@ fn read_json() -> Vec<User> {
if let Ok(lines) = read_lines(&path) {
for line in lines {
if let Ok(user) = line {
println!("{} contains:\n{}", display, &user);
info!("read {} from json file {}", display, &user);
// Parse line from file into a data structure
let user: User = serde_json::from_str(&user).unwrap();
users.push(user);
@ -78,12 +71,12 @@ fn append_json(users_list: &Vec<User>) -> Result<()> {
// Write to the file
match file.write_all(users_json.as_bytes()) {
Err(why) => panic!("couldn't write to {}: {}", display, why),
Ok(_) => println!("succesfully wrote to {}", display),
Ok(_) => info!("succesfully wrote to {}", display),
};
// Add newline
match file.write_all("\n".as_bytes()) {
Err(why) => panic!("couldn't write to {}: {}", display, why),
Ok(_) => println!("succesfully wrote to {}", display),
Ok(_) => info!("succesfully wrote to {}", display),
};
Ok(())
}
@ -114,7 +107,7 @@ fn write_json(users_list: &Vec<User>) -> Result<()> {
// Write to the file
match file.write_all(users_json.as_bytes()) {
Err(why) => panic!("couldn't write to {}: {}", display, why),
Ok(_) => println!("succesfully wrote to {}", display),
Ok(_) => info!("succesfully wrote to {}", display),
};
Ok(())
}
@ -125,12 +118,20 @@ pub fn register_user(name: &str, pin: i32) -> String {
let mut users: Vec<User> = read_json(); // Create an array of users out of parsed json
for i in &users { // loop through elements of the vector
if i.name == name.to_lowercase() {
return "false".to_string();
warn!("Cannot create user {}! User is already in system.", i.name);
return "User already exists!".to_string();
};
};
let pin_hashed = sha1::Sha1::from(&pin.to_string()).digest().to_string();
users.push(User { name: name.to_string().to_lowercase(), pin_hashed: pin_hashed});
append_json(&users);
let pin_hashed = sha1::Sha1::from(&pin.to_string()).digest().to_string(); // hash the pin
users.push( User { name: name.to_string().to_lowercase(), pin_hashed: pin_hashed}); // append the user to the vec
// append to the json file
match append_json(&users) {
Err(why) => panic!("couldn't append json: {}", why),
Ok(()) => info!("Succesfully appended to json"),
};
info!("succesfully created user {} with pin hash {}", users[users.len()-1].name.to_string(), users[users.len()-1].pin_hashed);
return format!("User {} registered with pin hash: {}", users[users.len()-1].name.to_string().to_lowercase(), users[users.len()-1].pin_hashed);
}
@ -142,12 +143,15 @@ pub fn check_pin(name: &str, pin: i32) -> String {
for i in &users { // loop through the vector
if i.name == name.to_lowercase() {
if i.pin_hashed == hashed_pin_input {
return "true".to_string();
info!("pin correct for user {}", i.name);
return "pin matches".to_string();
} else {
warn!("pin incorrect for user {}", i.name);
return "Incorrect pin".to_string();
};
};
};
warn!("cannot check pin for user {} as they do not exist", name.to_string().to_lowercase());
return format!("User {} does not exist.", name.to_string().to_lowercase());
}
@ -165,35 +169,38 @@ pub fn change(name: &str, pin: i32, new_name: &str, new_pin: i32) -> String {
// Check wether to change name or name+pin
if users[i].name == new_name.to_lowercase() {
// check if new name already exists
for n in &users {
if n.name == new_name.to_lowercase() {
return format!("New name {} is already taken!", new_name.to_lowercase());
} else {
users[i].pin_hashed = sha1::Sha1::from(&new_pin.to_string()).digest().to_string();
write_json(&users);
return format!("User {}'s new pin hash is {}.", name.to_string().to_lowercase(), users[i].pin_hashed);
users[i].pin_hashed = sha1::Sha1::from(&new_pin.to_string()).digest().to_string();
match write_json(&users) {
Err(why) => panic!("Cannot write to json! {}", why),
Ok(()) => info!("succesfully wrote to json file"),
}
}
info!("Changed pin of {}", name.to_string().to_lowercase());
return format!("User {}'s new pin hash is {}.", name.to_string().to_lowercase(), users[i].pin_hashed);
} else {
// check if new name already exists
for n in &users {
if n.name == new_name.to_lowercase() {
warn!("Could not change name of {} to {}, as new name is already taken.", name.to_lowercase(), new_name.to_lowercase());
return format!("New name {} is already taken!", new_name.to_lowercase());
} else {
users[i].name = new_name.to_string().to_lowercase();
users[i].pin_hashed = sha1::Sha1::from(&new_pin.to_string()).digest().to_string();
write_json(&users);
return format!("User previously known as {} is now {}. Pin hash, if different, is {}", name.to_string(), users[i].name.to_string(), users[i].pin_hashed.to_string());
}
}
users[i].name = new_name.to_string().to_lowercase();
users[i].pin_hashed = sha1::Sha1::from(&new_pin.to_string()).digest().to_string();
match write_json(&users) {
Err(why) => panic!("couldn't write to json file! {}", why),
Ok(()) => info!("succesfully wrote to json file"),
}
info!("Changed name of {} to {}. New pin hash is {}", name.to_string(), users[i].name.to_string(), users[i].pin_hashed.to_string());
return format!("User previously known as {} is now {}. Pin hash, if different, is {}", name.to_string(), users[i].name.to_string(), users[i].pin_hashed.to_string());
}
} else {
warn!("Incorrect pin given for user {}!", name.to_string());
return format!("Incorrect pin for user {}!", name.to_string());
}
}
}
warn!("User {} not found, could not change pin and/or name.", name.to_string());
return format!("User {} not found.", name.to_string());
}

View File

@ -1,15 +1,22 @@
#[macro_use]
extern crate log;
#[macro_use] extern crate rocket;
use rocket::fairing::AdHoc;
mod auth;
use auth::*;
mod user;
mod message;
#[launch]
fn rocket() -> _ {
env_logger::init();
info!("Started up rocket");
let cors_fairing = AdHoc::on_response("CORS", |_, res| {
Box::pin(async move {
res.set_raw_header("Access-Control-Allow-Origin", "*");
})
});
info!("Built CORS fairing");
rocket::build()
.mount(

11
src/message.rs Normal file
View File

@ -0,0 +1,11 @@
use uuid::Uuid;
use chrono::prelude::*;
use crate::user::User;
#[derive(Clone)]
pub struct Message {
pub id: Uuid,
pub user: User,
pub body: String,
pub created_at: DateTime<Utc>,
}

8
src/user.rs Normal file
View File

@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};
// Struct to store basic user data
#[derive(Clone, Serialize, Deserialize)]
pub struct User {
pub name: String,
pub pin_hashed: String,
}