# Copyright (c) Mainflux
# SPDX-License-Identifier: Apache-2.0

version: "3.7"

networks:
  mainflux-base-net:
    driver: bridge

volumes:
  mainflux-authn-db-volume:
  mainflux-users-db-volume:
  mainflux-things-db-volume:
  mainflux-things-redis-volume:
  mainflux-es-redis-volume:
  mainflux-mqtt-broker-volume:

services:
  nginx:
    image: nginx:1.16.0-alpine
    container_name: mainflux-nginx
    restart: on-failure
    volumes:
      - ./nginx/nginx-${AUTH-key}.conf:/etc/nginx/nginx.conf.template
      - ./nginx/entrypoint.sh:/entrypoint.sh
      - ./nginx/snippets:/etc/nginx/snippets
      - ./ssl/authorization.js:/etc/nginx/authorization.js
      - ./ssl/certs/mainflux-server.crt:/etc/ssl/certs/mainflux-server.crt
      - ./ssl/certs/ca.crt:/etc/ssl/certs/ca.crt
      - ./ssl/certs/mainflux-server.key:/etc/ssl/private/mainflux-server.key
      - ./ssl/dhparam.pem:/etc/ssl/certs/dhparam.pem
    ports:
      - ${MF_NGINX_HTTP_PORT}:${MF_NGINX_HTTP_PORT}
      - ${MF_NGINX_SSL_PORT}:${MF_NGINX_SSL_PORT}
      - ${MF_NGINX_MQTT_PORT}:${MF_NGINX_MQTT_PORT}
      - ${MF_NGINX_MQTTS_PORT}:${MF_NGINX_MQTTS_PORT}
    networks:
      - mainflux-base-net
    env_file:
      - ../.env
    command: /entrypoint.sh
    depends_on:
      - things
      - users
      - mqtt-adapter
      - http-adapter

  nats:
    image: nats:1.3.0
    container_name: mainflux-nats
    command: "-c /etc/nats/nats.conf"
    restart: on-failure
    volumes:
      - ./nats/:/etc/nats
    networks:
      - mainflux-base-net

  authn-db:
    image: postgres:10.8-alpine
    container_name: mainflux-authn-db
    restart: on-failure
    environment:
      POSTGRES_USER: ${MF_AUTHN_DB_USER}
      POSTGRES_PASSWORD: ${MF_AUTHN_DB_PASS}
      POSTGRES_DB: ${MF_AUTHN_DB}
    networks:
      - mainflux-base-net
    volumes:
      - mainflux-authn-db-volume:/var/lib/postgresql/data

  authn:
    image: mainflux/authn:latest
    container_name: mainflux-authn
    depends_on:
      - authn-db
    expose:
      - ${MF_AUTHN_GRPC_PORT}
    restart: on-failure
    environment:
      MF_AUTHN_LOG_LEVEL: ${MF_AUTHN_LOG_LEVEL}
      MF_AUTHN_DB_HOST: authn-db
      MF_AUTHN_DB_PORT: ${MF_AUTHN_DB_PORT}
      MF_AUTHN_DB_USER: ${MF_AUTHN_DB_USER}
      MF_AUTHN_DB_PASS: ${MF_AUTHN_DB_PASS}
      MF_AUTHN_DB: ${MF_AUTHN_DB}
      MF_AUTHN_HTTP_PORT: ${MF_AUTHN_HTTP_PORT}
      MF_AUTHN_GRPC_PORT: ${MF_AUTHN_GRPC_PORT}
      MF_AUTHN_SECRET: ${MF_AUTHN_SECRET}
      MF_JAEGER_URL: ${MF_JAEGER_URL}
    ports:
      - ${MF_AUTHN_HTTP_PORT}:${MF_AUTHN_HTTP_PORT}
      - ${MF_AUTHN_GRPC_PORT}:${MF_AUTHN_GRPC_PORT}
    networks:
      - mainflux-base-net

  users-db:
    image: postgres:10.8-alpine
    container_name: mainflux-users-db
    restart: on-failure
    environment:
      POSTGRES_USER: ${MF_USERS_DB_USER}
      POSTGRES_PASSWORD: ${MF_USERS_DB_PASS}
      POSTGRES_DB: ${MF_USERS_DB}
    networks:
      - mainflux-base-net
    volumes:
      - mainflux-users-db-volume:/var/lib/postgresql/data

  users:
    image: mainflux/users:latest
    container_name: mainflux-users
    volumes:
      - ./users/emailer/templates/${MF_EMAIL_TEMPLATE}:/${MF_EMAIL_TEMPLATE}
    depends_on:
      - users-db
      - authn
    restart: on-failure
    environment:
      MF_USERS_LOG_LEVEL: ${MF_USERS_LOG_LEVEL}
      MF_USERS_DB_HOST: users-db
      MF_USERS_DB_PORT: ${MF_USERS_DB_PORT}
      MF_USERS_DB_USER: ${MF_USERS_DB_USER}
      MF_USERS_DB_PASS: ${MF_USERS_DB_PASS}
      MF_USERS_DB: ${MF_USERS_DB}
      MF_USERS_HTTP_PORT: ${MF_USERS_HTTP_PORT}
      MF_JAEGER_URL: ${MF_JAEGER_URL}
      MF_EMAIL_DRIVER: ${MF_EMAIL_DRIVER}
      MF_EMAIL_HOST: ${MF_EMAIL_HOST}
      MF_EMAIL_PORT: ${MF_EMAIL_PORT}
      MF_EMAIL_USERNAME: ${MF_EMAIL_USERNAME}
      MF_EMAIL_PASSWORD: ${MF_EMAIL_PASSWORD}
      MF_EMAIL_FROM_ADDRESS: ${MF_EMAIL_FROM_ADDRESS}
      MF_EMAIL_FROM_NAME: ${MF_EMAIL_FROM_NAME}
      MF_EMAIL_TEMPLATE: ${MF_EMAIL_TEMPLATE}
      MF_TOKEN_RESET_ENDPOINT: ${MF_TOKEN_RESET_ENDPOINT}
      MF_AUTHN_GRPC_URL: ${MF_AUTHN_GRPC_URL}
      MF_AUTHN_GRPC_TIMEOUT: ${MF_AUTHN_GRPC_TIMEOUT}
    ports:
      - ${MF_USERS_HTTP_PORT}:${MF_USERS_HTTP_PORT}
    networks:
      - mainflux-base-net

  things-db:
    image: postgres:10.8-alpine
    container_name: mainflux-things-db
    restart: on-failure
    environment:
      POSTGRES_USER: ${MF_THINGS_DB_USER}
      POSTGRES_PASSWORD: ${MF_THINGS_DB_PASS}
      POSTGRES_DB: ${MF_THINGS_DB}
    networks:
      - mainflux-base-net
    volumes:
      - mainflux-things-db-volume:/var/lib/postgresql/data

  things-redis:
    image: redis:5.0-alpine
    container_name: mainflux-things-redis
    restart: on-failure
    networks:
      - mainflux-base-net
    volumes:
      - mainflux-things-redis-volume:/data

  things:
    image: mainflux/things:latest
    container_name: mainflux-things
    depends_on:
      - things-db
      - authn
    restart: on-failure
    environment:
      MF_THINGS_LOG_LEVEL: ${MF_THINGS_LOG_LEVEL}
      MF_THINGS_DB_HOST: things-db
      MF_THINGS_DB_PORT: ${MF_THINGS_DB_PORT}
      MF_THINGS_DB_USER: ${MF_THINGS_DB_USER}
      MF_THINGS_DB_PASS: ${MF_THINGS_DB_PASS}
      MF_THINGS_DB: ${MF_THINGS_DB}
      MF_THINGS_CACHE_URL: things-redis:${MF_REDIS_TCP_PORT}
      MF_THINGS_ES_URL: es-redis:${MF_REDIS_TCP_PORT}
      MF_THINGS_HTTP_PORT: ${MF_THINGS_HTTP_PORT}
      MF_THINGS_AUTH_HTTP_PORT: ${MF_THINGS_AUTH_HTTP_PORT}
      MF_THINGS_AUTH_GRPC_PORT: ${MF_THINGS_AUTH_GRPC_PORT}
      MF_THINGS_SECRET: ${MF_THINGS_SECRET}
      MF_JAEGER_URL: ${MF_JAEGER_URL}
      MF_AUTHN_GRPC_URL: ${MF_AUTHN_GRPC_URL}
      MF_AUTHN_GRPC_TIMEOUT: ${MF_AUTHN_GRPC_TIMEOUT}
    ports:
      - ${MF_THINGS_HTTP_PORT}:${MF_THINGS_HTTP_PORT}
      - ${MF_THINGS_AUTH_HTTP_PORT}:${MF_THINGS_AUTH_HTTP_PORT}
      - ${MF_THINGS_AUTH_GRPC_PORT}:${MF_THINGS_AUTH_GRPC_PORT}
    networks:
      - mainflux-base-net

  jaeger:
    image: jaegertracing/all-in-one:1.13
    container_name: mainflux-jaeger
    ports:
      - ${MF_JAEGER_PORT}:${MF_JAEGER_PORT}/udp
      - ${MF_JAEGER_FRONTEND}:${MF_JAEGER_FRONTEND}
      - ${MF_JAEGER_COLLECTOR}:${MF_JAEGER_COLLECTOR}
      - ${MF_JAEGER_CONFIGS}:${MF_JAEGER_CONFIGS}
    networks:
      - mainflux-base-net

  vernemq:
    image: mainflux/vernemq:latest
    container_name: mainflux-vernemq
    restart: on-failure
    environment:
      DOCKER_VERNEMQ_ALLOW_ANONYMOUS: ${MF_DOCKER_VERNEMQ_ALLOW_ANONYMOUS}
      DOCKER_VERNEMQ_LOG__CONSOLE__LEVEL: ${MF_DOCKER_VERNEMQ_LOG__CONSOLE__LEVEL}
    networks:
      - mainflux-base-net
    volumes:
      - mainflux-mqtt-broker-volume:/var/lib/vernemq

  mqtt-adapter:
    image: mainflux/mqtt:latest
    container_name: mainflux-mqtt
    depends_on:
      - vernemq
      - things
      - nats
    restart: on-failure
    environment:
      MF_MQTT_ADAPTER_LOG_LEVEL: ${MF_MQTT_ADAPTER_LOG_LEVEL}
      MF_MQTT_ADAPTER_MQTT_PORT: ${MF_MQTT_ADAPTER_MQTT_PORT}
      MF_MQTT_ADAPTER_WS_PORT: ${MF_MQTT_ADAPTER_WS_PORT}
      MF_MQTT_ADAPTER_ES_URL: es-redis:${MF_REDIS_TCP_PORT}
      MF_NATS_URL: ${MF_NATS_URL}
      MF_MQTT_ADAPTER_MQTT_TARGET_HOST: vernemq
      MF_MQTT_ADAPTER_MQTT_TARGET_PORT: ${MF_MQTT_BROKER_PORT}
      MF_MQTT_ADAPTER_WS_TARGET_HOST: vernemq
      MF_MQTT_ADAPTER_WS_TARGET_PORT: ${MF_MQTT_BROKER_WS_PORT}
      MF_JAEGER_URL: ${MF_JAEGER_URL}
      MF_THINGS_AUTH_GRPC_URL: ${MF_THINGS_AUTH_GRPC_URL}
      MF_THINGS_AUTH_GRPC_TIMEOUT: ${MF_THINGS_AUTH_GRPC_TIMEOUT}
    networks:
      - mainflux-base-net

  http-adapter:
    image: mainflux/http:latest
    container_name: mainflux-http
    depends_on:
      - things
      - nats
    restart: on-failure
    environment:
      MF_HTTP_ADAPTER_LOG_LEVEL: debug
      MF_HTTP_ADAPTER_PORT: ${MF_HTTP_ADAPTER_PORT}
      MF_NATS_URL: ${MF_NATS_URL}
      MF_JAEGER_URL: ${MF_JAEGER_URL}
      MF_THINGS_AUTH_GRPC_URL: ${MF_THINGS_AUTH_GRPC_URL}
      MF_THINGS_AUTH_GRPC_TIMEOUT: ${MF_THINGS_AUTH_GRPC_TIMEOUT}
    ports:
      - ${MF_HTTP_ADAPTER_PORT}:${MF_HTTP_ADAPTER_PORT}
    networks:
      - mainflux-base-net

  es-redis:
    image: redis:5.0-alpine
    container_name: mainflux-es-redis
    restart: on-failure
    networks:
      - mainflux-base-net
    volumes:
      - mainflux-es-redis-volume:/data

  coap-adapter:
    image: mainflux/coap:latest
    container_name: mainflux-coap
    depends_on:
      - things
      - nats
    restart: on-failure
    environment:
      MF_COAP_ADAPTER_LOG_LEVEL: ${MF_COAP_ADAPTER_LOG_LEVEL}
      MF_COAP_ADAPTER_PORT: ${MF_COAP_ADAPTER_PORT}
      MF_NATS_URL: ${MF_NATS_URL}
      MF_JAEGER_URL: ${MF_JAEGER_URL}
      MF_THINGS_AUTH_GRPC_URL: ${MF_THINGS_AUTH_GRPC_URL}
      MF_THINGS_AUTH_GRPC_TIMEOUT: ${MF_THINGS_AUTH_GRPC_TIMEOUT}
    ports:
      - ${MF_COAP_ADAPTER_PORT}:${MF_COAP_ADAPTER_PORT}/udp
      - ${MF_COAP_ADAPTER_PORT}:${MF_COAP_ADAPTER_PORT}/tcp
    networks:
      - mainflux-base-net
