feat: barebones backend init
This commit is contained in:
parent
cb49ba0e8d
commit
a3d8e07c26
2
backend/.gitignore
vendored
2
backend/.gitignore
vendored
@ -1 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
|
db.sqlite
|
||||||
|
|
||||||
|
1338
backend/Cargo.lock
generated
1338
backend/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -4,5 +4,9 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rocket = "0.5.1"
|
rocket = { version = "0.5.1", features = ["json", "secrets"] }
|
||||||
|
rocket_db_pools = { version = "0.2.0", features = ["sqlx_sqlite"] }
|
||||||
|
serde = { version = "1.0.217", features = ["derive"] }
|
||||||
|
serde_json = "1.0.134"
|
||||||
|
sqlx = { version = "0.7", features = ["macros", "migrate"] }
|
||||||
|
|
||||||
|
3
backend/Rocket.toml
Normal file
3
backend/Rocket.toml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[default.databases.main]
|
||||||
|
url = "./db.sqlite"
|
||||||
|
|
1
backend/migrations/20241231030841_init.sql
Normal file
1
backend/migrations/20241231030841_init.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
-- Add migration script here
|
@ -1,12 +1,83 @@
|
|||||||
|
use rocket::{
|
||||||
|
fairing,
|
||||||
|
http::Status,
|
||||||
|
request::{FromRequest, Outcome},
|
||||||
|
Request, Rocket,
|
||||||
|
};
|
||||||
|
use rocket_db_pools::sqlx::{self, Row};
|
||||||
|
use rocket_db_pools::{Connection, Database};
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
|
// so i was trying out claude.ai, and as a joke i talked to it as monke,
|
||||||
|
// and it answered like monke. so i thought it would be funny to,
|
||||||
|
// instead of plain, boring "User", just use "Monke" :D
|
||||||
|
|
||||||
|
/// Stores info about a Monke
|
||||||
|
struct Monke {
|
||||||
|
pub user_id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Database)]
|
||||||
|
#[database("main")]
|
||||||
|
struct MainDb(sqlx::SqlitePool);
|
||||||
|
|
||||||
|
#[rocket::async_trait]
|
||||||
|
impl fairing::Fairing for MainDb {
|
||||||
|
fn info(&self) -> fairing::Info {
|
||||||
|
fairing::Info {
|
||||||
|
name: "Database Migrations",
|
||||||
|
kind: fairing::Kind::Liftoff,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn on_liftoff(&self, _rocket: &Rocket<rocket::Orbit>) {
|
||||||
|
let db = self;
|
||||||
|
|
||||||
|
// monke run migrations here
|
||||||
|
sqlx::migrate!()
|
||||||
|
.run(&**db)
|
||||||
|
.await
|
||||||
|
.expect("Failed to migrate db");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rocket::async_trait]
|
||||||
|
impl<'r> FromRequest<'r> for Monke {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
||||||
|
// monke get cookies from request
|
||||||
|
let cookies = request.cookies();
|
||||||
|
|
||||||
|
// monke look for auth cookie
|
||||||
|
match cookies.get_private("monke_session") {
|
||||||
|
Some(cookie) => {
|
||||||
|
let auth_db = request.rocket().state::<MainDb>().unwrap();
|
||||||
|
|
||||||
|
// in real app, monke validate token here
|
||||||
|
if cookie.value().is_empty() {
|
||||||
|
Outcome::Error((Status::Unauthorized, ()))
|
||||||
|
} else {
|
||||||
|
Outcome::Success(Monke {
|
||||||
|
user_id: cookie.value().to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => Outcome::Error((Status::Unauthorized, ())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index() -> &'static str {
|
fn index() -> &'static str {
|
||||||
"Hello, world!"
|
"Hello, world!"
|
||||||
}
|
}
|
||||||
|
|
||||||
#[launch]
|
#[launch]
|
||||||
fn rocket() -> _ {
|
async fn rocket() -> _ {
|
||||||
rocket::build().mount("/", routes![index])
|
rocket::build()
|
||||||
|
.attach(MainDb::init())
|
||||||
|
.mount("/", routes![index])
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user