# Installation

## Requirements

Before proceeding with the installation, you should make sure the following requirements are installed on your machine:

* Git
* Docker
* MySQL

## Cloning the repository

The first step is to clone the repository via HTTPS or SSH.

```
git clone git@bitbucket.org:venuzle/venuzle-widget-api.git
```

## Install Composer dependencies and create the .env file

Move to the directory in which the project was cloned.

```
cd venuzle-widget-api
```

Install the Composer dependencies.

```
sudo docker-compose run --rm -w /var/www/html/venuzle-widget-api app composer install
```

Create the `.env` file

```bash
sudo docker-compose run --rm -w /var/www/html/venuzle-widget-api app cp .env.example .env
```

## Generate the application key

Each Laravel application must have a secret key that is used to encrypt and decrypt passwords, cookies, etc. Generate one by running the following command:

```bash
sudo docker-compose run --rm -w /var/www/html/venuzle-widget-api app php artisan key:generate
```

## Create the Database and add the details to the .env file

The Venuzle Widget has, unlike the Venuzle Manager, only one database. The default name should be `widget` but you are free to choose any name you want. Beside that, an indentic database will be required for running the tests locally. Run the following commands from the MySQL command line (or your MySQL client) to create the databases:

```bash
// Widget database
create database widget;

// Testing database
create database widget_testing;
```

Next, open the `.env` file and replace the database details with your valid details:

{% code title=".env" %}

```bash
DB_CONNECTION=mysql
DB_HOST=host.docker.internal
DB_PORT=3306
DB_DATABASE=widget
DB_USERNAME=root
DB_PASSWORD=secret

TESTING_DB_HOST=host.docker.internal
TESTING_DB_PORT=3306
TESTING_DB_DATABASE=widget_testing
TESTING_DB_USERNAME=root
TESTING_DB_PASSWORD=secret
```

{% endcode %}

## Set the session driver

The API is using Laravel Sanctum for authentication which requires a cookie session driver. Open the `.env` file and change the session driver:

{% code title=".env" %}

```bash
SESSION_DRIVER=cookie
```

{% endcode %}

## Running the migrations

At this point, if the database details are ok, you should be able to run the migrations. To do so, execute the following command:

```bash
sudo docker-compose run --rm -w /var/www/html/venuzle-widget-api app php artisan migrate
```

## Starting and stopping the container

In order to have the API running, you must start the container. To do so, run:

```bash
sudo docker-compose up -d
```

In order to stop the container, run:

```bash
sudo docker-compose down
```

## Port binding

By default, the docker container binds the port `80` on your machine. This means that you can access the API by using the URL `localhost:80` . Often, this port is already taken by something else (the frontend instance for example). In order to solve the problem, from the `docker-compose.yml` file you can bind any port you want. As an example, let's bind the port `8084` (line 13)

```bash
version: '2'
services:
  app:
    build:
      context: ./docker/app
      dockerfile: Dockerfile
    image: widget.venuzle.com/app
    restart: always
    volumes:
     - .:/var/www/html/venuzle-widget-api:cached
     - ./storage/logs/nginx:/var/log/nginx:cached
    ports:
     - "8084:80" # port is binded here
    networks:
     - sdnet
networks:
  sdnet:
    driver: "bridge"
```

After that change, you are able to access the API via the URL `localhost:8084`
