DEV Community

Francis Oyakhire
Francis Oyakhire

Posted on

Postiz Self Host Bluesky Python Client

This week's news about supply chain vulnerabilities in Microsoft's Agent Framework has sparked renewed interest in secure, self-hosted alternatives for critical infrastructure. As we explore the importance of control and transparency in our tech stack, we're also looking at how to build resilient, self-hosted solutions like Postiz, a self-hosted Bluesky-compatible social platform.

We've been experimenting with self-hosting Postiz using Docker Compose and driving it from a Python client. This setup allows us to manage our own data, avoid third-party dependencies, and ensure compliance with internal security policies. The Python client communicates with Postiz's API endpoint at /api/public/v1/posts, which is central to our implementation.

Our Docker Compose setup includes Postiz, a PostgreSQL database, and a reverse proxy. We use Docker Compose to manage the services, ensuring that all components are version-controlled and easily reproducible. Here's a simplified version of our docker-compose.yml:

version: '3.8'
services:
  postiz:
    image: postiz/postiz:latest
    ports:
      - "8080:8080"
    environment:
      POSTIZ_DB_HOST: postgres
      POSTIZ_DB_PORT: 5432
      POSTIZ_DB_NAME: postiz
      POSTIZ_DB_USER: postiz
      POSTIZ_DB_PASSWORD: postiz
    depends_on:
      - postgres
  postgres:
    image: postgres:14
    environment:
      POSTGRES_USER: postiz
      POSTGRES_PASSWORD: postiz
      POSTGRES_DB: postiz
    volumes:
      - postgres_data:/var/lib/postgresql/data
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
volumes:
  postgres_data:
Enter fullscreen mode Exit fullscreen mode

This configuration ensures that all services are running in isolated containers, with clear separation between the application, database, and network layers. The Nginx reverse proxy helps manage incoming traffic and secure the API endpoints.

When it comes to the Python client, we've implemented a simple HTTP client that sends JSON payloads to the /api/public/v1/posts endpoint. The client handles authentication via a Bearer token in the Authorization header. Here's a sample of the JSON payload we use for creating a post:

{
  "text": "This is a sample post from our Python client.",
  "visibility": "public"
}
Enter fullscreen mode Exit fullscreen mode

The Authorization header is set as Bearer <token>, where <token> is a valid JWT token issued by Postiz's authentication service. On successful requests, Postiz may return either a single object or a list of objects, depending on the context. To handle this, our client checks the response type and processes it accordingly. For example:

import requests

def create_post(text, token):
    url = "http://localhost:8080/api/public/v1/posts"
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }
    payload = {
        "text": text,
        "visibility": "public"
    }
    response = requests.post(url, headers=headers, json=payload)
    if response.status_code == 200:
        data = response.json()
        if isinstance(data, list):
            return data[0] if data else None
        return data
    return None
Enter fullscreen mode Exit fullscreen mode

This approach ensures that our client can gracefully handle both response formats and extract the relevant data. We've also implemented retries and error handling to deal with transient failures and unexpected responses.

One of the key tradeoffs we've made is the complexity of managing the Docker Compose setup and ensuring that all components are correctly configured. While this gives us full control, it also increases the maintenance burden. Additionally, the Python client requires careful handling of authentication and response parsing, which adds to the development time.

Looking ahead, we're exploring ways to automate the deployment of Postiz and reduce the manual configuration required. We're also looking into integrating more advanced features like media uploads and real-time notifications. What are your thoughts on self-hosted alternatives for social platforms like Bluesky? How do you balance control with complexity in your infrastructure choices?

Top comments (0)