technically, this can be used as a blogging software now. please do not do that though.

This commit is contained in:
SadlyNotSappho 2024-01-26 11:59:39 -08:00
parent 70f5ffd30c
commit 16c1fd028a
4 changed files with 48 additions and 18 deletions

View File

@ -25,7 +25,6 @@ RUN --mount=type=bind,source=src,target=src \
--mount=type=bind,source=Cargo.lock,target=Cargo.lock \ --mount=type=bind,source=Cargo.lock,target=Cargo.lock \
--mount=type=cache,target=/app/target/ \ --mount=type=cache,target=/app/target/ \
--mount=type=cache,target=/usr/local/cargo/registry/ \ --mount=type=cache,target=/usr/local/cargo/registry/ \
--mount=type=bind,source=db,target=/srv/db \
<<EOF <<EOF
set -e set -e
cargo build --locked --release cargo build --locked --release
@ -65,8 +64,6 @@ USER appuser
# Copy the executable from the "build" stage. # Copy the executable from the "build" stage.
COPY --from=build /bin/server /bin/ COPY --from=build /bin/server /bin/
ADD ./db /srv/db
# Expose the port that the application listens on. # Expose the port that the application listens on.
EXPOSE 8000 EXPOSE 8000

View File

@ -1,6 +0,0 @@
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title VARCHAR NOT NULL,
body TEXT NOT NULL,
published BOOLEAN NOT NULL DEFAULT FALSE
)

View File

@ -1,9 +1,9 @@
use rocket::fairing::{self, AdHoc, Fairing, Info, Kind}; use rocket::fairing::AdHoc;
use rocket::http::Status; use rocket::http::Status;
use rocket::response::content::{self, RawHtml}; use rocket::response::content::{self, RawHtml};
use rocket::{Build, Request, Rocket}; use rocket::{Build, Request, Rocket};
use rocket_db_pools::sqlx::pool::PoolConnection; use rocket_db_pools::sqlx::pool::PoolConnection;
use rocket_db_pools::sqlx::{Executor, PgConnection, Postgres, Row}; use rocket_db_pools::sqlx::{Executor, Postgres, Row};
use rocket_db_pools::Connection; use rocket_db_pools::Connection;
use std::fs; use std::fs;
#[macro_use] #[macro_use]
@ -85,17 +85,48 @@ struct Db(PgPool);
struct Post { struct Post {
id: i64, id: i64,
title: String, title: String,
body: String,
published: bool, published: bool,
} }
#[get("/dbtest/<id>")] #[get("/dbtest/<id>")]
async fn dbtest(mut db: Connection<Db>, id: i64) -> Option<String> { async fn dbtest(mut db: Connection<Db>, id: i64) -> Option<String> {
sqlx::query("SELECT content FROM posts WHERE id = ?") let res = db
.bind(id) .fetch_one(sqlx::query_as::<_, Post>(
.fetch_one(&mut **db) &format!("SELECT * FROM posts WHERE id = {id};")[..],
))
.await .await
.and_then(|r| Ok(r.try_get(0)?)) .unwrap();
.ok() Some(format!(
"{}\n{}\n{}\n{}",
res.get::<i32, _>("id"),
res.get::<String, _>("title"),
res.get::<String, _>("body"),
res.get::<bool, _>("published")
))
}
#[get("/dbcreate/<title>/<body>/<published>")]
async fn dbcreate(
mut db: Connection<Db>,
title: String,
body: String,
published: bool,
) -> &'static str {
db.fetch_all(
sqlx::query(
r#"
INSERT INTO posts (title, body, published)
VALUES ($1, $2, $3);
"#,
)
.bind(title)
.bind(body)
.bind(published),
)
.await
.unwrap();
"created maybe"
} }
#[catch(default)] #[catch(default)]
@ -113,7 +144,12 @@ async fn migrate(rocket: Rocket<Build>) -> Rocket<Build> {
let row = conn let row = conn
.fetch_one(sqlx::query_as::<_, Post>( .fetch_one(sqlx::query_as::<_, Post>(
"SELECT * FROM table WHERE id = 1;", "CREATE TABLE IF NOT EXISTS posts (
id SERIAL PRIMARY KEY,
title VARCHAR NOT NULL,
body TEXT NOT NULL,
published BOOLEAN NOT NULL DEFAULT FALSE
)",
)) ))
.await; .await;
@ -126,7 +162,10 @@ async fn main() {
.attach(Db::init()) .attach(Db::init())
// .attach(Post) // .attach(Post)
.attach(AdHoc::on_ignite("DB Migrations", migrate)) .attach(AdHoc::on_ignite("DB Migrations", migrate))
.mount("/", routes![hello, get_book, delay, login, logout, dbtest]) .mount(
"/",
routes![hello, get_book, delay, login, logout, dbtest, dbcreate],
)
.register("/", catchers![default_catcher]) .register("/", catchers![default_catcher])
.mount("/login", FileServer::from("/srv/web")) .mount("/login", FileServer::from("/srv/web"))
.launch() .launch()