Skip to content
Snippets Groups Projects
M4x1m3's avatar
09fb4c93

BomberNyan API

Getting Started

First, install php8 and composer, then clone the repository. You should now copy .env.example to .env and make the required edits. The example configuration should work out of the box and uses an sqlite database.

You should then run the following commands:

# Install all the dependencies
composer install
# Generate the JWT secret
php artisan jwt:secret
# Create the sqlite database.
touch database/database.sqlite
# Run all the migrations on the database
php artisan migrate

To start the development server, you can now run

php artisan serve

Generating the API documentation

The API generation can be generated with

php artisan l5-swagger:generate

Once you start the development server, you can access it at http://127.0.0.1:8000/api/documentation

Running the unit tests

The unit tests can be run with

php artisan test

Running using Docker

To run the API using docker, you first need to copy .env.docker.example to .env.docker and make the necessary changes.

You can then build the API's Docker container:

docker compose build

To start the API, run:

docker compose up

To completely stop it, run:

docker compose down

You can also use this command to clean the volumes:

docker volume rm api_database
docker volume rm api_storage

Warning: This will remove all the data used by the API (database and storage).

Deploying

Start by installing Docker and Docker compose on your production machine.

### Docker and docker compose prerequisites
sudo apt-get install curl
sudo apt-get install gnupg
sudo apt-get install ca-certificates
sudo apt-get install lsb-release

### Download the docker gpg file to Ubuntu
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

### Add Docker and docker compose support to the Ubuntu's packages list
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update

### Install docker and docker compose on Ubuntu
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Create a new system user with a home repository, and copy this repository's docker-compose.yml, .env.docker.example and nginx.conf to its home folder. Be sure to rename .env.docker.example to .env.docker and edit to edit the config files to your likings.

Since we will be deploying from images in the repository, be sure to edit docker-compose.yml and to remove the build: . line in the api service.

Download all the images used by the api:

docker compose pull

You should now be able to launch the API by running

docker compose up

We will now add a systemd unit at /etc/systemd/system/api.service:

[Unit]
Description=BomberNyan API
After=multi-user.target

[Service]
Type=simple
User=api
Group=api
Restart=always
ExecStart=/usr/bin/docker compose up
ExecStop=/usr/bin/docker compose down
WorkingDirectory=/srv/api/

[Install]
WantedBy=multi-user.target

You can then start and enable that systemd unit

sudo systemctl enable api
sudo systemctl start api

The API should now run as a service.

You should also edit /etc/docker/daemon.json to tell Docker to use journald as its log driver:

{
  "log-driver": "journald"
}

This allows you to see the container's logs with

sudo journalctl -feu docker