again, sorta get db working
This commit is contained in:
parent
d1f4ae4c2c
commit
03cda97a0d
|
@ -15,15 +15,14 @@ services:
|
|||
ports:
|
||||
- 8000:8000
|
||||
environment:
|
||||
- PG_DBNAME=example
|
||||
- PG_HOST=db
|
||||
- PG_USER=postgres
|
||||
- PG_PASSWORD=mysecretpassword
|
||||
# name in main.rs db.user POSTGRES_PASSWORD POSTGRES_DB
|
||||
- ROCKET_DATABASES={diesel_postgres={url="postgres://postgres:passwordpasswordpassword@db/example"}}
|
||||
|
||||
- ROCKET_ADDRESS=0.0.0.0
|
||||
- ROCKET_PORT=8000
|
||||
- RUST_LOG=debug
|
||||
|
||||
- ROCKET_SECRET_KEY="openssl rand -base64 32"
|
||||
- ROCKET_SECRET_KEY=openssl rand -base64 32
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ./web
|
||||
|
@ -47,7 +46,7 @@ services:
|
|||
- db-data:/var/lib/postgresql/data
|
||||
environment:
|
||||
- POSTGRES_DB=example
|
||||
- POSTGRES_PASSWORD_FILE=/run/secrets/db-password
|
||||
- POSTGRES_PASSWORD=passwordpasswordpassword
|
||||
expose:
|
||||
- 5432
|
||||
healthcheck:
|
||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -1,6 +1,9 @@
|
|||
use rocket_db_pools::{Database, Connection};
|
||||
use rocket_db_pools::diesel::{QueryResult, PgPool, prelude::*};
|
||||
use rocket::http::Status;
|
||||
use rocket::response::content::{self, RawHtml};
|
||||
use rocket::Request;
|
||||
use rocket_db_pools::diesel;
|
||||
use rocket_db_pools::diesel::{prelude::*, PgPool, QueryResult};
|
||||
use rocket_db_pools::{Connection, Database};
|
||||
use std::fs;
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
@ -11,10 +14,9 @@ use rocket::{
|
|||
tokio::time::{sleep, Duration},
|
||||
};
|
||||
|
||||
|
||||
#[get("/")]
|
||||
fn hello() -> String {
|
||||
fs::read_to_string("/home/sadlynotsappho/Projects/Rust/Projects/fossil/web/index.html").unwrap()
|
||||
fn hello() -> RawHtml<String> {
|
||||
content::RawHtml(fs::read_to_string("/srv/web/index.html").unwrap())
|
||||
// format!("hi!!!")
|
||||
}
|
||||
|
||||
|
@ -90,17 +92,25 @@ rocket_db_pools::diesel::table! {
|
|||
|
||||
#[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?;
|
||||
let post_ids: Vec<i64> = posts::table.select(posts::id).load(&mut db).await?;
|
||||
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]
|
||||
async fn main() {
|
||||
let _rocket = rocket::build()
|
||||
.attach(Db::init())
|
||||
.mount("/", routes![hello, get_book, delay, login, logout, dbtest])
|
||||
.register("/", catchers![default_catcher])
|
||||
.mount("/login", FileServer::from("/srv/web"))
|
||||
.launch()
|
||||
.await
|
||||
|
|
Loading…
Reference in New Issue