···11+FROM rust
22+WORKDIR /app
33+44+# copying just cargo config and making a stub main
55+# this means docker can cache building deps
66+# and just build the main program in watch mode
77+# much quicker and nicer on my cpu, network, and ram lol
88+COPY ./Cargo.toml ./Cargo.toml
99+COPY ./Cargo.lock ./Cargo.lock
1010+RUN mkdir ./src && echo 'fn main() { println!("Using cache build!!"); loop { println!("DOCKERFILE failed..."); } }' > ./src/main.rs
1111+RUN cargo build --locked
1212+RUN rm -f ./target/debug/index
1313+1414+COPY ./src ./src
1515+COPY ./.sqlx ./.sqlx
1616+RUN cargo build --locked
1717+1818+RUN cp ./target/debug/index ./index
1919+CMD ["/app/index"]
+2
sqlx.sh
···11+docker compose up db -d
22+DATABASE_URL=postgres://user:password@localhost:5432/user cargo sqlx prepare
+31
src/config.rs
···11+use jacquard::types::string::Did;
22+use std::env;
33+use std::sync::LazyLock;
44+55+// this should be loaded before the program starts any threads
66+// if this panics threads that access it will be poisoned
77+pub static USER: LazyLock<Did<'static>> = LazyLock::new(|| {
88+ let Ok(env) = env::var("INDEX_USER") else {
99+ panic!("INDEX_USER not set");
1010+ };
1111+1212+ let Ok(did) = Did::new_owned(env) else {
1313+ panic!("INDEX_USER was not a valid did")
1414+ };
1515+1616+ did
1717+});
1818+1919+pub static POSTGRES_URL: LazyLock<String> = LazyLock::new(|| {
2020+ let Ok(user) = env::var("POSTGRES_USER") else {
2121+ panic!("POSTGRES_USER was not set. Cannot connect to database")
2222+ };
2323+ let Ok(password) = env::var("POSTGRES_PASSWORD") else {
2424+ panic!("POSTGRES_PASSWORD was not set. Cannot connect to database")
2525+ };
2626+ let Ok(host) = env::var("POSTGRES_HOST") else {
2727+ panic!("POSTGRES_HOST was not set. Cannot connect to database")
2828+ };
2929+3030+ format!("postgres://{}:{}@{}/{}", user, password, host, user)
3131+});
+63
src/db.rs
···11+use crate::config;
22+use sqlx::{Pool, Postgres, postgres::PgPool, query};
33+44+pub async fn init() -> Pool<Postgres> {
55+ let conn = PgPool::connect(&config::POSTGRES_URL).await;
66+ let conn = match conn {
77+ Ok(val) => val,
88+ Err(err) => {
99+ println!("Could not connect to the database. Got error {err}");
1010+ panic!()
1111+ }
1212+ };
1313+ println!("Connected to db with pool: {:?}", conn);
1414+1515+ // initialise db tables
1616+ if let Err(err) = query!(
1717+ "CREATE TABLE IF NOT EXISTS records (
1818+ collection TEXT,
1919+ rkey TEXT,
2020+ record JSON NOT NULL,
2121+ PRIMARY KEY (collection, rkey)
2222+ );"
2323+ )
2424+ .execute(&conn)
2525+ .await
2626+ {
2727+ println!("Creating table `records`: \n{err}");
2828+ panic!("Could not instantiate db");
2929+ };
3030+3131+ if let Err(err) = query!(
3232+ "CREATE TABLE IF NOT EXISTS foreign_records (
3333+ did TEXT,
3434+ collection TEXT,
3535+ rkey TEXT,
3636+ record JSON NOT NULL,
3737+ PRIMARY KEY (did, collection, rkey)
3838+ );"
3939+ )
4040+ .execute(&conn)
4141+ .await
4242+ {
4343+ println!("Creating table `foreign_records`: \n{err}");
4444+ panic!();
4545+ };
4646+4747+ if let Err(err) = query!(
4848+ "CREATE TABLE IF NOT EXISTS blobs (
4949+ did TEXT,
5050+ cid TEXT,
5151+ blob bytea NOT NULL,
5252+ PRIMARY KEY (did, cid)
5353+ )"
5454+ )
5555+ .execute(&conn)
5656+ .await
5757+ {
5858+ println!("Creating table `blobs`: \n{err}");
5959+ panic!();
6060+ };
6161+6262+ return conn;
6363+}