Flake to setup a local env for atproto development
at main 1.9 kB view raw
1{ pkgs, did-plc-server }: 2 3pkgs.writeShellScriptBin "plc" '' 4 set -e 5 6 echo "Starting PLC services (PostgreSQL + DID PLC Server)..." 7 8 # Set PostgreSQL environment 9 export PGHOST="localhost" 10 export PGDATA="./data/plc/db" 11 export PGPORT="5433" 12 export PGUSER="$(whoami)" 13 14 # Create data directory 15 mkdir -p "$PGDATA" 16 17 echo "Using PostgreSQL at $PGDATA" 18 19 # Initialize PostgreSQL data directory if it doesn't exist 20 if [ ! -d "$PGDATA/base" ]; then 21 echo "Initializing PostgreSQL database..." 22 ${pkgs.postgresql}/bin/pg_ctl initdb -D "$PGDATA" -o "--auth-local=trust --auth-host=trust" 23 24 # Add some logging configuration 25 echo "log_min_messages = warning" >> "$PGDATA/postgresql.conf" 26 echo "log_min_error_statement = error" >> "$PGDATA/postgresql.conf" 27 echo "log_connections = on" >> "$PGDATA/postgresql.conf" 28 echo "log_disconnections = on" >> "$PGDATA/postgresql.conf" 29 fi 30 31 # Start PostgreSQL server 32 echo "Starting PostgreSQL on port $PGPORT..." 33 ${pkgs.postgresql}/bin/pg_ctl -D "$PGDATA" -o "-p $PGPORT --unix_socket_directories='$PWD'" start 34 35 # Wait for PostgreSQL to be ready 36 echo "Waiting for PostgreSQL to be ready..." 37 until ${pkgs.postgresql}/bin/pg_isready -p "$PGPORT" -h localhost; do 38 sleep 1 39 done 40 41 # Create database if it doesn't exist 42 ${pkgs.postgresql}/bin/createdb -p "$PGPORT" -h localhost plc 2>/dev/null || true 43 44 # Set DATABASE_URL 45 export DATABASE_URL="postgresql://$PGUSER@localhost:$PGPORT/plc" 46 47 echo "PostgreSQL ready at: $DATABASE_URL" 48 echo "Starting DID PLC Server..." 49 50 # Cleanup function using pg_ctl 51 cleanup() { 52 echo "Shutting down PostgreSQL..." 53 ${pkgs.postgresql}/bin/pg_ctl -D "$PGDATA" stop -m fast 2>/dev/null || true 54 } 55 56 # Set trap to cleanup on exit 57 trap cleanup EXIT INT TERM 58 59 # Start the PLC server (don't use exec so cleanup trap works) 60 ${did-plc-server}/bin/did-plc-server 61''