some initial setup stuff
This commit is contained in:
commit
aa4036afba
15 changed files with 2748 additions and 0 deletions
17
crates/backend/Cargo.toml
Normal file
17
crates/backend/Cargo.toml
Normal file
|
@ -0,0 +1,17 @@
|
|||
[package]
|
||||
name = "backend"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = { version = "0.7.5", features = [ "json", "macros" ] }
|
||||
axum-macros = "0.4.1"
|
||||
chrono-tz = "0.10.0"
|
||||
maud = "0.26.0"
|
||||
sqlx = { version = "0.8.2", features = ["postgres", "runtime-tokio", "tls-rustls-ring", "uuid" ] }
|
||||
tokio = { version = "1.40.0", features = ["full"] }
|
||||
tokio-tungstenite = "0.24.0"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
anyhow = "1"
|
||||
uuid = { version = "1.10.0", features = ["serde"] }
|
||||
rand = "0.8.5"
|
16
crates/backend/migrations/20240921175315_init.sql
Normal file
16
crates/backend/migrations/20240921175315_init.sql
Normal file
|
@ -0,0 +1,16 @@
|
|||
--============================================================================--
|
||||
-- I HATE DATABASES!!! --
|
||||
--============================================================================--
|
||||
|
||||
create table chats (
|
||||
id uuid default (gen_random_uuid()) primary key,
|
||||
url_path char(6) unique not null,
|
||||
name text
|
||||
);
|
||||
|
||||
create table messages (
|
||||
id uuid default (gen_random_uuid()) primary key,
|
||||
chat_id uuid not null references chats(id),
|
||||
content varchar(2000) not null,
|
||||
from_admin boolean not null
|
||||
)
|
33
crates/backend/src/admin.rs
Normal file
33
crates/backend/src/admin.rs
Normal file
|
@ -0,0 +1,33 @@
|
|||
use axum::{
|
||||
extract::{Path, State},
|
||||
routing::get,
|
||||
Json, Router,
|
||||
};
|
||||
use rand::distributions::{Alphanumeric, DistString};
|
||||
use sqlx::{Pool, Postgres, QueryBuilder};
|
||||
|
||||
use crate::model::Chat;
|
||||
|
||||
pub fn router(pool: Pool<Postgres>) -> Router {
|
||||
Router::new()
|
||||
.route("/new/:amount", get(create))
|
||||
.with_state(pool)
|
||||
}
|
||||
|
||||
async fn create(Path(amount): Path<u8>, State(pool): State<Pool<Postgres>>) -> Json<Vec<Chat>> {
|
||||
let paths: Vec<String> = (0..amount)
|
||||
.map(|_| Alphanumeric.sample_string(&mut rand::thread_rng(), 6))
|
||||
.collect();
|
||||
|
||||
let r: Vec<Chat> = QueryBuilder::new("insert into chats (url_path)")
|
||||
.push_values(paths, |mut b, v| {
|
||||
b.push_bind(v);
|
||||
})
|
||||
.push("returning *")
|
||||
.build_query_as()
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Json(r)
|
||||
}
|
29
crates/backend/src/chat.rs
Normal file
29
crates/backend/src/chat.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
use axum::{
|
||||
extract::{Path, State},
|
||||
response::Html,
|
||||
Json,
|
||||
};
|
||||
use maud::{html, Render};
|
||||
use sqlx::{Pool, Postgres};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::model::{Chat, Message};
|
||||
|
||||
pub async fn get(
|
||||
Path(url_path): Path<String>,
|
||||
State(pool): State<Pool<Postgres>>,
|
||||
) -> Json<Vec<Message>> {
|
||||
let chat = sqlx::query_as!(Chat, r#"select * from chats where url_path = $1"#, url_path)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let messages = sqlx::query_as!(
|
||||
Message,
|
||||
r#"select * from messages where chat_id = $1"#,
|
||||
chat.id
|
||||
)
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
Json(messages)
|
||||
}
|
28
crates/backend/src/main.rs
Normal file
28
crates/backend/src/main.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
use axum::{routing::get, Router};
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
const DB_URL: &str = "postgres://localhost/chatdings";
|
||||
const ADMIN_TOK: &str = "meow";
|
||||
|
||||
mod admin;
|
||||
mod chat;
|
||||
mod model;
|
||||
mod stat;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let pool = Pool::<Postgres>::connect(DB_URL).await?;
|
||||
|
||||
sqlx::migrate!().run(&pool).await?;
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(|| async { "<h1>gay</h1>" }))
|
||||
.route("/:path", get(chat::get))
|
||||
.with_state(pool.clone())
|
||||
.nest("/stat", stat::router(pool.clone()))
|
||||
.nest("/admin", admin::router(pool.clone()));
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
|
||||
axum::serve(listener, app).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
34
crates/backend/src/model.rs
Normal file
34
crates/backend/src/model.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
use serde::Serialize;
|
||||
use sqlx::{prelude::FromRow, Decode, Encode};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Decode, Serialize, FromRow)]
|
||||
pub struct Chat {
|
||||
// Uuid but sqlx doesnt impl serde traits for them
|
||||
pub id: Uuid,
|
||||
pub url_path: String,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Decode, Serialize, FromRow)]
|
||||
pub struct Message {
|
||||
// Uuid but sqlx doesnt impl serde traits for them
|
||||
pub id: Uuid,
|
||||
pub chat_id: Uuid,
|
||||
|
||||
pub content: String,
|
||||
pub from_admin: bool,
|
||||
}
|
||||
|
||||
#[derive(Encode)]
|
||||
pub struct NewChat {
|
||||
url_path: String,
|
||||
name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Encode)]
|
||||
pub struct NewMessage {
|
||||
chat_id: Uuid,
|
||||
content: String,
|
||||
from_admin: bool,
|
||||
}
|
20
crates/backend/src/stat.rs
Normal file
20
crates/backend/src/stat.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use axum::{extract::State, routing::get, Json, Router};
|
||||
use sqlx::{types::Uuid, Pool, Postgres};
|
||||
|
||||
use crate::model::Chat;
|
||||
|
||||
// TODO: /stat/* should require authentication
|
||||
pub fn router(pool: Pool<Postgres>) -> Router {
|
||||
Router::new().route("/chats", get(chats)).with_state(pool)
|
||||
}
|
||||
|
||||
async fn chats(State(pool): State<Pool<Postgres>>) -> Json<Vec<Chat>> {
|
||||
let r = sqlx::query_as!(Chat, "select * from chats;")
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Json(r)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue