some initial setup stuff
This commit is contained in:
commit
aa4036afba
15 changed files with 2748 additions and 0 deletions
1
.envrc
Normal file
1
.envrc
Normal file
|
@ -0,0 +1 @@
|
|||
use flake . --impure
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
/target
|
||||
.devenv
|
||||
.direnv
|
1
.pre-commit-config.yaml
Symbolic link
1
.pre-commit-config.yaml
Symbolic link
|
@ -0,0 +1 @@
|
|||
/nix/store/8lzsqxaphqkmr0awpvrqx37n8dwgq18g-pre-commit-config.json
|
2189
Cargo.lock
generated
Normal file
2189
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
5
Cargo.toml
Normal file
5
Cargo.toml
Normal file
|
@ -0,0 +1,5 @@
|
|||
[workspace]
|
||||
resolver = "2"
|
||||
members = [
|
||||
"crates/backend"
|
||||
]
|
5
build.rs
Normal file
5
build.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
// generated by `sqlx migrate build-script`
|
||||
fn main() {
|
||||
// trigger recompilation when a new migration is added
|
||||
println!("cargo:rerun-if-changed=migrations");
|
||||
}
|
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)
|
||||
}
|
292
flake.lock
Normal file
292
flake.lock
Normal file
|
@ -0,0 +1,292 @@
|
|||
{
|
||||
"nodes": {
|
||||
"crane": {
|
||||
"locked": {
|
||||
"lastModified": 1725409566,
|
||||
"narHash": "sha256-PrtLmqhM6UtJP7v7IGyzjBFhbG4eOAHT6LPYOFmYfbk=",
|
||||
"owner": "ipetkov",
|
||||
"repo": "crane",
|
||||
"rev": "7e4586bad4e3f8f97a9271def747cf58c4b68f3c",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "ipetkov",
|
||||
"repo": "crane",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"devenv": {
|
||||
"inputs": {
|
||||
"flake-compat": "flake-compat",
|
||||
"nix": "nix",
|
||||
"nixpkgs": "nixpkgs",
|
||||
"pre-commit-hooks": "pre-commit-hooks"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1688824367,
|
||||
"narHash": "sha256-qHxX0U8K+BvN+P1+ZhPuFMHRL2aCmQxSnXLzzNpTLD0=",
|
||||
"owner": "cachix",
|
||||
"repo": "devenv",
|
||||
"rev": "1e4701fb1f51f8e6fe3b0318fc2b80aed0761914",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "cachix",
|
||||
"repo": "devenv",
|
||||
"rev": "1e4701fb1f51f8e6fe3b0318fc2b80aed0761914",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"fenix": {
|
||||
"inputs": {
|
||||
"nixpkgs": [
|
||||
"nixpkgs"
|
||||
],
|
||||
"rust-analyzer-src": "rust-analyzer-src"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1726900127,
|
||||
"narHash": "sha256-v3r7yJY8YE4HAzD5DXOxLkzj8YZKQ0xuccp9yppGW1U=",
|
||||
"owner": "nix-community",
|
||||
"repo": "fenix",
|
||||
"rev": "18eefba7fd0bf03e115785948758a44125a9fd68",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-community",
|
||||
"repo": "fenix",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"flake-compat": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1696426674,
|
||||
"narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=",
|
||||
"owner": "edolstra",
|
||||
"repo": "flake-compat",
|
||||
"rev": "0f9255e01c2351cc7d116c072cb317785dd33b33",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "edolstra",
|
||||
"repo": "flake-compat",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"flake-utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1726560853,
|
||||
"narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"gitignore": {
|
||||
"inputs": {
|
||||
"nixpkgs": [
|
||||
"devenv",
|
||||
"pre-commit-hooks",
|
||||
"nixpkgs"
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1709087332,
|
||||
"narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=",
|
||||
"owner": "hercules-ci",
|
||||
"repo": "gitignore.nix",
|
||||
"rev": "637db329424fd7e46cf4185293b9cc8c88c95394",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "hercules-ci",
|
||||
"repo": "gitignore.nix",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"lowdown-src": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1726439114,
|
||||
"narHash": "sha256-/SsSoaw14Fxx3Fat/nAJ6gW2HhiXiCnhAmHoSah8EdQ=",
|
||||
"owner": "kristapsdz",
|
||||
"repo": "lowdown",
|
||||
"rev": "2c081b488217bc4b32a710e6ce353759ebb37120",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "kristapsdz",
|
||||
"repo": "lowdown",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nix": {
|
||||
"inputs": {
|
||||
"lowdown-src": "lowdown-src",
|
||||
"nixpkgs": [
|
||||
"devenv",
|
||||
"nixpkgs"
|
||||
],
|
||||
"nixpkgs-regression": "nixpkgs-regression"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1676545802,
|
||||
"narHash": "sha256-EK4rZ+Hd5hsvXnzSzk2ikhStJnD63odF7SzsQ8CuSPU=",
|
||||
"owner": "domenkozar",
|
||||
"repo": "nix",
|
||||
"rev": "7c91803598ffbcfe4a55c44ac6d49b2cf07a527f",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "domenkozar",
|
||||
"ref": "relaxed-flakes",
|
||||
"repo": "nix",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1726871744,
|
||||
"narHash": "sha256-V5LpfdHyQkUF7RfOaDPrZDP+oqz88lTJrMT1+stXNwo=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "a1d92660c6b3b7c26fb883500a80ea9d33321be2",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixpkgs-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs-regression": {
|
||||
"locked": {
|
||||
"lastModified": 1643052045,
|
||||
"narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs-stable": {
|
||||
"locked": {
|
||||
"lastModified": 1720386169,
|
||||
"narHash": "sha256-NGKVY4PjzwAa4upkGtAMz1npHGoRzWotlSnVlqI40mo=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "194846768975b7ad2c4988bdb82572c00222c0d7",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-24.05",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs_2": {
|
||||
"locked": {
|
||||
"lastModified": 1726871744,
|
||||
"narHash": "sha256-V5LpfdHyQkUF7RfOaDPrZDP+oqz88lTJrMT1+stXNwo=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "a1d92660c6b3b7c26fb883500a80ea9d33321be2",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixpkgs-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"pre-commit-hooks": {
|
||||
"inputs": {
|
||||
"flake-compat": [
|
||||
"devenv",
|
||||
"flake-compat"
|
||||
],
|
||||
"gitignore": "gitignore",
|
||||
"nixpkgs": [
|
||||
"devenv",
|
||||
"nixpkgs"
|
||||
],
|
||||
"nixpkgs-stable": "nixpkgs-stable"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1725513492,
|
||||
"narHash": "sha256-tyMUA6NgJSvvQuzB7A1Sf8+0XCHyfSPRx/b00o6K0uo=",
|
||||
"owner": "cachix",
|
||||
"repo": "pre-commit-hooks.nix",
|
||||
"rev": "7570de7b9b504cfe92025dd1be797bf546f66528",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "cachix",
|
||||
"repo": "pre-commit-hooks.nix",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"crane": "crane",
|
||||
"devenv": "devenv",
|
||||
"fenix": "fenix",
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs_2"
|
||||
}
|
||||
},
|
||||
"rust-analyzer-src": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1726443025,
|
||||
"narHash": "sha256-nCmG4NJpwI0IoIlYlwtDwVA49yuspA2E6OhfCOmiArQ=",
|
||||
"owner": "rust-lang",
|
||||
"repo": "rust-analyzer",
|
||||
"rev": "94b526fc86eaa0e90fb4d54a5ba6313aa1e9b269",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "rust-lang",
|
||||
"ref": "nightly",
|
||||
"repo": "rust-analyzer",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"systems": {
|
||||
"locked": {
|
||||
"lastModified": 1681028828,
|
||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
75
flake.nix
Normal file
75
flake.nix
Normal file
|
@ -0,0 +1,75 @@
|
|||
{
|
||||
inputs = {
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
||||
|
||||
crane = {
|
||||
url = "github:ipetkov/crane";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
fenix = {
|
||||
url = "github:nix-community/fenix";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
|
||||
devenv = {
|
||||
url = "github:cachix/devenv/1e4701fb1f51f8e6fe3b0318fc2b80aed0761914";
|
||||
};
|
||||
};
|
||||
|
||||
outputs = {
|
||||
self,
|
||||
nixpkgs,
|
||||
crane,
|
||||
fenix,
|
||||
flake-utils,
|
||||
devenv,
|
||||
...
|
||||
} @ inputs:
|
||||
flake-utils.lib.eachDefaultSystem (system: let
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
rs-toolchain = with fenix.packages.${system};
|
||||
combine [
|
||||
complete.toolchain
|
||||
];
|
||||
in {
|
||||
# devShells.default = pkgs.mkShell rec {
|
||||
# buildInputs = with pkgs; [
|
||||
# ];
|
||||
|
||||
# LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath buildInputs;
|
||||
# };
|
||||
|
||||
devShells.default = devenv.lib.mkShell {
|
||||
inherit inputs pkgs;
|
||||
modules = [
|
||||
({pkgs, ...}: {
|
||||
packages = with pkgs; [
|
||||
rs-toolchain
|
||||
sqlx-cli
|
||||
libpqxx
|
||||
cargo-watch
|
||||
bacon
|
||||
];
|
||||
|
||||
languages.rust.enable = true;
|
||||
|
||||
pre-commit.hooks = {
|
||||
clippy.enable = true;
|
||||
rustfmt.enable = true;
|
||||
};
|
||||
|
||||
services.postgres = {
|
||||
enable = true;
|
||||
listen_addresses = "127.0.0.1";
|
||||
};
|
||||
|
||||
env = {
|
||||
DATABASE_URL = "postgres://localhost/chatdings";
|
||||
};
|
||||
})
|
||||
];
|
||||
};
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue