A Swift-based REST API server for habit tracking, built with the Vapor web framework.
- Framework: Vapor 4 (Swift server-side framework)
- ORM: Fluent
- Authentication: JWT
- Databases:
- PostgreSQL (production)
- SQLite (development)
- Swift Version: 6.2+
- Platform: macOS 13+
- Shared DTOs: GrowBitSharedDTO (external package)
- Secure JWT authentication
- Protected API routes
- User registration and management
- Habit and category CRUD operations
- RESTful API design
POST /api/register- User registration ✅POST /api/login- User login ✅
POST /api/:userId/categories- Create new category ✅GET /api/:userId/categories- Get all categories for user ✅DELETE /api/:userId/categories/:categoryId- Delete category ✅
POST /api/refresh- Refresh JWT tokenPOST /api/logout- User logout
PUT /api/:userId/categories/:id- Update category
GET /api/habits- Get all habitsPOST /api/habits- Create new habitPUT /api/habits/:id- Update habitDELETE /api/habits/:id- Delete habit
POST /api/entries- Mark habit completionDELETE /api/entries/:id- Remove habit completionGET /api/entries/calendar/:month- Get monthly calendar data
- Swift 6.2+ installed on your system
- Xcode (for macOS development)
- Docker (optional, for containerized deployment)
- Clone the repository:
git clone https://github.com/dmakarau/GrowBit-API-Server.git
cd GrowBit-API-Server- Resolve dependencies:
swift package resolve- Set up environment variables (create
.envfile):
DATABASE_URL=your_database_url_here
JWT_SECRET=your_jwt_secret_hereImportant: Generate a secure JWT secret using:
openssl rand -base64 32swift run GrowBitAppServer serve --hostname 0.0.0.0 --port 8080# Build the image
docker compose build
# Start the server
docker compose up app
# Stop all services
docker compose downRun the test suite:
swift testGrowBit-API-Server/
├── Package.swift # Swift Package Manager configuration
├── Sources/
│ └── GrowBitAppServer/
│ ├── entrypoint.swift # Application entry point
│ ├── configure.swift # Application configuration
│ ├── routes.swift # Route definitions
│ ├── Controllers/ # API controllers
│ │ ├── UserController.swift # User registration/auth controller
│ │ └── HabitsController.swift # Habits and categories controller
│ ├── Models/ # Data models
│ │ ├── User.swift # User model with validation
│ │ ├── Category.swift # Category model
│ │ └── AuthPayload.swift # JWT payload structure
│ ├── Extensions/ # Protocol conformances for shared types
│ │ ├── RegisterResponseDTO+Extensions.swift # Vapor Content conformance
│ │ ├── LoginResponseDTO+Extensions.swift # Vapor Content conformance
│ │ └── CategoryResponseDTO+Extensions.swift # Category DTO conformance
│ └── Migrations/ # Database migrations
│ ├── CreateUsersTableMigration.swift
│ └── CreateHabitsCategoryTableMigration.swift
├── Tests/
│ └── GrowBitAppServerTests/
│ ├── GrowBitAppServerTests.swift
│ └── GrowBitAppServerLoginTests.swift
├── Public/ # Static files directory
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose configuration
└── README.md # This file
Create a .env file in the root directory with the following variables:
# Database Configuration
DATABASE_URL=postgresql://username:password@localhost:5432/growbit_db
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-here
# Server Configuration (optional)
LOG_LEVEL=debugThis application is configured for deployment on Heroku with PostgreSQL:
- Create a new Heroku app
- Add Heroku Postgres addon
- Set environment variables in Heroku dashboard
- Deploy using Git or GitHub integration
The included Dockerfile provides a production-ready container:
docker build -t habit-tracker-api .
docker run -p 8080:8080 habit-tracker-apiThis project serves as a learning experience for backend development with Vapor.
- ✅ Basic Vapor server setup with routing infrastructure
- ✅ User model with Fluent ORM integration
- ✅ User registration endpoint with validation
- ✅ User login endpoint with JWT token generation
- ✅ Password hashing and verification
- ✅ Database migration for users table
- ✅ Category model with database migration
- ✅ Categories CRUD operations (Create, Read, Delete)
- ✅ Category validation (color code format, empty names, duplicate names)
- ✅ Color code normalization (RRGGBB format with # prefix)
- ✅ User ownership verification for category operations
- ✅ Swift 6.2 concurrency support (@Sendable)
- ✅ Shared DTO package integration with @retroactive conformance
- ✅ Test suite for authentication endpoints
- ✅ Test suite for category operations (create, fetch, delete)
- ✅ Comprehensive error handling with proper HTTP status codes
- 🔄 User logout endpoint
- 🔄 Protected routes with JWT middleware
- 📋 Category UPDATE operation
- 📋 Habits CRUD operations
- 📋 Habit entries and calendar functionality
- 📋 JWT token refresh endpoint
This is a learning project. Feel free to explore the code and suggest improvements.
This project is available for educational purposes.