Initialize repo with basic functionality
commit
8af82c0b8c
|
@ -0,0 +1,3 @@
|
|||
/target
|
||||
.vscode
|
||||
users.json
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "registration"
|
||||
version = "0.1.0"
|
||||
authors = ["Erin Nova <erin@the-system.eu.org>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rocket = "0.5.0-rc.1"
|
||||
serde = "1.0.126"
|
||||
serde_json = "1.0"
|
|
@ -0,0 +1,8 @@
|
|||
# Registration api system
|
||||
`POST /api/register/<name>/<pin>` Register the username with the pin provided if it doesn't already exist
|
||||
|
||||
`GET /api/users/<name>` Check if the user exists
|
||||
|
||||
`GET /api/users/<name>/<pin>` Check if the user exists, and if the pin provided matches
|
||||
|
||||
`POST /api/register/<name>/<pin>/<new-name>/<new-pin>` Change a users pin/name
|
|
@ -0,0 +1,184 @@
|
|||
#[macro_use] extern crate rocket;
|
||||
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: i32,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> &'static str {
|
||||
"API Info:
|
||||
|
||||
`POST /api/register/<name>/<pin>` Register the username with the pin provided if it doesn't already exist
|
||||
|
||||
`GET /api/users/<name>` Check if the user exists
|
||||
|
||||
`GET /api/users/<name>/<pin>` Check if the user exists, and if the pin provided matches
|
||||
|
||||
`POST /api/register/<name>/<pin>/<new-pin>` Change a users pin (<new-pin> is an int)
|
||||
|
||||
`POST /api/register/<name>/<pin>/<new-name>` Change a users name (<new-pin> is an str)"
|
||||
}
|
||||
|
||||
// The output is wrapped in a Result to allow matching on errors
|
||||
// Returns an Iterator to the Reader of the lines of the file.
|
||||
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where P: AsRef<Path>, {
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
||||
|
||||
// Function to read json from file into the vector
|
||||
fn read_json() -> Vec<User> {
|
||||
// Create path to file
|
||||
let path = Path::new("users.json");
|
||||
let display = path.display();
|
||||
|
||||
let mut users: Vec<User> = Vec::new(); // Create an empty vector of users
|
||||
|
||||
// Read through the lines and append them to the array
|
||||
if let Ok(lines) = read_lines(&path) {
|
||||
for line in lines {
|
||||
if let Ok(user) = line {
|
||||
println!("{} contains:\n{}", display, &user);
|
||||
|
||||
// Parse line from file into a data structure
|
||||
let user: User = serde_json::from_str(&user).unwrap();
|
||||
users.push(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
return users;
|
||||
}
|
||||
|
||||
// Function to append the last value of the users vector to the file
|
||||
fn append_json(users_list: &Vec<User>) -> Result<()> {
|
||||
// Create a file to write to
|
||||
let path = Path::new("users.json");
|
||||
let display = path.display();
|
||||
|
||||
let mut file = match OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(&path){
|
||||
Err(why) => panic!("couldn't create {}: {}", display, why),
|
||||
Ok(file) => file,
|
||||
};
|
||||
|
||||
// Serialize the last user value
|
||||
let users_json = serde_json::to_string(&users_list[users_list.len()-1])?;
|
||||
|
||||
// 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),
|
||||
};
|
||||
// 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(())
|
||||
}
|
||||
|
||||
// Function to write whole vector of users to file
|
||||
fn write_json(users_list: &Vec<User>) -> Result<()> {
|
||||
// Create a file to write to
|
||||
let path = Path::new("users.json");
|
||||
let display = path.display();
|
||||
|
||||
let mut file = match OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(&path){
|
||||
Err(why) => panic!("couldn't create {}: {}", display, why),
|
||||
Ok(file) => file,
|
||||
};
|
||||
|
||||
let mut users_json = String::new();
|
||||
for i in 0..users_list.len() {
|
||||
// Serialize the users
|
||||
users_json += &serde_json::to_string(&users_list[i])?;
|
||||
if i <= users_list.len()-2 { // don't append newline if it's the last element
|
||||
users_json += "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// 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(())
|
||||
}
|
||||
|
||||
// Post request to register a user and pin
|
||||
#[post("/api/register/<name>/<pin>")]
|
||||
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 {
|
||||
return "false".to_string();
|
||||
};
|
||||
};
|
||||
users.push(User { name: name.to_string(), pin: pin});
|
||||
append_json(&users);
|
||||
return format!("User {} registered with pin {}", users[users.len()-1].name.to_string(), users[users.len()-1].pin);
|
||||
}
|
||||
|
||||
// Check if pin matches user
|
||||
#[get("/api/users/<name>/<pin>")]
|
||||
fn check_pin(name: &str, pin: i32) -> String {
|
||||
let users: Vec<User> = read_json();
|
||||
for i in &users { // loop through the vector
|
||||
if i.name == name {
|
||||
if i.pin == pin {
|
||||
return "true".to_string();
|
||||
} else {
|
||||
return "Incorrect pin".to_string();
|
||||
};
|
||||
};
|
||||
};
|
||||
return format!("User {} does not exist.", name.to_string());
|
||||
}
|
||||
|
||||
// Change a users pin
|
||||
#[post("/api/users/change/<name>/<pin>/<new_name>/<new_pin>")]
|
||||
fn change(name: &str, pin: i32, new_name: &str, new_pin: i32) -> String {
|
||||
let mut users: Vec<User> = read_json();
|
||||
|
||||
// Loop over elements in vector
|
||||
for i in 0..users.len() {
|
||||
if users[i].name == name { // make sure name exists
|
||||
if users[i].pin == pin { // check if pin is correct
|
||||
users[i].name = new_name.to_string();
|
||||
users[i].pin = new_pin;
|
||||
write_json(&users);
|
||||
return format!("User previously known as {} is now called {}. New pin is {}.", name.to_string(), users[i].name.to_string(), users[i].pin);
|
||||
} else {
|
||||
return format!("Incorrect pin for user {}!", name.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return format!("User {} not found.", name.to_string());
|
||||
}
|
||||
|
||||
#[get("/api/users/<name>")]
|
||||
fn get_user(name: &str) -> String {
|
||||
return format!("User {}", name);
|
||||
}
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
rocket::build().mount("/", routes![index,get_user,register_user, check_pin, change])
|
||||
}
|
Loading…
Reference in New Issue