This is a Laravel-based API for Personal Assistance application, providing secure authentication and API functionality powered by Sanctum with Redis for caching and sessions.
- Requirements
- Installation
- Environment Variables
- Running the Project
- Database Migrations
- API Authentication
- Usage
- Testing
- Troubleshooting
- Docker Configuration
- Contributing
- License
The Docker setup is organized by environment and managed within the .docker folder. Each environment has a detailed README with configuration details:
This section covers Docker Compose, Dockerfile, Nginx, PHP, and other settings for each environment.
- Docker and Docker Compose for containerized environment
- PHP (8.2), PostgreSQL, and Redis set up in Docker containers
- Composer for PHP dependencies
-
Clone the Repository:
git clone https://github.com/lucas-negrello/pa-api.git cd pa-api -
Install PHP Dependencies:
composer install
-
Set Up Environment File: Copy
.env.exampleto.envand update configuration settings as needed.cp .env.example .env
-
Generate Application Key:
php artisan key:generate
Configure the following variables in the .env file:
# Application Configuration
APP_NAME=pa-api
APP_ENV=local
APP_KEY=base64:...
APP_DEBUG=true
APP_URL=http://localhost
# Database Configuration
DB_CONNECTION=pgsql
DB_HOST=db
DB_PORT=5432
DB_DATABASE=pa-api
DB_USERNAME=root
DB_PASSWORD=password
# Redis Configuration
CACHE_DRIVER=redis
SESSION_DRIVER=redis
REDIS_HOST=redis
REDIS_PORT=6379
# Sanctum Configuration
SANCTUM_STATEFUL_DOMAINS=localhost
SESSION_DOMAIN=localhost-
Start the Containers:
docker-compose up --build -d
-
Run Migrations: After starting the containers, migrate the database:
docker exec app php artisan migrate -
Check the Application:
- Access the API at:
http://localhost:{APP_PORT}(default: 8084). - The API is served through Nginx and powered by PHP-FPM.
- Access the API at:
To create or update the database schema, run migrations with:
docker-compose exec app php artisan migrateTo rollback the last migration, run:
docker-compose exec app php artisan rollbackThis project uses Laravel Sanctum for token-based authentication.
- Register:
POST /api/register- Create a new user. - Login:
POST /api/login- Log in and retrieve an access token. - Logout:
POST /api/logout- Invalidate the user's access token.
For routes protected by auth:sanctum, include the JWT token in the Authorization header:
Authorization: Bearer {token}-
Run Tests: Use PHPUnit to run tests within the Docker container:
docker exec app php artisan test
-
API Testing: Use tools like Postman or cURL to test API endpoints and ensure responses match expected output.
-
Database Connection Issues:
- Verify the
DB_HOSTandDB_PORTin the.envfile and ensure they match the settings indocker-compose.yml. - Restart the containers with
docker-compose down && docker-compose up -dif connection issues persist.
- Verify the
-
Redis Issues:
- Ensure
CACHE_DRIVERandSESSION_DRIVERare both set toredisin.env. - Check Redis logs with
docker-compose logs redisfor any errors.
- Ensure
Contributions are welcome! Please fork the repository and create a pull request with your changes. Follow the project's coding guidelines and ensure all new features are covered by tests.
This project is licensed under the MIT License. See the LICENSE file for more details.