Skip to content

Commit

Permalink
Merge pull request #1 from zunnu/dev
Browse files Browse the repository at this point in the history
Rest api
  • Loading branch information
zunnu authored Oct 20, 2021
2 parents 8c86ec7 + c404212 commit bef0f12
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 10 deletions.
9 changes: 9 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
['path' => '/log-reader'],
function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'LogReader', 'action' => 'index'])->setExtensions(['json']);

$routes->prefix('api', function (RouteBuilder $routes) {
$routes->prefix('v1', function (RouteBuilder $routes) {
$routes->connect('/files', ['controller' => 'LogReader', 'action' => 'files'])->setExtensions(['json']);
$routes->connect('/types', ['controller' => 'LogReader', 'action' => 'types'])->setExtensions(['json']);
$routes->connect('/logs', ['controller' => 'LogReader', 'action' => 'logs'])->setExtensions(['json']);
// // $routes->connect('/:controller');
});
});
$routes->fallbacks(DashedRoute::class);
}
);
9 changes: 9 additions & 0 deletions src/Controller/Api/V1/AppController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace LogReader\Controller\Api\V1;

use App\Controller\AppController as BaseController;

class AppController extends BaseController
{
}
74 changes: 74 additions & 0 deletions src/Controller/Api/V1/LogReaderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
namespace LogReader\Controller\Api\V1;

use LogReader\Controller\Api\V1\AppController;
use Cake\Log\Log;
use LogReader\Reader;

class LogReaderController extends AppController
{
public function initialize() {
parent::initialize();
}

/**
* Logs method
*/
public function logs($date = null) {
$this->viewBuilder()->setLayout(false);
$conditions = [];

// SEARCH
if ($this->request->is('post')) {
$data = $this->request->getData();

if(!empty($data['files'])) {
$files = explode(',', $data['files']);
$conditions['files'] = array_map('trim', array_filter($files));
}

if(!empty($data['types'])) {
$types = explode(',', $data['types']);
$conditions['types'] = array_map('trim', array_filter($types));
}
}

$this->Reader = new Reader($conditions);
$logs = $this->Reader->read();

$this->set([
'logs' => $logs,
'_serialize' => ['logs']
]);
}

/**
* Types method
* Return available log types
*/
public function types($date = null) {
$this->viewBuilder()->setLayout(false);
$this->Reader = new Reader();
$types = $this->Reader->getLogTypes();

$this->set([
'types' => $types,
'_serialize' => ['types']
]);
}

/**
* Files method
* Return the available log files
*/
public function files($date = null) {
$this->viewBuilder()->setLayout(false);
$this->Reader = new Reader();
$files = $this->Reader->getFiles();

$this->set([
'files' => $files,
'_serialize' => ['files']
]);
}
}
1 change: 1 addition & 0 deletions src/Controller/LogReaderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function index($date = null) {
$this->Reader = new Reader($conditions);
$this->set('logs', $this->Reader->read());
$this->set('files', $this->Reader->getFiles());
$this->set('types', $this->Reader->getLogTypes());
$this->set('selectedFiles', !empty($conditions['files']) ? $conditions['files'] : []);
$this->set('selectedTypes', !empty($conditions['types']) ? $conditions['types'] : []);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ class Reader {
// prefixes
protected $config = [];

private $logTypes = [
'info' => 'Info',
'emergency' => 'Emergency',
'critical' => 'Critical',
'alert' => 'Alert',
'error' => 'Error',
'warning' => 'Warning',
'notice' => 'Notice',
'debug' => 'Debug'
];

public function __construct($config = []) {
$this->config = $config;
}
Expand Down Expand Up @@ -193,4 +204,8 @@ private function _parseData($data) {

return $buildData;
}

public function getLogTypes() {
return $this->logTypes;
}
}
Empty file.
10 changes: 0 additions & 10 deletions src/Template/LogReader/index.ctp
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
<?php
$types = [
'info' => 'Info',
'emergency' => 'Emergency',
'critical' => 'Critical',
'alert' => 'Alert',
'error' => 'Error',
'warning' => 'Warning',
'notice' => 'Notice',
'debug' => 'Debug'
];
$fileList = [];

if(!empty($files)) {
Expand Down

0 comments on commit bef0f12

Please sign in to comment.