commit fe38d42a9eaf8e2f26f4432754c0ecc5455d707f Author: eiiko6 Date: Sun Dec 14 17:30:08 2025 +0100 base service with authentication and user creation diff --git a/db/Dockerfile b/db/Dockerfile new file mode 100644 index 0000000..39d085a --- /dev/null +++ b/db/Dockerfile @@ -0,0 +1,13 @@ +FROM docker.io/library/postgres:15 + +# Set environment variables +ENV POSTGRES_USER=chatapp +ENV POSTGRES_PASSWORD=secret +ENV POSTGRES_DB=chatapp + +# Copy the initialization SQL script +COPY init.sql /docker-entrypoint-initdb.d/ +COPY mock_data.sql /docker-entrypoint-initdb.d/ + +# Expose port 5432 +EXPOSE 5432 diff --git a/db/init.sql b/db/init.sql new file mode 100644 index 0000000..dc75867 --- /dev/null +++ b/db/init.sql @@ -0,0 +1,26 @@ +CREATE TABLE IF NOT EXISTS user ( + id SERIAL PRIMARY KEY, + email TEXT UNIQUE, + username TEXT NOT NULL UNIQUE, + password_hash TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS channel ( + id SERIAL PRIMARY KEY, + owner INT NOT NULL REFERENCES user(id) ON DELETE CASCADE, + name TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS membership ( + user INT REFERENCES user(id), + channel INT REFERENCES channel(id), + PRIMARY KEY (user, channel) +); + +CREATE TABLE IF NOT EXISTS message ( + id BIGSERIAL PRIMARY KEY, + sender INT REFERENCES user(id) NOT NULL, + channel INT REFERENCES channel(id) NOT NULL, + type VARCHAR(32) NOT NULL, + content TEXT NOT NULL +); diff --git a/db/mock_data.sql b/db/mock_data.sql new file mode 100644 index 0000000..a05d0ce --- /dev/null +++ b/db/mock_data.sql @@ -0,0 +1,24 @@ +INSERT INTO user (username, password_hash, email) VALUES +('alice', '$argon2id$v=19$m=19456,t=2,p=1$W0OzC/dmZQt7/xUJt4E9hA$cYiUC91a5yCQU9tDUadw0FKjUmTRv453cYwu1nfMKUQ', 'alice@example.com'), +('bob', '$argon2id$v=19$m=19456,t=2,p=1$1T7VaQps1X5Wj+TJHt8FIQ$/hA7PSITskjELwfNw+s6BvCJmUA4dDDrSGJvDvHx7Kc', 'bob@example.com'), +('carol', '$argon2id$v=19$m=19456,t=2,p=1$Kw4Re4lggxzDldu3vNl2PA$6DP4MPftfXI77g8EZRXYmWgcnVnAKLq0dkZOb/eBIC8', 'carol@example.com'); + +INSERT INTO channel (owner, name) VALUES +(1, 'General Discussion'), +(2, 'Tech Talk'), +(1, 'Random Memes'); + +INSERT INTO membership (user, channel) VALUES +(1, 1), -- Alice in General Discussion +(2, 1), -- Bob in General Discussion +(2, 2), -- Bob in Tech Talk +(3, 1), -- Carol in General Discussion +(1, 3); -- Alice in Random Memes + +INSERT INTO message (sender, channel, type, content) VALUES +(1, 1, 'text', 'Hey everyone, hows it going?'), +(2, 1, 'text', 'All good! Just trying to get through some work.'), +(3, 1, 'text', 'Hello! How are you guys?'), +(2, 2, 'text', 'Anyone seen the new tech updates?'), +(1, 3, 'image', 'Heres a funny meme I found!'), +(3, 1, 'text', 'I love how active this channel is!'); diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..4452510 --- /dev/null +++ b/flake.nix @@ -0,0 +1,41 @@ +{ + description = "chatapp"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + rust-overlay.url = "github:oxalica/rust-overlay"; + }; + + outputs = { nixpkgs, flake-utils, rust-overlay, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + overlays = [ rust-overlay.overlays.default ]; + pkgs = import nixpkgs { inherit system overlays; }; + rust = pkgs.rust-bin.stable.latest.default; + openssl = pkgs.openssl; + in { + packages.default = pkgs.rustPlatform.buildRustPackage { + pname = "-server"; + version = "0.1.0"; + + src = ./.; + cargoLock = { lockFile = ./Cargo.lock; }; + + nativeBuildInputs = [ rust pkgs.pkg-config ]; + + buildInputs = [ openssl ]; + + OPENSSL_LIB_DIR = "${openssl.out}/lib"; + OPENSSL_INCLUDE_DIR = "${openssl.dev}/include"; + PKG_CONFIG_PATH = "${openssl.dev}/lib/pkgconfig"; + }; + + devShells.default = pkgs.mkShell { + packages = + [ rust pkgs.cargo pkgs.rust-analyzer pkgs.pkg-config openssl ]; + OPENSSL_DIR = openssl.dev; + PKG_CONFIG_PATH = "${openssl.dev}/lib/pkgconfig"; + }; + }); +} diff --git a/run_db.sh b/run_db.sh new file mode 100755 index 0000000..694b068 --- /dev/null +++ b/run_db.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env sh +podman build -t chatapp-db ./db +podman run --replace -d \ + --name chatapp-db \ + -p 5432:5432 \ + chatapp-db