diff --git a/Cargo.lock b/Cargo.lock index 954dad5..3de787b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,6 +69,7 @@ checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" dependencies = [ "async-trait", "axum-core", + "axum-macros", "bitflags 1.3.2", "bytes", "futures-util", @@ -110,6 +111,18 @@ dependencies = [ "tower-service", ] +[[package]] +name = "axum-macros" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdca6a10ecad987bda04e95606ef85a5417dcaac1a78455242d72e031e2b6b62" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "backtrace" version = "0.3.69" @@ -193,6 +206,7 @@ dependencies = [ "iana-time-zone", "js-sys", "num-traits", + "serde", "wasm-bindgen", "windows-targets", ] @@ -339,6 +353,12 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.3" diff --git a/Cargo.toml b/Cargo.toml index 49f1654..3a40bf2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,8 +15,8 @@ edition = "2021" name = "fediblog" [dependencies] -axum = "0.6.20" -chrono = "0.4.31" +axum = {version = "0.6.20", features = ["macros"]} +chrono = {version = "0.4.31", features = ["serde"]} diesel = { version = "2.1.3", features = ["postgres", "extras", "uuid"] } dotenvy = "0.15.7" serde = { version = "1.0.189", features = ["derive"] } diff --git a/migrations/2023-10-15-172140_create_users/up.sql b/migrations/2023-10-15-172140_create_users/up.sql index e8aee9d..1f5e9a2 100644 --- a/migrations/2023-10-15-172140_create_users/up.sql +++ b/migrations/2023-10-15-172140_create_users/up.sql @@ -6,11 +6,12 @@ CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; CREATE TABLE users ( - id SERIAL PRIMARY KEY, + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), username VARCHAR NOT NULL, epost VARCHAR NOT NULL, pass VARCHAR NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT NOW(), - updated_at TIMESTAMP NOT NULL DEFAULT NOW() + updated_at TIMESTAMP NOT NULL DEFAULT NOW(), + main_fedi TEXT ); SELECT diesel_manage_updated_at('users'); diff --git a/migrations/2023-10-15-172149_create_posts/up.sql b/migrations/2023-10-15-172149_create_posts/up.sql index 7e02e5f..66867ba 100644 --- a/migrations/2023-10-15-172149_create_posts/up.sql +++ b/migrations/2023-10-15-172149_create_posts/up.sql @@ -10,7 +10,7 @@ CREATE TABLE posts ( title VARCHAR NOT NULL, body TEXT NOT NULL, published BOOLEAN NOT NULL DEFAULT FALSE, - author INTEGER NOT NULL REFERENCES users(id), + user_id UUID NOT NULL REFERENCES users(id), created_at TIMESTAMP NOT NULL DEFAULT NOW(), updated_at TIMESTAMP NOT NULL DEFAULT NOW() ); diff --git a/migrations/2023-10-15-172156_create_blogs/up.sql b/migrations/2023-10-15-172156_create_blogs/up.sql index 07a0a32..c9ea5fb 100644 --- a/migrations/2023-10-15-172156_create_blogs/up.sql +++ b/migrations/2023-10-15-172156_create_blogs/up.sql @@ -9,7 +9,7 @@ CREATE TABLE blogs ( name VARCHAR NOT NULL, description TEXT NOT NULL, url TEXT NOT NULL, - owner INTEGER NOT NULL REFERENCES users(id), + user_id UUID NOT NULL REFERENCES users(id), created_at TIMESTAMP NOT NULL DEFAULT NOW(), updated_at TIMESTAMP NOT NULL DEFAULT NOW() ); diff --git a/migrations/2023-10-17-164429_posts_add_blog_ref/down.sql b/migrations/2023-10-17-164429_posts_add_blog_ref/down.sql new file mode 100644 index 0000000..089f535 --- /dev/null +++ b/migrations/2023-10-17-164429_posts_add_blog_ref/down.sql @@ -0,0 +1,8 @@ +-- SPDX-FileCopyrightText: 2023 Louis Hollingworth +-- +-- SPDX-License-Identifier: AGPL-3.0-or-later + +-- This file should undo anything in `up.sql` + +ALTER TABLE posts +DROP COLUMN blog_id; diff --git a/migrations/2023-10-17-164429_posts_add_blog_ref/up.sql b/migrations/2023-10-17-164429_posts_add_blog_ref/up.sql new file mode 100644 index 0000000..ba032c7 --- /dev/null +++ b/migrations/2023-10-17-164429_posts_add_blog_ref/up.sql @@ -0,0 +1,8 @@ +-- SPDX-FileCopyrightText: 2023 Louis Hollingworth +-- +-- SPDX-License-Identifier: AGPL-3.0-or-later + +-- Your SQL goes here + +ALTER TABLE posts +ADD COLUMN blog_id INTEGER NOT NULL REFERENCES blogs(id); diff --git a/src/lib.rs b/src/lib.rs index bbd4e6c..ab924a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,8 @@ // SPDX-FileCopyrightText: 2023 Louis Hollingworth // // SPDX-License-Identifier: AGPL-3.0-or-later -pub mod schema; -pub mod uuid; pub mod models; +pub mod schema; use diesel::prelude::*; pub fn establish_connection() -> diesel::pg::PgConnection { @@ -13,4 +12,3 @@ pub fn establish_connection() -> diesel::pg::PgConnection { diesel::pg::PgConnection::establish(&database_url) .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) } - diff --git a/src/main.rs b/src/main.rs index ef49f15..43aec32 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,18 @@ * * SPDX-License-Identifier: AGPL-3.0-or-later */ -#![feature(trivial_bounds)] +use axum::{routing::get, Router}; -fn main() { +#[tokio::main] +async fn main() { + let app = Router::new().route("/health", get(health_check)); + + axum::Server::bind(&"0.0.0.0:7654".parse().unwrap()) + .serve(app.into_make_service()) + .await + .unwrap(); +} + +async fn health_check() -> &'static str { + "Saluton, amiko! Looks like we are running!" } diff --git a/src/models.rs b/src/models.rs index ad4277d..3463d7d 100644 --- a/src/models.rs +++ b/src/models.rs @@ -3,10 +3,52 @@ // SPDX-License-Identifier: AGPL-3.0-or-later use diesel::prelude::*; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; -#[derive(Queryable, Selectable)] -#[diesel(table_name = crate::schema::posts)] +#[derive(Queryable, Selectable, Identifiable, Debug, PartialEq, Serialize, Deserialize)] +#[diesel(table_name = crate::schema::users)] #[diesel(check_for_backend(diesel::pg::Pg))] pub struct User { - pub id: i32, + pub id: Uuid, + pub username: String, + pub epost: String, + pub pass: String, + pub created_at: chrono::NaiveDateTime, + pub updated_at: chrono::NaiveDateTime, + pub main_fedi: Option, +} + +#[derive( + Queryable, Selectable, Identifiable, Associations, Debug, PartialEq, Serialize, Deserialize, +)] +#[diesel(belongs_to(User))] +#[diesel(table_name = crate::schema::blogs)] +pub struct Blog { + pub id: isize, + pub slug: String, + pub name: String, + pub description: String, + pub url: String, + pub user_id: Uuid, + pub created_at: chrono::NaiveDateTime, + pub updated_at: chrono::NaiveDateTime, +} + +#[derive( + Queryable, Selectable, Identifiable, Associations, Debug, PartialEq, Serialize, Deserialize, +)] +#[diesel(belongs_to(User))] +#[diesel(belongs_to(Blog))] +#[diesel(table_name = crate::schema::posts)] +pub struct Post { + pub id: isize, + pub slug: String, + pub title: String, + pub body: String, + pub published: bool, + pub user_id: Uuid, + pub blog_id: isize, + pub created_at: chrono::NaiveDateTime, + pub updated_at: chrono::NaiveDateTime, } diff --git a/src/schema.rs b/src/schema.rs index b4b0cc5..c05913f 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -11,7 +11,7 @@ diesel::table! { name -> Varchar, description -> Text, url -> Text, - owner -> Int4, + user_id -> Uuid, created_at -> Timestamp, updated_at -> Timestamp, } @@ -24,28 +24,27 @@ diesel::table! { title -> Varchar, body -> Text, published -> Bool, - author -> Int4, + user_id -> Uuid, created_at -> Timestamp, updated_at -> Timestamp, + blog_id -> Int4, } } diesel::table! { users (id) { - id -> Int4, + id -> Uuid, username -> Varchar, epost -> Varchar, pass -> Varchar, created_at -> Timestamp, updated_at -> Timestamp, + main_fedi -> Nullable, } } -diesel::joinable!(blogs -> users (owner)); -diesel::joinable!(posts -> users (author)); +diesel::joinable!(blogs -> users (user_id)); +diesel::joinable!(posts -> blogs (blog_id)); +diesel::joinable!(posts -> users (user_id)); -diesel::allow_tables_to_appear_in_same_query!( - blogs, - posts, - users, -); +diesel::allow_tables_to_appear_in_same_query!(blogs, posts, users,);