Merge branch 'main' of gitlab.com:PhilippHofer/rot

This commit is contained in:
Philipp 2023-07-16 21:14:44 +02:00
commit c00e59708b
55 changed files with 73 additions and 5 deletions

View File

@ -4,8 +4,9 @@ version = "0.1.0"
edition = "2021"
[features]
default = ["rowing-tera"]
default = ["rest", "rowing-tera" ]
rowing-tera = ["rocket_dyn_templates", "tera"]
rest = []
[dependencies]
rocket = { version = "0.5.0-rc.3", features = ["secrets"]}

View File

@ -3,6 +3,9 @@ pub mod model;
#[cfg(feature = "rowing-tera")]
pub mod tera;
#[cfg(feature = "rest")]
pub mod rest;
#[cfg(test)]
#[macro_export]
macro_rules! testdb {

View File

@ -1,13 +1,15 @@
use std::str::FromStr;
#[cfg(feature = "rest")]
use rot::rest;
#[cfg(feature = "rowing-tera")]
use rot::tera;
use sqlx::{pool::PoolOptions, sqlite::SqliteConnectOptions, ConnectOptions};
#[macro_use]
extern crate rocket;
#[cfg(feature = "rowing-tera")]
#[launch]
async fn rocket() -> _ {
use sqlx::SqlitePool;
@ -26,5 +28,8 @@ async fn rocket() -> _ {
#[cfg(feature = "rowing-tera")]
let rocket = tera::config(rocket);
#[cfg(feature = "rest")]
let rocket = rest::config(rocket);
rocket
}

59
src/rest/mod.rs Normal file
View File

@ -0,0 +1,59 @@
use rocket::{
form::Form, fs::FileServer, http::CookieJar, post, routes, Build, FromForm, Rocket, State,
};
use serde_json::json;
use sqlx::SqlitePool;
use crate::model::user::{LoginError, User};
#[derive(FromForm)]
struct LoginForm<'r> {
name: &'r str,
password: &'r str,
}
// curl -X POST localhost:8000/api/login -d "name=rower&password=rower"
#[post("/", data = "<login>")]
async fn login(login: Form<LoginForm<'_>>, db: &State<SqlitePool>) -> String {
match User::login(db, login.name, login.password).await {
Ok(user) => serde_json::to_string(&json!({"status": "success", "user": user})).unwrap(),
Err(LoginError::NoPasswordSet(_)) => {
serde_json::to_string(&json!({"status": "set new pw"})).unwrap()
}
Err(_) => serde_json::to_string(&json!({"status": "wrong"})).unwrap(),
}
//let user_json: String = format!("{}", json!(user));
//cookies.add_private(Cookie::new("loggedin_user", user_json));
}
pub fn config(rocket: Rocket<Build>) -> Rocket<Build> {
rocket
.mount("/", FileServer::from("svelte/build").rank(0))
.mount("/api/login", routes![login])
}
//#[cfg(test)]
//mod test {
// use crate::testdb;
//
// use super::start;
// use rocket::http::Status;
// use rocket::local::asynchronous::Client;
// use rocket::uri;
// use sqlx::SqlitePool;
//
// #[sqlx::test]
// fn test_not_logged_in() {
// let pool = testdb!();
//
// let client = Client::tracked(start(pool))
// .await
// .expect("valid rocket instance");
// let response = client.get(uri!(super::index)).dispatch().await;
//
// assert_eq!(response.status(), Status::SeeOther);
// let location = response.headers().get("Location").next().unwrap();
// assert_eq!(location, "/auth");
// }
//}

View File

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

Before

Width:  |  Height:  |  Size: 352 KiB

After

Width:  |  Height:  |  Size: 352 KiB

View File

Before

Width:  |  Height:  |  Size: 113 KiB

After

Width:  |  Height:  |  Size: 113 KiB

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -13,8 +13,8 @@ const config = {
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter({
fallback: 'app.html',
})
}
}),
},
};
export default config;

View File

@ -6,7 +6,7 @@ cd frontend
npm install
npm run build
cd ..
cd rot_app
cd svelte
npm install
npm run build
sudo systemctl restart rot