From d1f4ae4c2c48922c06d5d1abd003eba4890bd896 Mon Sep 17 00:00:00 2001 From: SadlyNotSappho Date: Tue, 2 Jan 2024 11:56:38 -0800 Subject: [PATCH] sorta get db working --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 1 + Dockerfile | 7 +++++-- src/main.rs | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d967bff..dcc449f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -332,6 +332,7 @@ dependencies = [ "byteorder", "diesel_derives", "itoa", + "pq-sys", ] [[package]] @@ -454,6 +455,7 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" name = "fossil" version = "0.1.0" dependencies = [ + "diesel", "rocket", "rocket_db_pools", ] @@ -1068,6 +1070,15 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "pq-sys" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31c0052426df997c0cbd30789eb44ca097e3541717a7b8fa36b1c464ee7edebd" +dependencies = [ + "vcpkg", +] + [[package]] name = "proc-macro2" version = "1.0.69" @@ -1851,6 +1862,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index e886a8a..bf88679 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +diesel = { version = "2.1.4", features = ["postgres"] } rocket = {version="0.5.0",features=["secrets","json"]} rocket_db_pools = {version="0.1.0",features=["diesel_postgres"]} diff --git a/Dockerfile b/Dockerfile index 775490c..adeeea2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ ARG RUST_VERSION=1.74.0 ARG APP_NAME=fossil -FROM rust:${RUST_VERSION}-slim-bullseye AS build +FROM rust:${RUST_VERSION} AS build ARG APP_NAME WORKDIR /app @@ -43,7 +43,7 @@ EOF # most recent version of that tag when you build your Dockerfile. If # reproducability is important, consider using a digest # (e.g., debian@sha256:ac707220fbd7b67fc19b112cee8170b41a9e97f703f588b2cdbbcdcecdd8af57). -FROM debian:bullseye-slim AS final +FROM debian:stable-slim AS final # Create a non-privileged user that the app will run under. # See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ #user @@ -56,6 +56,9 @@ RUN adduser \ --no-create-home \ --uid "${UID}" \ appuser + +RUN apt update && apt install libpq-dev libc6 -y + USER appuser # Copy the executable from the "build" stage. diff --git a/src/main.rs b/src/main.rs index cdc81ba..faa66f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +use rocket_db_pools::{Database, Connection}; +use rocket_db_pools::diesel::{QueryResult, PgPool, prelude::*}; +use rocket_db_pools::diesel; use std::fs; #[macro_use] extern crate rocket; @@ -8,6 +11,7 @@ use rocket::{ tokio::time::{sleep, Duration}, }; + #[get("/")] fn hello() -> String { fs::read_to_string("/home/sadlynotsappho/Projects/Rust/Projects/fossil/web/index.html").unwrap() @@ -64,10 +68,39 @@ async fn logout(cookies: &CookieJar<'_>) -> &'static str { } } +#[derive(Database)] +#[database("diesel_postgres")] +struct Db(PgPool); + +#[derive(Queryable, Insertable)] +#[diesel(table_name = posts)] +struct Post { + id: i64, + title: String, + published: bool, +} + +rocket_db_pools::diesel::table! { + posts (id) { + id -> BigInt, + title -> Text, + published -> Bool, + } +} + +#[get("/dbtest")] +async fn dbtest(mut db: Connection) -> QueryResult { + let post_ids: Vec = posts::table + .select(posts::id) + .load(&mut db) + .await?; + Ok(format!("{post_ids:?}")) +} + #[rocket::main] async fn main() { let _rocket = rocket::build() - .mount("/", routes![hello, get_book, delay, login, logout]) + .mount("/", routes![hello, get_book, delay, login, logout, dbtest]) .mount("/login", FileServer::from("/srv/web")) .launch() .await