Skip to content

Commit

Permalink
Refacored API to use Kipchak
Browse files Browse the repository at this point in the history
  • Loading branch information
meezaan committed Sep 6, 2022
1 parent 019e589 commit e5622e7
Show file tree
Hide file tree
Showing 112 changed files with 7,708 additions and 3,497 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
edition-importer/*
.git/*
.circleci/*
.idea/*
.gitignore

4 changes: 2 additions & 2 deletions .k8s/manifest-dallas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ spec:
value: "11211"
livenessProbe:
httpGet:
path: /v1/liveness
path: /liveness
port: 8080
initialDelaySeconds: 7
periodSeconds: 30
timeoutSeconds: 5
failureThreshold: 3
startupProbe:
httpGet:
path: /v1/status
path: /status
port: 8080
periodSeconds: 7
failureThreshold: 3
Expand Down
4 changes: 2 additions & 2 deletions .k8s/manifest-london.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ spec:
value: "11211"
livenessProbe:
httpGet:
path: /v1/liveness
path: /liveness
port: 8080
initialDelaySeconds: 7
periodSeconds: 30
timeoutSeconds: 5
failureThreshold: 3
startupProbe:
httpGet:
path: /v1/status
path: /status
port: 8080
periodSeconds: 7
failureThreshold: 3
Expand Down
4 changes: 2 additions & 2 deletions .k8s/manifest-singapore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ spec:
value: "11211"
livenessProbe:
httpGet:
path: /v1/liveness
path: /liveness
port: 8080
initialDelaySeconds: 7
periodSeconds: 30
timeoutSeconds: 5
failureThreshold: 3
startupProbe:
httpGet:
path: /v1/status
path: /status
port: 8080
periodSeconds: 7
failureThreshold: 3
Expand Down
43 changes: 16 additions & 27 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
FROM islamicnetwork/php:8.0-apache

# Copy files
RUN cd ../ && rm -rf /var/www/html
COPY . /var/www/
COPY /etc/apache2/mods-enabled/mpm_prefork.conf /etc/apache2/mods-enabled/mpm_prefork.conf

# Run Composer
RUN cd /var/www && composer install --no-dev

RUN chown -R www-data:www-data /var/www/
ENV MYSQL_USER "someUser"
ENV MYSQL_PASSWORD "somePassword"
ENV MYSQL_DATABASE "someDb"
ENV MYSQL_HOST_1 "localhost"
ENV MYSQL_HOST_2 "localhost"
ENV MYSQL_HOST_3 "localhost"
# 0 = disabled. 1 = enabled
ENV WAF_PROXY_MODE "0"
ENV WAF_KEY "someKey"
ENV LOAD_BALANCER_MODE "0"
ENV LOAD_BALANCER_KEY "KEY"

COPY doctrineProxies.sh /usr/local/bin/doctrineProxies.sh
RUN chmod 755 /usr/local/bin/doctrineProxies.sh

CMD ["/usr/local/bin/doctrineProxies.sh"]
FROM islamicnetwork/php:8.1-apache-dev

# Copy files
COPY . /var/www/
COPY etc/apache2/mods-enabled/mpm_prefork.conf /etc/apache2/mods-enabled/mpm_prefork.conf

# Run Composer
RUN cd /var/www && composer install --no-dev

# Set the correct permissions
RUN chown -R www-data:www-data /var/www/

COPY doctrineProxies.sh /usr/local/bin/doctrineProxies.sh
RUN chmod 755 /usr/local/bin/doctrineProxies.sh

CMD ["/usr/local/bin/doctrineProxies.sh"]
10 changes: 0 additions & 10 deletions Dockerfile.db

This file was deleted.

37 changes: 37 additions & 0 deletions api/Controllers/AlQuranController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Api\Controllers;

use Doctrine\ORM\EntityManager;
use Mamluk\Kipchak\Components\Controllers;
use Psr\Container\ContainerInterface;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;

/**
* All Controllers extending Controllers\Slim Contain the Service / DI Container as a protected property called $container.
* Access it using $this->container in your controller.
* Default objects bundled into a container are:
* logger - which returns an instance of \Monolog\Logger. This is also a protected property on your controller. Access it using $this->logger.
*/

class AlQuranController extends Controllers\Slim
{
/**
* @var EntityManager
*/
protected $em;

/**
* @var MemcachedAdapter
*/
protected $mc;

public function __construct(ContainerInterface $container)
{
parent::__construct($container);

$this->em = $this->container->get('database.doctrine.entitymanager.primary');

$this->mc = $this->container->get('cache.memcached.cache');
}
}
28 changes: 28 additions & 0 deletions api/Controllers/Liveness.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Api\Controllers;

use Mamluk\Kipchak\Components\Controllers;
use Mamluk\Kipchak\Components\Http;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Doctrine\DBAL\Connection;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;

/**
* All Controllers extending Controllers\Slim Contain the Service / DI Container as a protected property called $container.
* Access it using $this->container in your controller.
* Default objects bundled into a container are:
* logger - which returns an instance of \Monolog\Logger. This is also a protected property on your controller. Access it using $this->logger.
*/

class Liveness extends Controllers\Slim
{
public function get(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
return Http\Response::json($response,
'UP',
200
);
}
}
69 changes: 69 additions & 0 deletions api/Controllers/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Api\Controllers;

use Mamluk\Kipchak\Components\Controllers;
use Mamluk\Kipchak\Components\Http;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Doctrine\DBAL\Connection;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
use Symfony\Contracts\Cache\ItemInterface;

/**
* All Controllers extending Controllers\Slim Contain the Service / DI Container as a protected property called $container.
* Access it using $this->container in your controller.
* Default objects bundled into a container are:
* logger - which returns an instance of \Monolog\Logger. This is also a protected property on your controller. Access it using $this->logger.
*/

class Status extends Controllers\Slim
{

public function get(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{

$this->logger->debug('Checking Status...');

/**
* @var $db Connection
*/
$db = $this->container->get('database.doctrine.dbal.primary');

/**
* @var $mc MemcachedAdapter
*/
$mc = $this->container->get('cache.memcached.cache');

$mc->get('status', function (ItemInterface $item) {
$item->expiresAfter(3);
return 'up';
});

$mcStatus = $mc->getItem('status');

try {
$dbResult = $db->fetchAssociative("SELECT id FROM ayat WHERE id = ? ", [7]);
} catch (Exception $e) {
$dbResult = false;
}

$status = [
'memcached' => $mcStatus->get() === 'up' ? 'OK' : 'NOT OK',
'database' => $dbResult === false ? 'NOT OK' : 'OK (' . $dbResult['id']. ')',
];

if ($mcStatus->get() !== 'up' || $dbResult === false) {
return Http\Response::json($response,
$status,
500
);

}

return Http\Response::json($response,
$status,
200
);
}
}
Loading

0 comments on commit e5622e7

Please sign in to comment.