sorta get db working
This commit is contained in:
parent
b9bf81a1be
commit
d1f4ae4c2c
|
@ -332,6 +332,7 @@ dependencies = [
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"diesel_derives",
|
"diesel_derives",
|
||||||
"itoa",
|
"itoa",
|
||||||
|
"pq-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -454,6 +455,7 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||||
name = "fossil"
|
name = "fossil"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"diesel",
|
||||||
"rocket",
|
"rocket",
|
||||||
"rocket_db_pools",
|
"rocket_db_pools",
|
||||||
]
|
]
|
||||||
|
@ -1068,6 +1070,15 @@ version = "0.2.17"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
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]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.69"
|
version = "1.0.69"
|
||||||
|
@ -1851,6 +1862,12 @@ version = "0.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
|
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vcpkg"
|
||||||
|
version = "0.2.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version_check"
|
name = "version_check"
|
||||||
version = "0.9.4"
|
version = "0.9.4"
|
||||||
|
|
|
@ -6,5 +6,6 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
diesel = { version = "2.1.4", features = ["postgres"] }
|
||||||
rocket = {version="0.5.0",features=["secrets","json"]}
|
rocket = {version="0.5.0",features=["secrets","json"]}
|
||||||
rocket_db_pools = {version="0.1.0",features=["diesel_postgres"]}
|
rocket_db_pools = {version="0.1.0",features=["diesel_postgres"]}
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
ARG RUST_VERSION=1.74.0
|
ARG RUST_VERSION=1.74.0
|
||||||
ARG APP_NAME=fossil
|
ARG APP_NAME=fossil
|
||||||
FROM rust:${RUST_VERSION}-slim-bullseye AS build
|
FROM rust:${RUST_VERSION} AS build
|
||||||
ARG APP_NAME
|
ARG APP_NAME
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ EOF
|
||||||
# most recent version of that tag when you build your Dockerfile. If
|
# most recent version of that tag when you build your Dockerfile. If
|
||||||
# reproducability is important, consider using a digest
|
# reproducability is important, consider using a digest
|
||||||
# (e.g., debian@sha256:ac707220fbd7b67fc19b112cee8170b41a9e97f703f588b2cdbbcdcecdd8af57).
|
# (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.
|
# Create a non-privileged user that the app will run under.
|
||||||
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ #user
|
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ #user
|
||||||
|
@ -56,6 +56,9 @@ RUN adduser \
|
||||||
--no-create-home \
|
--no-create-home \
|
||||||
--uid "${UID}" \
|
--uid "${UID}" \
|
||||||
appuser
|
appuser
|
||||||
|
|
||||||
|
RUN apt update && apt install libpq-dev libc6 -y
|
||||||
|
|
||||||
USER appuser
|
USER appuser
|
||||||
|
|
||||||
# Copy the executable from the "build" stage.
|
# Copy the executable from the "build" stage.
|
||||||
|
|
35
src/main.rs
35
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;
|
use std::fs;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
@ -8,6 +11,7 @@ use rocket::{
|
||||||
tokio::time::{sleep, Duration},
|
tokio::time::{sleep, Duration},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn hello() -> String {
|
fn hello() -> String {
|
||||||
fs::read_to_string("/home/sadlynotsappho/Projects/Rust/Projects/fossil/web/index.html").unwrap()
|
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]
|
#[rocket::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let _rocket = rocket::build()
|
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"))
|
.mount("/login", FileServer::from("/srv/web"))
|
||||||
.launch()
|
.launch()
|
||||||
.await
|
.await
|
||||||
|
|
Loading…
Reference in New Issue