services:
  apache-config-init:
    image: busybox:latest
    container_name: apache-config-init
    volumes:
      - apache-config:/config
    command:
      - /bin/sh
      - -c
      - |
        cat > /config/httpd.conf <<'EOF'
        ServerRoot "/usr/local/apache2"
        Listen 80
        
        LoadModule mpm_event_module modules/mod_mpm_event.so
        LoadModule authn_file_module modules/mod_authn_file.so
        LoadModule authn_core_module modules/mod_authn_core.so
        LoadModule authz_host_module modules/mod_authz_host.so
        LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
        LoadModule authz_user_module modules/mod_authz_user.so
        LoadModule authz_core_module modules/mod_authz_core.so
        LoadModule access_compat_module modules/mod_access_compat.so
        LoadModule auth_basic_module modules/mod_auth_basic.so
        LoadModule reqtimeout_module modules/mod_reqtimeout.so
        LoadModule filter_module modules/mod_filter.so
        LoadModule mime_module modules/mod_mime.so
        LoadModule log_config_module modules/mod_log_config.so
        LoadModule env_module modules/mod_env.so
        LoadModule headers_module modules/mod_headers.so
        LoadModule setenvif_module modules/mod_setenvif.so
        LoadModule version_module modules/mod_version.so
        LoadModule unixd_module modules/mod_unixd.so
        LoadModule status_module modules/mod_status.so
        LoadModule autoindex_module modules/mod_autoindex.so
        LoadModule dir_module modules/mod_dir.so
        LoadModule alias_module modules/mod_alias.so

        <IfModule unixd_module>
          User daemon
          Group daemon
        </IfModule>

        ServerAdmin admin@localhost
        ServerName localhost

        <Directory />
          AllowOverride none
          Require all denied
        </Directory>

        DocumentRoot "/usr/local/apache2/htdocs"
        <Directory "/usr/local/apache2/htdocs">
          Options Indexes FollowSymLinks
          AllowOverride None
          Require all granted
        </Directory>

        <IfModule dir_module>
          DirectoryIndex index.html
        </IfModule>

        <Files ".ht*">
          Require all denied
        </Files>

        ErrorLog /usr/local/apache2/logs/error_log
        LogLevel warn

        <IfModule log_config_module>
          LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
          LogFormat "%h %l %u %t \"%r\" %>s %b" common
          CustomLog /usr/local/apache2/logs/access_log combined
        </IfModule>

        <IfModule mime_module>
          TypesConfig conf/mime.types
          AddType application/x-compress .Z
          AddType application/x-gzip .gz .tgz
        </IfModule>

        # Configuration du module status pour apache_exporter
        <Location "/server-status">
          SetHandler server-status
          Require all granted
        </Location>
        EOF
        echo "Apache config created"
    restart: "no"

  apache_service:
    image: httpd:2.4
    container_name: conteneur_service
    ports:
      - "8080:80"
    volumes:
      - apache-logs:/usr/local/apache2/logs
      - apache-config:/usr/local/apache2/conf:ro
    networks:
      - observability
    depends_on:
      - apache-config-init
    command: >
      sh -c "touch /usr/local/apache2/logs/access_log /usr/local/apache2/logs/error_log &&
             exec httpd-foreground"

  apache_exporter:
    image: lusotycoon/apache-exporter:v1.0.10
    container_name: apache_exporter_service
    command:
      - '--scrape_uri=http://apache_service/server-status?auto'
    ports:
      - "9117:9117"
    depends_on:
      - apache_service
    networks:
      - observability

  promtail-service-config-init:
    image: busybox:latest
    container_name: promtail-service-config-init
    volumes:
      - promtail-service-config-vol:/config
    command:
      - /bin/sh
      - -c
      - |
        cat > /config/config.yaml <<'EOF'
        server:
          http_listen_port: 9080
          grpc_listen_port: 0
        positions:
          filename: /tmp/positions.yaml
        clients:
          - url: http://192.168.56.61:3100/loki/api/v1/push
        scrape_configs:
          - job_name: apache_access
            static_configs:
              - targets: [localhost]
                labels:
                  job: apache_access
                  host: conteneur_service
                  __path__: /var/log/apache2/access_log
          - job_name: apache_error
            static_configs:
              - targets: [localhost]
                labels:
                  job: apache_error
                  host: conteneur_service
                  __path__: /var/log/apache2/error_log
        EOF
        echo "Promtail service config created"
    restart: "no"

  promtail_service:
    image: grafana/promtail:2.9.0
    container_name: promtail_service
    volumes:
      - apache-logs:/var/log/apache2:ro
      - promtail-service-config-vol:/etc/promtail:ro
    command:
      - '-config.file=/etc/promtail/config.yaml'
    depends_on:
      - promtail-service-config-init
      - apache_service
    networks:
      - observability

volumes:
  apache-logs:
  apache-config:
  promtail-service-config-vol:

networks:
  observability:
    external: true
    name: observability_observability