Project Structure

v0.4.8Updated for nself v0.4.8

Understanding the nself project structure helps you customize and extend your backend effectively. Each file and directory has a specific purpose in the orchestration of your services.

Complete Project Directory Tree

project-root/                          # Your project directory
│
├── _backup/                           # Auto-backups from nself operations
│   └── [timestamps]/                  # Timestamped backup folders
│
├── auth/                              # nHost Auth service configuration
│   └── config/                        # Auth service config files
│
├── functions/                         # Serverless functions directory
│   ├── dist/                          # Compiled functions output
│   └── src/                           # Function source code
│
├── hasura/                            # Hasura GraphQL engine configuration
│   ├── metadata/                      # Hasura metadata (tables, relationships)
│   └── migrations/                    # Database migrations managed by Hasura
│
├── logs/                              # Application and service logs
│
├── monitoring/                        # Observability stack configuration
│   ├── grafana/                       # Grafana dashboards and provisioning
│   ├── loki/                          # Loki log aggregation config
│   ├── prometheus/                    # Prometheus metrics collection config
│   ├── promtail/                      # Promtail log shipper config
│   └── tempo/                         # Tempo distributed tracing config
│
├── nginx/                             # Nginx reverse proxy configuration
│   ├── conf.d/                        # Individual service proxy configs
│   ├── ssl/                           # SSL certificate symlinks
│   └── nginx.conf                     # Main Nginx configuration
│
├── postgres/                          # PostgreSQL database configuration
│   └── init/                          # Database initialization scripts
│       ├── 00-init.sql                # Initial database and user setup
│       ├── 01-extensions.sql          # Enable PostgreSQL extensions
│       ├── 02-schemas.sql             # Create database schemas
│       ├── 10-hasura.sql              # Hasura-specific setup
│       └── 20-auth.sql                # Auth service database setup
│
├── services/                          # Custom backend services
│   ├── bullmq_worker/                 # BullMQ job queue worker service
│   ├── express_api/                   # Express.js REST API service
│   ├── go_grpc/                       # Go gRPC service
│   └── python_api/                    # Python FastAPI service
│
├── ssl/                               # SSL certificates storage
│   └── certificates/
│       ├── localhost/                 # Local dev certificates
│       └── [domain]/                  # Production domain certificates
│
├── storage/                           # MinIO/S3 file storage
│   ├── temp/                          # Temporary file uploads
│   └── uploads/                       # Persistent file storage
│
├── .volumes/                          # Docker volume mount points (hidden)
│   ├── postgres/                      # PostgreSQL data persistence
│   ├── redis/                         # Redis data persistence
│   └── minio/                         # MinIO object storage data
│
├── docker-compose.yml                 # Main Docker Compose orchestration file
├── .env                               # Active environment configuration
├── .env.dev                           # Development environment settings
├── .env.staging                       # Staging environment settings
├── .env.prod                          # Production environment settings
├── .env.example                       # Example environment template
└── .gitignore                         # Git ignore rules

Directory Descriptions

Core Infrastructure

_backup/ - Automatic backups created by nself during operations. Each timestamped folder contains previous versions of configuration files, useful for rollback scenarios.

.volumes/ - Docker volume mount points for persistent data storage. This hidden directory contains subdirectories for each stateful service (PostgreSQL, Redis, MinIO) ensuring data persists across container restarts.

Service Configuration

auth/ - Configuration for the authentication service, including JWT settings, OAuth providers, email templates, and security rules.

hasura/ - GraphQL engine configuration containing:

  • metadata/ - Table definitions, relationships, permissions, and remote schemas
  • migrations/ - Versioned database schema changes

functions/ - Serverless functions that extend your backend capabilities:

  • src/ - TypeScript/JavaScript source code
  • dist/ - Compiled production-ready functions

Custom Services

services/ - Your custom backend services generated from templates:

  • Each subdirectory represents a microservice
  • Contains Dockerfile, source code, and configuration
  • Services can be in any language (Node.js, Python, Go, Rust, etc.)
  • Automatically integrated with the rest of the stack

Routing and Networking

nginx/ - Reverse proxy configuration that routes all traffic:

  • conf.d/ - Individual service routing rules
  • ssl/ - Symbolic links to active SSL certificates
  • nginx.conf - Main configuration with security headers

ssl/certificates/ - SSL/TLS certificates for HTTPS:

  • localhost/ - Self-signed certificates for local development
  • Domain-specific folders for staging/production certificates

Data Layer

postgres/init/ - Database initialization scripts executed in order:

  • 00-init.sql - Database and user creation
  • 01-extensions.sql - Enable extensions (uuid, pgcrypto, postgis, etc.)
  • 02-schemas.sql - Create logical schemas for service isolation
  • 10-hasura.sql - Hasura-specific tables and functions
  • 20-auth.sql - Authentication service schema

storage/ - File storage managed by MinIO (S3-compatible):

  • uploads/ - User-uploaded files
  • temp/ - Temporary files cleared periodically

Observability

monitoring/ - Complete observability stack configuration:

  • grafana/ - Dashboards and data source provisioning
  • prometheus/ - Metrics collection rules and targets
  • loki/ - Log aggregation configuration
  • tempo/ - Distributed tracing setup
  • promtail/ - Log shipping from containers to Loki

Environment Files

nself supports multiple environment configurations:

.env                  # Active configuration (symlink or copy)
.env.dev              # Development settings (local development)
.env.staging          # Staging settings (pre-production testing)
.env.prod             # Production settings (live environment)
.env.example          # Template with all available options

Environment Selection

  • Development: Uses .env.dev with debug enabled
  • Staging: Uses .env.staging with production-like settings
  • Production: Uses .env.prod with security hardening

Key Concepts

Service Isolation

Each service runs in its own container with isolated:

  • Network namespace (communication via Docker network)
  • File system (only specified volumes are shared)
  • Process space (independent process trees)
  • Resource limits (CPU/memory constraints)

Data Persistence

Stateful data is stored in .volumes/ ensuring:

  • Data survives container restarts
  • Easy backup and restoration
  • Consistent permissions and ownership

Common Operations

Adding a New Custom Service

# Define in .env file using CS_N variables:
CS_1=my_service:express-js:8001

# Run build to generate from template
nself build

# Customize code in services/my_service/
# Rebuild with nself start

Best Practices

  • Version Control: Commit everything except .env, .volumes/, and ssl/certificates/
  • Secrets: Never commit passwords or API keys
  • Backups: Monitor _backup/ size and clean old backups periodically
  • Migrations: Use numbered prefixes and organize chronologically
  • Testing: Use separate .env.test for testing
  • Documentation: Document service dependencies in service README files

Stack Overview

A complete nself deployment includes 30+ integrated services:

Core Services

  • PostgreSQL - Primary database with 60+ extensions
  • Hasura - GraphQL API engine
  • Auth - Authentication and authorization
  • Storage - MinIO S3-compatible object storage
  • Nginx - Reverse proxy and SSL termination

Optional Services

  • Redis - Caching and session storage
  • BullMQ - Job queue and background processing
  • MeiliSearch - Full-text search engine
  • MailPit - Email testing (dev) / SMTP (prod)
  • Functions - Serverless compute

Monitoring Stack

  • Prometheus - Metrics collection
  • Grafana - Visualization and dashboards
  • Loki - Log aggregation
  • Tempo - Distributed tracing
  • Alertmanager - Alert routing and management

Next Steps