account finished, logging added
This commit is contained in:
parent
ea16071903
commit
9c059055b6
13
Cargo.toml
13
Cargo.toml
|
|
@ -6,3 +6,16 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.86"
|
||||
axum = "0.7.5"
|
||||
axum_session = "0.14.0"
|
||||
axum_session_auth = "0.14.0"
|
||||
axum_session_mongo = "0.1.0"
|
||||
candle-nn = "0.5.1"
|
||||
chrono = "0.4.38"
|
||||
http = "1.1.0"
|
||||
mongodb = { version = "2.8.2", features = ["bson-chrono-0_4", "tokio-runtime"]}
|
||||
rlg = "0.0.4"
|
||||
serde = "1.0.203"
|
||||
tokio = "1.38.0"
|
||||
vrd = "0.0.7"
|
||||
|
|
|
|||
|
|
@ -1,26 +1,32 @@
|
|||
pub mod db;
|
||||
|
||||
use axum::{
|
||||
extract::Path,
|
||||
routing::{get, post},
|
||||
body::Body,
|
||||
extract::Path,
|
||||
Json,
|
||||
Router
|
||||
response::Response,
|
||||
Router,
|
||||
routing::{get, post}
|
||||
};
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_mongo::SessionMongoPool;
|
||||
use chrono::Utc;
|
||||
use crate::AppError;
|
||||
use db::{get_users, User};
|
||||
use http::StatusCode;
|
||||
use mongodb::{
|
||||
bson::{doc, oid::ObjectId, to_document},
|
||||
Client
|
||||
};
|
||||
use rlg::macro_info_log;
|
||||
|
||||
type AuthenticationSession = AuthSession<User, ObjectId, SessionMongoPool, Client>;
|
||||
type ApiResult = Result<Response, AppError>;
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.nest("/:user_id", user_router())
|
||||
.route("/",
|
||||
.route("/sign-up",
|
||||
post(post_sign_up)
|
||||
)
|
||||
}
|
||||
|
|
@ -41,65 +47,109 @@ fn user_router() -> Router {
|
|||
)
|
||||
}
|
||||
|
||||
pub async fn get_sign_in(Path(user_id): Path<ObjectId>) -> Result<String, AppError> {
|
||||
pub async fn get_sign_in(Path(user_id): Path<ObjectId>) -> ApiResult {
|
||||
let id = format!("user_id: {}", user_id);
|
||||
macro_info_log!(&Utc::now().to_string(), &id, "Salt request initiated");
|
||||
|
||||
let db = get_users().await?;
|
||||
let query = doc! { "_id" : user_id};
|
||||
|
||||
match db.find_one(query, None).await? {
|
||||
Some(user) => {
|
||||
//TODO: Return User salt
|
||||
Ok(String::new())
|
||||
macro_info_log!(&Utc::now().to_string(), &id, "Salt request successful");
|
||||
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::CREATED)
|
||||
.body(Body::from(user.auth.salt.unwrap()))?)
|
||||
},
|
||||
None => {
|
||||
//TODO: Return User does not exist
|
||||
Ok(String::new())
|
||||
macro_info_log!(&Utc::now().to_string(), &id, "Salt Request unsuccessful: username does not exist");
|
||||
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(Body::from("User does not exist"))?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn post_sign_in(Path(user_id): Path<ObjectId>, auth: AuthenticationSession, body: String) -> Result<String, AppError> {
|
||||
pub async fn post_sign_in(Path(user_id): Path<ObjectId>, auth: AuthenticationSession, body: String) -> ApiResult {
|
||||
let id = format!("user_id: {}", user_id);
|
||||
macro_info_log!(&Utc::now().to_string(), &id, "Sign-in request initiated");
|
||||
|
||||
let db = get_users().await?;
|
||||
let query = doc! { "_id": &user_id, "_auth._hash": body };
|
||||
|
||||
match db.find_one(query, None).await? {
|
||||
Some(_user) => {
|
||||
macro_info_log!(&Utc::now().to_string(), &id, "Sign-in request successful");
|
||||
|
||||
auth.login_user(user_id);
|
||||
auth.remember_user(true);
|
||||
//TODO: Return API Key or Auth Key and User ID
|
||||
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body(Body::from(format!("{}", user_id)))?)
|
||||
},
|
||||
None => {
|
||||
//TODO: Return or Redirect Unauthorized
|
||||
},
|
||||
};
|
||||
macro_info_log!(&Utc::now().to_string(), &id, "Sign-in request unsuccessful: incorrect credentials");
|
||||
|
||||
Ok(String::new())
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::UNAUTHORIZED)
|
||||
.body(Body::from("Username or password is incorrect"))?)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn post_sign_up(Json(body): Json<User>) -> Result<(), AppError> {
|
||||
pub async fn post_sign_up(Json(body): Json<User>) -> ApiResult {
|
||||
let id = "guest_user";
|
||||
macro_info_log!(&Utc::now().to_string(), &id, "Sign-up request initiated");
|
||||
|
||||
let db = get_users().await?;
|
||||
let query = to_document(&body)?;
|
||||
|
||||
match db.find_one(query, None).await? {
|
||||
match db.find_one(query.clone(), None).await? {
|
||||
Some(_user) => {
|
||||
//TODO: Return or Redirect User Exists
|
||||
macro_info_log!(&Utc::now().to_string(), &id, "Sign-up request unsuccessful: username already exists");
|
||||
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::NOT_ACCEPTABLE)
|
||||
.body(Body::from("Username is already taken"))?)
|
||||
},
|
||||
None => {
|
||||
//TODO: Reutrn UsedId
|
||||
macro_info_log!(&Utc::now().to_string(), &id, "Sign-up request successful");
|
||||
|
||||
db.insert_one(body, None).await?;
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::CREATED)
|
||||
.body(Body::from("Account created successfully"))?)
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
pub async fn post_sign_out(auth: AuthenticationSession) {
|
||||
pub async fn post_sign_out(Path(user_id): Path<ObjectId>, auth: AuthenticationSession) -> ApiResult {
|
||||
let id = format!("user_id: {}", user_id);
|
||||
macro_info_log!(&Utc::now().to_string(), &id, "Sign-out request initiated");
|
||||
|
||||
match auth.is_authenticated() {
|
||||
true => auth.logout_user(),
|
||||
true => {
|
||||
macro_info_log!(&Utc::now().to_string(), &id, "Sign-out request successful");
|
||||
|
||||
auth.logout_user();
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body(Body::from("Sign out successful"))?)
|
||||
},
|
||||
false => {
|
||||
//TODO: Redirect Not Logged in
|
||||
macro_info_log!(&Utc::now().to_string(), &id, "Sign-out request unsuccessful: user was not logged in");
|
||||
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::NOT_ACCEPTABLE)
|
||||
.body(Body::from("No user is not logged in"))?)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn post_backup() {} //TODO: Backup
|
||||
|
||||
pub async fn get_restore() {} //TODO: Restore
|
||||
pub async fn get_restore() {} //TODO: restore
|
||||
|
|
|
|||
15
src/lib.rs
15
src/lib.rs
|
|
@ -9,29 +9,36 @@ use axum_session_auth::{AuthConfig, AuthSessionLayer};
|
|||
use axum_session_mongo::SessionMongoPool;
|
||||
use http::StatusCode;
|
||||
use mongodb::{bson::oid::ObjectId, Client};
|
||||
use rlg::{config::Config, macro_info_log};
|
||||
|
||||
pub async fn run() -> Result<()> {
|
||||
init_logging();
|
||||
macro_info_log!(&chrono::Utc::now().to_string(), "server", "Server Initializing...");
|
||||
let db = get_db_client().await?;
|
||||
let session_store = session(db.clone()).await?;
|
||||
let auth_config = AuthConfig::<ObjectId>::default();
|
||||
|
||||
macro_info_log!(&chrono::Utc::now().to_string(), "server", "Routes Initializing...");
|
||||
|
||||
let app = router()
|
||||
.layer(SessionLayer::new(session_store))
|
||||
.layer(AuthSessionLayer::<User, ObjectId, SessionMongoPool, Client>
|
||||
::new(Some(db)).with_config(auth_config)
|
||||
);
|
||||
macro_info_log!(&chrono::Utc::now().to_string(), "server", "Routes Initialized");
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||
|
||||
axum::serve(listener, app).await?;
|
||||
|
||||
macro_info_log!(&chrono::Utc::now().to_string(), "server", "Server Initialized");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn router() -> axum::Router {
|
||||
axum::Router::new()
|
||||
.nest("/account", account::router())
|
||||
.nest("/predict", model::router())
|
||||
//.nest("/predict", model::router())
|
||||
}
|
||||
|
||||
async fn session(db: Client) -> Result<SessionStore<SessionMongoPool>> {
|
||||
|
|
@ -62,3 +69,9 @@ where
|
|||
Self(err.into())
|
||||
}
|
||||
}
|
||||
|
||||
fn init_logging() {
|
||||
std::env::set_var("LOG_FILE_PATH", "$HOME/.dermy/server.log");
|
||||
|
||||
let config = Config::load();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue