again, sorta get db working

This commit is contained in:
SadlyNotSappho 2024-01-05 11:58:28 -08:00
parent d1f4ae4c2c
commit 03cda97a0d
2 changed files with 24 additions and 15 deletions

View File

@ -15,15 +15,14 @@ services:
ports: ports:
- 8000:8000 - 8000:8000
environment: environment:
- PG_DBNAME=example # name in main.rs db.user POSTGRES_PASSWORD POSTGRES_DB
- PG_HOST=db - ROCKET_DATABASES={diesel_postgres={url="postgres://postgres:passwordpasswordpassword@db/example"}}
- PG_USER=postgres
- PG_PASSWORD=mysecretpassword
- ROCKET_ADDRESS=0.0.0.0 - ROCKET_ADDRESS=0.0.0.0
- ROCKET_PORT=8000 - ROCKET_PORT=8000
- RUST_LOG=debug - RUST_LOG=debug
- ROCKET_SECRET_KEY="openssl rand -base64 32" - ROCKET_SECRET_KEY=openssl rand -base64 32
volumes: volumes:
- type: bind - type: bind
source: ./web source: ./web
@ -47,7 +46,7 @@ services:
- db-data:/var/lib/postgresql/data - db-data:/var/lib/postgresql/data
environment: environment:
- POSTGRES_DB=example - POSTGRES_DB=example
- POSTGRES_PASSWORD_FILE=/run/secrets/db-password - POSTGRES_PASSWORD=passwordpasswordpassword
expose: expose:
- 5432 - 5432
healthcheck: healthcheck:

View File

@ -1,6 +1,9 @@
use rocket_db_pools::{Database, Connection}; use rocket::http::Status;
use rocket_db_pools::diesel::{QueryResult, PgPool, prelude::*}; use rocket::response::content::{self, RawHtml};
use rocket::Request;
use rocket_db_pools::diesel; use rocket_db_pools::diesel;
use rocket_db_pools::diesel::{prelude::*, PgPool, QueryResult};
use rocket_db_pools::{Connection, Database};
use std::fs; use std::fs;
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
@ -11,10 +14,9 @@ use rocket::{
tokio::time::{sleep, Duration}, tokio::time::{sleep, Duration},
}; };
#[get("/")] #[get("/")]
fn hello() -> String { fn hello() -> RawHtml<String> {
fs::read_to_string("/home/sadlynotsappho/Projects/Rust/Projects/fossil/web/index.html").unwrap() content::RawHtml(fs::read_to_string("/srv/web/index.html").unwrap())
// format!("hi!!!") // format!("hi!!!")
} }
@ -90,17 +92,25 @@ rocket_db_pools::diesel::table! {
#[get("/dbtest")] #[get("/dbtest")]
async fn dbtest(mut db: Connection<Db>) -> QueryResult<String> { async fn dbtest(mut db: Connection<Db>) -> QueryResult<String> {
let post_ids: Vec<i64> = posts::table let post_ids: Vec<i64> = posts::table.select(posts::id).load(&mut db).await?;
.select(posts::id)
.load(&mut db)
.await?;
Ok(format!("{post_ids:?}")) Ok(format!("{post_ids:?}"))
} }
#[catch(default)]
fn default_catcher(status: Status, _: &Request) -> RawHtml<String> {
content::RawHtml(
fs::read_to_string("/srv/web/error.html")
.unwrap()
.replace("{{errorcode}}", &status.code.to_string()[..]),
)
}
#[rocket::main] #[rocket::main]
async fn main() { async fn main() {
let _rocket = rocket::build() let _rocket = rocket::build()
.attach(Db::init())
.mount("/", routes![hello, get_book, delay, login, logout, dbtest]) .mount("/", routes![hello, get_book, delay, login, logout, dbtest])
.register("/", catchers![default_catcher])
.mount("/login", FileServer::from("/srv/web")) .mount("/login", FileServer::from("/srv/web"))
.launch() .launch()
.await .await