2023-04-25 05:44:08 +00:00
|
|
|
use log::{info, warn, error};
|
|
|
|
|
use env_logger::Env;
|
2023-04-27 01:46:34 -04:00
|
|
|
use actix_web::{web, get, post, web::Json, App, HttpResponse, HttpServer, Responder};
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
2023-04-27 17:34:08 +00:00
|
|
|
mod security;
|
2023-04-25 05:44:08 +00:00
|
|
|
|
2023-04-27 01:46:34 -04:00
|
|
|
static PORT: u16 = 8009;
|
2023-04-25 05:44:08 +00:00
|
|
|
|
2023-04-27 01:46:34 -04:00
|
|
|
|
|
|
|
|
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
|
|
|
|
|
struct Login {
|
|
|
|
|
net_id: String,
|
|
|
|
|
password: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2023-04-25 05:44:08 +00:00
|
|
|
|
|
|
|
|
//init logging system
|
|
|
|
|
let env = Env::default().filter_or("LOG_LEVEL", "info");
|
|
|
|
|
env_logger::init_from_env(env);
|
|
|
|
|
|
2023-04-27 17:34:08 +00:00
|
|
|
let _ = HttpServer::new( || {
|
2023-04-27 01:46:34 -04:00
|
|
|
App::new()
|
|
|
|
|
.service(index)
|
|
|
|
|
.service(login)
|
|
|
|
|
.service(homepage)
|
|
|
|
|
.service(plan_page)
|
|
|
|
|
})
|
2023-04-27 17:34:08 +00:00
|
|
|
.bind(("0.0.0.0", PORT))?
|
2023-04-27 01:46:34 -04:00
|
|
|
.run()
|
|
|
|
|
.await;
|
2023-04-25 05:44:08 +00:00
|
|
|
//Temporary for testing purposes, should write something to make a random salt
|
|
|
|
|
let username = "cmckechn";
|
|
|
|
|
let password = "password";
|
|
|
|
|
|
|
|
|
|
//proof of concept tests, create_user should fail in this instance because user was already
|
|
|
|
|
//created
|
2023-04-27 17:34:08 +00:00
|
|
|
security::authenticate(username, password).unwrap();
|
|
|
|
|
security::create_user("test", "test_create", "test_first", "test_last").unwrap();
|
|
|
|
|
security::authenticate("test", "test_create").unwrap();
|
2023-04-27 01:46:34 -04:00
|
|
|
Ok(())
|
2023-04-25 05:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
2023-04-27 01:46:34 -04:00
|
|
|
#[get("/")]
|
|
|
|
|
async fn index() -> impl Responder {
|
|
|
|
|
HttpResponse::Ok().body("Hello world!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/login")]
|
|
|
|
|
async fn login(json: Json<Login>) -> Result<String, actix_web::Error> {
|
|
|
|
|
Ok(format!("{} {}", json.net_id, json.password))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/{net_id}/home")]
|
|
|
|
|
async fn homepage(path: web::Path<String>) -> impl Responder {
|
|
|
|
|
let net_id = path.into_inner();
|
|
|
|
|
HttpResponse::Ok().body(format!("You have reached the homepage of {} user", net_id) )
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/{net_id}/plans")]
|
|
|
|
|
async fn plan_page(path: web::Path<String>) -> impl Responder {
|
|
|
|
|
let net_id = path.into_inner();
|
|
|
|
|
HttpResponse::Ok().body(format!("You have reached the plan page of {}", net_id))
|
|
|
|
|
}
|