Started rocket backend

This commit is contained in:
2024-08-10 20:24:26 +02:00
parent 07ae4c4118
commit e37a57ea81
13 changed files with 188 additions and 167 deletions

View File

@@ -3,14 +3,100 @@
use rocket::fs::{FileServer, relative};
use rocket_dyn_templates::{Template, context};
use rocket_db_pools::{Database, Connection};
use rocket_db_pools::diesel::{QueryResult, MysqlPool, prelude::*};
use rocket::form::Form;
/*use dotenvy::dotenv;
use std::env;*/
#[get("/table/<tname>")]
#[derive(Database)]
#[database("inventur")]
struct Db(MysqlPool);
/*fn connect_db() -> MysqlConnection {
dotenv.ok();
let database_url = env::var("DATABASE_URL").expect("Can't find database url");
MysqlConnection::establish(&database_url).expect_or_else(|_| panic!("Couldn't connect to database {}.", database_url));
}*/
struct Owner {
id: i64,
email: String,
name: Option<String>,
}
#[derive(Queryable, Insertable)]
#[diesel(table_name = users)]
struct User {
id: i64,
email: String,
}
diesel::table! {
users (id) {
id -> BigInt,
email -> Text,
}
}
#[derive(Queryable, Insertable)]
#[diesel(table_name = jrtables)]
struct JRTable {
id: i64,
name: String,
owner_id: i64,
}
diesel::table! {
jrtables (id) {
id -> BigInt,
name -> Text,
owner_id -> BigInt,
}
}
#[get("/<tname>")]
fn table(tname: &str) -> Template {
Template::render("table", context!{tname: tname, columns: ["name", "djhfae", "fsjhr"], rows: [["1", "first", "hasdjf", "753rgf"], ["2", "second", "7438ued", "🚀"], ["3", "third", "", ""]] })
let columns = ["name", "djhfae", "fsjhr"];
let rows = [
["1", "first", "hasdjf", "753rgf"],
["2", "second", "7438ued", "🚀"],
["3", "third", "", ""]
];
Template::render("table",
context!{tname: tname,
columns: columns,
rows: rows,
}
)
}
#[derive(FromForm)]
struct New_table<'r> {
name: &'r str,
fields: Vec<&'r str>,
#[field(name = "done")]
complete: bool,
}
#[post("/create", data="<data>")]
fn create(data: Form<New_table<'_>>) {
//println!("{:?}", data);
}
#[derive(FromForm)]
struct Args <'r> {
value: &'r str,
}
#[get("/table/<tname>?filter&<column>&<mode>&<args..>")]
async fn filter(db:Connection<Db>, tname: &str, column: &str, mode: &str, args: Args<'_>) -> &'static str {
todo!()
}
#[get("/")]
fn index() -> Template {
fn index(db: Connection<Db>) -> Template {
Template::render("test", context!{foo: 123,})
}
@@ -18,6 +104,9 @@ fn index() -> Template {
fn rocket() -> _ {
rocket::build()
.attach(Template::fairing())
.attach(Db::init())
.mount("/", FileServer::from(relative!("static")))
.mount("/", routes![index, table])
.mount("/", routes![index])
.mount("/table", routes![create, table])
//connect_db();
}

0
src/models.rs Normal file
View File

11
src/schema.rs Normal file
View File

@@ -0,0 +1,11 @@
// @generated automatically by Diesel CLI.
diesel::table! {
users (id) {
id -> Integer,
#[max_length = 64]
email -> Varchar,
#[max_length = 255]
name -> Nullable<Varchar>,
}
}