Files
privatestudio/README_SERVE.md
T
Shaun KrugerandCopilot 9f9fce7a6e Add comprehensive ps-serve command guide
Includes:
- Installation instructions for testing in a virtualenv
- Detailed reference for all ps-serve commands (init, create-user, create-key, serve)
- Complete workflow examples
- Troubleshooting guide
- Development tips and security best practices
- Quick reference cheat sheet

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Update readme

Add userinfo endpoint and asset backup script.

Fix s3 and add new DB fields

Add pyproject file

Cleanup
2026-11-22 15:27:00 -07:00

11 KiB

ps-serve Command Guide

A comprehensive guide to using the ps-serve command-line interface for managing the Private Studio video transcoding service.

Overview

ps-serve is the command-line tool for Private Studio. It provides commands to:

  • Initialize the database
  • Create and manage user accounts
  • Generate and manage API keys
  • Start the web server

Installation for Testing

Quick Setup in a Virtual Environment

If you want to test the web service locally, here's the fastest way to get started:

1. Clone the Repository

git clone https://github.com/yourusername/privatestudio.git
cd privatestudio

2. Create a Virtual Environment

python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

3. Install in Development Mode

pip install -e ".[dev]"

This installs the package in "editable" mode, which means:

  • Changes to the code are immediately reflected
  • All dependencies (FastAPI, SQLAlchemy, etc.) are installed
  • Development tools (pytest, httpx) are also installed

4. Verify Installation

ps-serve --help

You should see the help output showing all available commands.

Virtual Environment Location

Keep your virtual environment separate from the source code:

# Recommended structure
~/projects/privatestudio/
├── venv/                 # Virtual environment (git-ignored)
├── privatestudio/        # Source code
├── tests/
├── pyproject.toml
└── README.md

Updating Your Installation

When you pull new changes from git:

# Activate your virtual environment
source venv/bin/activate

# Reinstall to pick up new dependencies
pip install -e ".[dev]"

# Run tests to verify everything works
pytest tests/test_web.py -v

Commands Reference

ps-serve init

Initialize the database for a new installation.

Syntax:

ps-serve init

What it does:

  • Creates a transcoding.db SQLite database file in the current directory
  • Creates all tables (users, api_keys, assets, jobs, profiles, etc.)
  • Safe to run multiple times (idempotent)

Example:

$ mkdir ~/transcoding-server && cd ~/transcoding-server
$ ps-serve init
Database initialized
$ ls
transcoding.db

Environment Variable:

  • DATABASE_URL: Path to database (default: ./transcoding.db)
DATABASE_URL="sqlite:///var/lib/privatestudio/transcoding.db" ps-serve init

ps-serve create-user

Create a new user account for API access.

Syntax:

ps-serve create-user <username>

Arguments:

  • <username>: The username for the new account (must be unique)

What it does:

  • Creates a new user in the database
  • Returns the user ID (needed for creating API keys)
  • Users can have multiple API keys

Examples:

$ ps-serve create-user alice
User created: alice (ID: 1)

$ ps-serve create-user bob
User created: bob (ID: 2)

Error Handling:

$ ps-serve create-user alice
Error: User 'alice' already exists

Output Format: The command prints the username and user ID, which you'll need for the next step.


ps-serve create-key

Generate a new API key for a user.

Syntax:

ps-serve create-key <user_name>

Arguments:

  • <user_name>: The username of the user (obtained from create-user output)

What it does:

  • Generates a random 32-character API key
  • Stores the SHA256 hash in the database
  • Prints the plain key (save this somewhere safe!)
  • The key cannot be recovered later—users must create a new one if lost

Example:

$ ps-serve create-key alice
API key created for user alice
Key: 4YXDJoXf26auwGFQPKB6aB-kY6HxbkKPJzmWFbs7cnM
Keep this key safe - you won't be able to see it again!

Security Notes:

  • The key is only displayed once
  • Store it securely (environment variable, secret manager, encrypted file)
  • Never commit it to version control
  • Users can have multiple active keys (useful for key rotation)

Example Workflow:

# Create two keys for the same user (one for testing, one for production)
$ ps-serve create-key alice
Key: test_key_abc123...

$ ps-serve create-key alice
Key: prod_key_xyz789...

ps-serve serve

Start the FastAPI web server.

Syntax:

ps-serve serve [OPTIONS]

Options:

--host HOST              Server host address (default: 0.0.0.0)
--port PORT              Server port (default: 8000)
--reload                 Enable auto-reload on code changes (for development)
--log-level LEVEL        Log level: debug, info, warning, error (default: info)

What it does:

  • Starts the FastAPI server
  • Initializes the database if it doesn't exist
  • Listens for HTTP requests
  • Logs all requests and errors

Examples:

Start with defaults:

$ ps-serve serve
INFO:     Application startup complete
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)

Development mode with auto-reload:

ps-serve serve --reload

Changes to Python files will automatically restart the server.

Custom host and port:

ps-serve serve --host 127.0.0.1 --port 5000

Now the server is only accessible locally at http://127.0.0.1:5000

Detailed logging:

ps-serve serve --log-level debug

All options together:

ps-serve serve --host localhost --port 8080 --reload --log-level debug

Environment Variables: These can also be set via environment variables:

  • SERVER_HOST: Server host (default: 0.0.0.0)
  • SERVER_PORT: Server port (default: 8000)
  • DATABASE_URL: Database path (default: ./transcoding.db)
  • STORAGE_PATH: File storage directory (default: ./storage)
  • SQL_ECHO: Log all SQL queries (true/false)

Example:

export SERVER_HOST=localhost
export SERVER_PORT=9000
export STORAGE_PATH=/media/transcoding
ps-serve serve

Complete Workflow

Here's a typical setup workflow from start to finish:

1. Initialize Database

mkdir ~/my-transcoding-server
cd ~/my-transcoding-server
ps-serve init

2. Create Users

ps-serve create-user alice
ps-serve create-user bob

3. Create API Keys

ps-serve create-key alice  # For alice
# Save this key somewhere secure
ps-serve create-key bob  # For bob

4. Start the Server

ps-serve serve

5. Use the API

In another terminal:

# Upload a video
curl -X PUT \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -F "file=@myvideo.mp4" \
  http://localhost:8000/s3/1/videos/myvideo.mp4

# List uploaded files
curl -H "Authorization: Bearer YOUR_API_KEY" \
  http://localhost:8000/s3/1

Troubleshooting

Command not found: ps-serve

Problem: ps-serve: command not found

Solution: Make sure you've activated the virtual environment and installed the package:

source venv/bin/activate
pip install -e .

Database locked error

Problem: sqlite3.OperationalError: database is locked

Solution: SQLite doesn't handle concurrent writes well. For production:

  1. Stop any running servers
  2. Use PostgreSQL instead (change DATABASE_URL)
  3. Reduce concurrency

Port already in use

Problem: OSError: [Errno 48] Address already in use

Solution: Use a different port:

ps-serve serve --port 8001

Or kill the existing process:

# Find the process
lsof -i :8000
# Kill it
kill -9 <PID>

API key not working

Problem: 401 Unauthorized: Invalid API key

Possible causes:

  1. Wrong API key
  2. API key was revoked
  3. Key header is malformed (must be Authorization: Bearer KEY)

Debug:

# List all keys for a user
curl -H "Authorization: Bearer YOUR_KEY" \
  http://localhost:8000/api/keys

Running Tests

After making changes, run the test suite to ensure everything works:

# Activate virtual environment
source venv/bin/activate

# Run all tests
pytest tests/test_web.py -v

# Run a specific test
pytest tests/test_web.py::test_upload_file -v

# Run with coverage
pytest tests/test_web.py --cov=privatestudio.web --cov-report=html

Coverage report is generated in htmlcov/index.html


Development Tips

1. Auto-reload During Development

ps-serve serve --reload

Every time you save a Python file, the server will restart automatically.

2. View SQL Queries

export SQL_ECHO=true
ps-serve serve

This logs all database queries to stdout for debugging.

3. Use a Specific Database

export DATABASE_URL="sqlite:///./test_db.db"
ps-serve init
ps-serve serve

Useful for keeping multiple database instances (dev, test, prod).

4. Store Configuration in .env

Create a .env file:

# .env
DATABASE_URL=sqlite:///./transcoding.db
STORAGE_PATH=./media
SERVER_HOST=localhost
SERVER_PORT=8000
SQL_ECHO=false

Then source it before running commands:

source .env
ps-serve serve

Or use a tool like python-dotenv:

pip install python-dotenv

Then in your code or shell initialization.


Security Best Practices

  1. Protect API Keys

    • Never commit keys to version control
    • Store in environment variables or secure vaults
    • Rotate keys regularly
  2. Database Security

    • Use PostgreSQL for production (not SQLite)
    • Enable database backups
    • Run database on a separate server
  3. Network Security

    • Use HTTPS in production (behind a reverse proxy like nginx)
    • Bind to localhost if only local access is needed
    • Implement rate limiting and authentication
  4. File Storage

    • Keep storage directory outside the web root
    • Use a dedicated disk or object storage (S3, MinIO) for production
    • Implement backup strategy

Next Steps

After getting ps-serve running:

  1. Upload Your First Video

    • See DEPLOYMENT.md for S3 API examples
  2. Create a Transcode Profile

    • Define encoding profiles in YAML
    • Request transcode jobs
  3. Integrate with Your Application

    • Use the REST API from your application
    • Handle job status polling
  4. Deploy to Production

    • Use a real database (PostgreSQL)
    • Run behind nginx or similar proxy
    • Set up monitoring and backups

Getting Help

For more detailed information:

  • API Documentation: Start the server and visit http://localhost:8000/docs
  • Deployment Guide: See README.md
  • Source Code: Check privatestudio/web/ for implementation details

Quick Reference Cheat Sheet

# Setup
python3 -m venv venv
source venv/bin/activate
pip install -e ".[dev]"

# First time
ps-serve init
ps-serve create-user myuser
ps-serve create-key 1

# Development
ps-serve serve --reload --log-level debug

# Testing
pytest tests/test_web.py -v

# Production
DATABASE_URL="postgresql://user:pass@localhost/psdb" \
SERVER_HOST=0.0.0.0 \
SERVER_PORT=8000 \
ps-serve serve