Rails 8 Production-Ready API Starter β for startups, hackathons, or clean backend projects.
Overview β’ Quick Start β’ Architecture β’ API Documentation β’ Development & Testing
- Production-Grade Architecture: Built with scalability and maintainability in mind
- Modern Development Flow: Docker-based development and deployment
- Security First: JWT authentication, role-based authorization, and comprehensive security measures
- Developer Experience: Hot-reloading, debugging tools, and extensive documentation
- Best Practices: SOLID principles, clean code, and comprehensive testing
- CI/CD Ready: GitHub Actions workflow with security scanning and automated testing
-
π Security
- JWT stateless authentication
- Role-based authorization (Admin/User)
- CORS protection via Rack
- Brakeman security scans
- Secure headers & password encryption
-
ποΈ API Architecture
- RESTful API structure (v1)
- JSON serialization with ActiveModelSerializers
- Clean controller logic with concerns
- Policy-based permissions
-
π₯ User Management
- JWT authentication flow
- Role-based access control
- Profile management
- Ruby 3.3.0
- Rails 8.0.2
- PostgreSQL 14 or later
- Bundler 2.5 or later
- Docker (optional)
# Clone the repository
git clone https://github.com/y2-znt/rails-api-starter.git
cd rails-api-starter
# Start the development environment
docker compose -f docker-compose-dev.yml up -d --build
# Setup the database and run migrations
docker compose -f docker-compose-dev.yml exec rails-api-dev bundle exec rails db:setup db:migrate
# Run tests to verify setup
docker compose -f docker-compose-dev.yml exec rails-api-dev bundle exec rails test
# Clone the repository
git clone https://github.com/y2-znt/rails-api-starter.git
cd rails-api-starter
# Install dependencies
bundle install
# Setup database
cp config/database.yml.example config/database.yml
rails db:setup
rails db:migrate
# Run tests to verify setup
rails test
# Start the server
rails server
Visit http://localhost:4000
- You're ready to go! π
This project follows a classic Rails API structure with a versioned /api/v1
namespace:
app/controllers/api/v1
: API endpoints and business logicapp/models
: Active Record models and validationsapp/policies
: Pundit authorization policiesapp/serializers
: JSON response formattinglib/
: Reusable services and utilitiesconfig/
: Application configurationdb/
: Database migrations and schema
- Framework: Ruby on Rails 8 (API mode)
- Database: PostgreSQL 14
- Authentication: JWT (stateless)
- Authorization: Pundit
- API Serialization: ActiveModelSerializers
- Development Tools:
- RuboCop Rails Omakase for code style
- Annotaterb for model documentation
- Overcommit for Git hooks
- Security:
- Bcrypt for password hashing
- Rack CORS for CORS handling
- Brakeman for security scanning
Endpoint | Method | Description | Response (Success) |
---|---|---|---|
/api/v1/auth/register |
POST | Register new user | 201 Created + JWT token |
/api/v1/auth/login |
POST | Login | 200 OK + JWT token |
/api/v1/auth/logout |
POST | Logout | 204 No Content |
Endpoint | Method | Auth | Admin | Description |
---|---|---|---|---|
/api/v1/users |
GET | β | β | List all users |
/api/v1/users |
POST | β | β | Create a user |
/api/v1/users/:id |
GET | β | π« | Get user details |
/api/v1/users/:id |
PUT | β | π« | Update user |
/api/v1/users/:id |
DELETE | β | β | Delete user |
Endpoint | Method | Description | Auth | Response |
---|---|---|---|---|
/api/v1/me |
GET | Get own profile | β | 200 OK |
/api/v1/me |
PATCH | Update profile | β | 204 No Content |
/api/v1/me |
DELETE | Delete own account | β | 204 No Content |
We use a comprehensive testing setup:
- MiniTest: Main testing framework
- FactoryBot: Test data generation
- Shoulda Matchers & Context: Enhanced test assertions and contexts
- 100% test coverage goal
# Run all tests
rails test
# Run specific tests
rails test test/models/user_test.rb
rails test test/models/user_test.rb:42
- Brakeman: Security analysis
- RuboCop: Code style enforcement
- Annotate: Model documentation
- Bundle Audit: Dependency scanning
# Security scan
bin/brakeman --no-pager
# Lint code
bin/rubocop -f github
This project uses Overcommit to enforce code quality and project consistency through Git hooks.
Before starting development, make sure to set it up properly:
# Install Git hooks
bundle exec overcommit --install
# Sign the configuration file (required once)
bundle exec overcommit --sign
You can also manually run all hooks:
bundle exec overcommit --run
The project includes the following pre-commit hooks:
- RuboCop: Automatically checks and corrects Ruby code style
- Runs with
--autocorrect
flag to fix simple issues - Required to pass before commit
- Uses Rails Omakase style guide
- Runs with
This project includes a complete GitHub Actions pipeline:
Stage | Tools |
---|---|
π Security | Brakeman, Bundle Audit |
π¨ Lint | RuboCop Rails Omakase |
π§ͺ Tests | MiniTest, PostgreSQL |
Built with β€οΈ using Ruby on Rails 8