sorta get db working

This commit is contained in:
SadlyNotSappho 2024-01-02 11:56:38 -08:00
parent b9bf81a1be
commit d1f4ae4c2c
4 changed files with 57 additions and 3 deletions

17
Cargo.lock generated
View File

@ -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"

View File

@ -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"]}

View File

@ -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.

View File

@ -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<Db>) -> QueryResult<String> {
let post_ids: Vec<i64> = 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