---
apiVersion: v1
kind: ConfigMap
metadata:
  name: frontend-configmap
  namespace: guestbook
data:
  guestbook.php: |
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    require 'Predis/Autoloader.php';
    Predis\Autoloader::register();
    if (isset($_GET['cmd']) === true) {
      $host = 'redis-ha';
      if (getenv('GET_HOSTS_FROM') == 'env') {
        $host = getenv('REDIS_MASTER_SERVICE_HOST');
      }
      header('Content-Type: application/json');
      if ($_GET['cmd'] == 'set') {
        $sentinels = ['tcp://redis-ha:26379'];
        $options   = ['replication' => 'sentinel', 'service' => 'mymaster'];
        $client = new Predis\Client($sentinels, $options);
        $client->set($_GET['key'], $_GET['value']);
        print('{"message": "Updated"}');
      } else {
        $host = 'redis-ha';
        if (getenv('GET_HOSTS_FROM') == 'env') {
          $host = getenv('REDIS_SLAVE_SERVICE_HOST');
        }
        $sentinels = ['tcp://redis-ha:26379'];
        $options   = ['replication' => 'sentinel', 'service' => 'mymaster'];
        $client = new Predis\Client($sentinels, $options);
        $value = $client->get($_GET['key']);
        print('{"data": "' . $value . '"}');
      }
    } else {
      phpinfo();
    } ?>
