services:
    application:
        build:
            context: ./docker/app
            dockerfile: Dockerfile
            args:
                APP_ENV: '${APP_ENV:-local}'
                PHP_VERSION: '${PHP_VERSION:-8}'
                COMPOSER_VERSION: '${COMPOSER_VERSION:-2}'
                NODE_VERSION: '${NODE_VERSION:-16}'
        image: "${COMPOSE_PROJECT_NAME}:${APP_ENV:-local}"
        pull_policy: never
        restart: unless-stopped
        environment:
            HOST_UID: '${HOST_UID:-1000}'
            HOST_GID: '${HOST_GID:-1000}'
            APP_SCHEDULE: '${APP_SCHEDULE:-false}'
            WORK_QUEUE: ${WORK_QUEUE:-false}
            WORK_PHP_FPM: ${WORK_PHP_FPM:-false}
            WORK_WEBSOCKET: ${WORK_WEBSOCKET:-false}
            WORK_OCTANE: ${WORK_OCTANE:-false}
        volumes:
            -   type: bind
                source: '.'
                target: '/var/www/html'
            -   type: bind
                source: '${HOME}/.npm'
                target: '/tmp/.npm'
            -   type: bind
                source: '${HOME}/.composer'
                target: '/tmp/.composer'
        networks:
            - neoscrypts
        depends_on:
            mysql:
                condition: service_healthy
            redis:
                condition: service_healthy
            mongo:
                condition: service_healthy
            bitgo:
                condition: service_started
            ethereum-api:
                condition: service_healthy
            binance-api:
                condition: service_healthy
        healthcheck:
            test: [ "CMD", "healthcheck" ]
            start_period: 20m

    nginx:
        build:
            context: ./docker/nginx
            dockerfile: Dockerfile
            args:
                NGINX_VERSION: '${NGINX_VERSION:-1}'
        stop_signal: SIGTERM
        restart: unless-stopped
        environment:
            VIRTUAL_HOST: '${APP_DOMAIN:-cryptitan.local}'
            VIRTUAL_HTTP_PORT: '${NGINX_HTTP_PORT:-80}'
            VIRTUAL_HTTPS_PORT: '${NGINX_HTTPS_PORT:-}'
            APP_DOMAIN: '${APP_DOMAIN:-cryptitan.local}'
            NGINX_DEFAULT_CONFIG: '${NGINX_DEFAULT_CONFIG:-/etc/nginx/conf.d/default}'
            NGINX_WEBSOCKET_CONFIG: '${NGINX_WEBSOCKET_CONFIG:-/etc/nginx/conf.d/websocket}'
            NGINX_DEFAULT_SERVER: '${NGINX_DEFAULT_SERVER:-/etc/nginx/includes/php-fpm}'
            WEBSOCKET_ENABLE: '${WEBSOCKET_ENABLE:-false}'
        volumes:
            -   type: volume
                source: 'certbot-root'
                target: '/var/www/certbot'
            -   type: volume
                source: 'letsencrypt-lib'
                target: '/var/lib/letsencrypt'
            -   type: volume
                source: 'letsencrypt'
                target: '/etc/letsencrypt'
            -   type: bind
                source: '.'
                target: '/var/www/html'
                read_only: true
        networks:
            - neoscrypts
            - nginx-proxy
        depends_on:
            application:
                condition: service_healthy
        healthcheck:
            test: [ "CMD-SHELL", "service nginx status || exit 1" ]

    mysql:
        image: 'mysql:${MYSQL_VERSION:-8}'
        restart: unless-stopped
        environment:
            MYSQL_DATABASE: '${DB_DATABASE:-cryptitan}'
            MYSQL_USER: '${DB_USERNAME:-cryptitan_user}'
            MYSQL_PASSWORD: '${DB_PASSWORD:-secret}'
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD:-secret}'
        volumes:
            - 'neoscrypts-mysql:/var/lib/mysql'
        networks:
            - neoscrypts
        cap_add:
            - SYS_NICE
        healthcheck:
            test: [ "CMD", "mysqladmin", "ping", "-p${DB_PASSWORD:-secret}" ]

    mongo:
        image: 'mongo:${MONGO_VERSION:-6}'
        command: --auth --noscripting
        restart: unless-stopped
        environment:
            MONGO_INITDB_DATABASE: '${DB_DATABASE:-cryptitan}'
            MONGO_INITDB_ROOT_USERNAME: '${DB_USERNAME:-cryptitan_user}'
            MONGO_INITDB_ROOT_PASSWORD: '${DB_PASSWORD:-secret}'
        volumes:
            - 'neoscrypts-mongo:/data/db'
            - 'neoscrypts-mongo-config:/data/configdb'
        networks:
            - neoscrypts
        healthcheck:
            test: [ "CMD-SHELL", "echo 'db.stats().ok' | mongosh localhost:27017/test --quiet"]
            start_period: 40s

    redis:
        build:
            context: ./docker/redis
            dockerfile: Dockerfile
            args:
                REDIS_VERSION: '${REDIS_VERSION:-6}'
        restart: unless-stopped
        volumes:
            - 'neoscrypts-redis:/data'
        networks:
            - neoscrypts
        healthcheck:
            test: [ "CMD", "redis-cli", "ping" ]

    bitgo:
        image: bitgosdk/express:latest
        restart: unless-stopped
        environment:
            BITGO_ENV: '${BITGO_ENV:-test}'
            BITGO_PORT: '${BITGO_PORT:-4000}'
            BITGO_DISABLE_SSL: 1
        networks:
            - neoscrypts

    ethereum-api:
        image: neoscrypts/ethereum-api:london
        restart: unless-stopped
        environment:
            HOST_UID: '${HOST_UID:-1000}'
            ETHEREUM_MONGODB_URL: '${MONGODB_URL:-mongodb://$DB_USERNAME:$DB_PASSWORD@mongo:27017/?authSource=admin}'
            ETHEREUM_MONGODB_NAME: 'ethereum-api'
            ETHEREUM_WS: '${ETHEREUM_WS}'
        volumes:
            -   type: bind
                source: './storage/ethereum'
                target: '/home/node/storage'
        networks:
            - neoscrypts
        healthcheck:
            test: [ "CMD-SHELL", "curl --fail http://localhost:7000/ping || exit 1" ]
            start_period: 2m

    binance-api:
        image: neoscrypts/ethereum-api:london
        restart: unless-stopped
        environment:
            HOST_UID: '${HOST_UID:-1000}'
            ETHEREUM_MONGODB_URL: '${MONGODB_URL:-mongodb://$DB_USERNAME:$DB_PASSWORD@mongo:27017/?authSource=admin}'
            ETHEREUM_MONGODB_NAME: 'binance-api'
            ETHEREUM_WS: '${BINANCE_WS}'
        volumes:
            -   type: bind
                source: './storage/binance'
                target: '/home/node/storage'
        networks:
            - neoscrypts
        healthcheck:
            test: [ "CMD-SHELL", "curl --fail http://localhost:7000/ping || exit 1" ]
            start_period: 2m

networks:
    nginx-proxy:
        external: true
        name: '${PROXY_NETWORK:-nginx-proxy}'
    neoscrypts:
        driver: bridge

volumes:
    certbot-root:
        external: true
        name: '${CERTBOT_ROOT_VOLUME:-certbot-root}'
    letsencrypt-lib:
        external: true
        name: '${LETSENCRYPT_LIB_VOLUME:-letsencrypt-lib}'
    letsencrypt-log:
        external: true
        name: '${LETSENCRYPT_LOG_VOLUME:-letsencrypt-log}'
    letsencrypt:
        external: true
        name: '${LETSENCRYPT_VOLUME:-letsencrypt}'
    neoscrypts-mysql:
        driver: local
    neoscrypts-mongo:
        driver: local
    neoscrypts-mongo-config:
        driver: local
    neoscrypts-redis:
        driver: local
    neoscrypts-pgsql:
        driver: local
