(feat) Server is now running

Added DB schemas that are needed for the basics.

Signed-off-by: Louis Hollingworth <louis@hollingworth.nl>
This commit is contained in:
Louis Hollingworth 2023-10-17 18:20:02 +01:00
parent b330bea46d
commit cf80d4b70d
Signed by: lucxjo
GPG key ID: A11415CB3DC7809B
11 changed files with 111 additions and 24 deletions

20
Cargo.lock generated
View file

@ -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"

View file

@ -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"] }

View file

@ -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');

View file

@ -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()
);

View file

@ -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()
);

View file

@ -0,0 +1,8 @@
-- SPDX-FileCopyrightText: 2023 Louis Hollingworth <louis@hollingworth.nl>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later
-- This file should undo anything in `up.sql`
ALTER TABLE posts
DROP COLUMN blog_id;

View file

@ -0,0 +1,8 @@
-- SPDX-FileCopyrightText: 2023 Louis Hollingworth <louis@hollingworth.nl>
--
-- 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);

View file

@ -1,9 +1,8 @@
// SPDX-FileCopyrightText: 2023 Louis Hollingworth <louis@hollingworth.nl>
//
// 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))
}

View file

@ -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!"
}

View file

@ -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<String>,
}
#[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,
}

View file

@ -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<Text>,
}
}
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,);