This project is a backend service for managing Star Wars characters and teams. It is built with Django and Django REST Framework and uses PostgreSQL as its database.
- Star Wars Backend
- Docker
- Docker Compose
-
Clone the repository:
git clone https://github.com/NicolasArnouts/starwars-backend.git cd starwars-backend -
Create a
.envfile based on.env.example:cp .env.example .env
-
Update the
.envfile with your own configuration values. -
Build and start the Docker containers:
docker-compose up --build -d
-
Create a superuser: Creating a Superuser
-
Optional: (re)Fetch data: Fetching Characters
-
Clone the repository:
git clone https://github.com/NicolasArnouts/starwars-backend.git cd starwars-backend -
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
-
On Windows:
.\venv\Scripts\activate
-
On macOS and Linux:
source venv/bin/activate
-
-
Install the dependencies:
pip install -r requirements.txt
-
Create a
.envfile based on.env.example:cp .env.example .env
-
Update the
.envfile with your own configuration values. -
Apply the migrations:
python manage.py migrate
-
Fetch initial character data:
python manage.py fetch_characters
-
Start the Django development server:
python manage.py runserver
The project requires the following environment variables to be set:
SECRET_KEY: Django secret key.DEBUG: Debug mode (set toTruefor development).ALLOWED_HOSTS: Comma-separated list of allowed hosts.DB_NAME: Database name.DB_USER: Database user.DB_PASSWORD: Database password.DB_HOST: Database host (typicallydbwhen using Docker Compose).DB_PORT: Database port (typically5432for PostgreSQL).APP_PORT: Application port (e.g.,8088).APP_HOST: Application host (e.g.,0.0.0.0).
To run the project, use Docker Compose:
docker-compose up -dOnce the project is running, you can access the API at:
http://localhost:8088/api/
POST /api/token/: Obtain JWT token.POST /api/token/refresh/: Refresh JWT token.
GET /api/characters/: List all characters.GET /api/characters/{id}/: Retrieve a specific character.GET /api/characters/?name={name}&height={height}&mass={mass}&gender={gender}&homeworld={homeworld}&species={species}&hairColor={hairColor}&eyeColor={eyeColor}&skinColor={skinColor}&born={born}&died={died}: Filter characters by various parameters.
GET /api/team/: List all teams.POST /api/team/: Create a new team.DELETE /api/team/{id}/: Delete a team.POST /api/team/{id}/add_member/: Add a member to a team.POST /api/team/{id}/remove_member/: Remove a member from a team.
To run the tests in Docker, use the following command:
docker-compose exec web pytestTo fetch the Star Wars characters using Docker, use the following command:
docker-compose exec web python manage.py fetch_charactersTo create a superuser in the bash shell within the Docker container, use the following command:
-
Open a bash shell inside the running web container:
docker-compose exec web /bin/bash -
Create a superuser:
python manage.py createsuperuser
Remember this info as you will need it to authenticate to the API
A Postman collection is included in the repository. You can find it at postman_collection.json. Import this collection into Postman to interact with the API endpoints easily. Make sure you change the collection environment values to django_username, django_password which are valid account credentials.
Here are some additional Docker commands that might be useful for managing your development environment:
-
To open a bash shell inside the running web container:
docker-compose exec web /bin/bash -
To rebuild and start the Docker containers in detached mode:
docker-compose up --build -d
-
To build without using the cache do:
docker-compose build --no-cache
-
To stop and remove the containers, along with the associated volumes:
docker-compose down -v
-
To view the logs of the running containers:
docker-compose logs -f
-
To list all Docker containers (running and stopped):
docker ps -a
-
To remove all stopped containers:
docker container prune
-
To remove all unused images:
docker image prune -a
This project is licensed under the MIT License. See the LICENSE file for more details.