Beispiel #1
0
 /**
  * Add new message to queue
  *
  * @param Psr\Http\Message\RequestInterface $request
  * @param Psr\Http\Message\ResponseInterface $response 
  * @param array $args
  * 
  * @return Psr\Http\Message\ResponseInterface
  */
 public function newMessage(Request $request, Response $response, array $args)
 {
     global $database;
     $err = new \Malmanger\Mpmq\Util\ErrorHandler();
     $data = $request->getParsedBody();
     $this->log->debug("newMessage data=" . print_r($data, true));
     // Check for mandatory parameters and set defaults
     $id = $args['id'];
     $queue = new DbQueue($id);
     if (!$queue->queueExists()) {
         $err->addNotFound("queue with id=" . $id);
     }
     $messageId = null;
     $title = null;
     $payload = null;
     $timeout = null;
     //        $key = "id";
     //        if (!array_key_exists($key, $data)) {
     //            $err->addMissing($key);
     //        } else {
     //            $id = $data["id"];
     //        }
     $key = "title";
     if (!array_key_exists($key, $data)) {
         $err->addMissing($key);
     } else {
         $title = $data[$key];
     }
     $key = "payload";
     if (array_key_exists($key, $data)) {
         $payload = $data[$key];
     }
     $key = "timeout";
     if (array_key_exists($key, $data)) {
         $timeout = $data[$key];
     } else {
         $timeout = $queue->getTimeout();
     }
     $message = new DbMessage($id, $messageId, $title, $payload, $timeout);
     //        if ($queue->queueExists()) {
     //            $err->addExists($id);
     //        }
     if ($err->getLevel() > 0) {
         return $err->getErrorResponse($response);
     }
     $messageId = $message->save();
     if (!$messageId) {
         $err->addDbUpdate("newMessage");
     }
     if ($err->getLevel() > 0) {
         return $err->getErrorResponse($response);
     } else {
         $data = array();
         $data['id'] = $message->getQueueId();
         $data['messageId'] = $message->getMessageId();
         $data['title'] = $message->getTitle();
         $data['payload'] = $message->getPayload();
         $data['timeout'] = $message->getTimeout();
         $data['inFlight'] = $message->getInFlight();
         $resp = new \Malmanger\Mpmq\Util\ResponseHandler($data);
         return $resp->getResponse($response);
     }
 }
Beispiel #2
0
 public function deleteQueue(Request $request, Response $response, array $args)
 {
     global $database;
     $err = new \Malmanger\Mpmq\Util\ErrorHandler();
     $data = $request->getParsedBody();
     $this->log->debug("deleteQueue args=" . print_r($args, true));
     $queue = new DbQueue($args['id']);
     if (!$queue->queueExists()) {
         $err->addNotFound($args['id']);
     }
     if (!$queue->delete()) {
         $err->addDbUpdate("deleteQueue");
     }
     if ($err->getLevel() > 0) {
         return $err->getErrorResponse($response);
     } else {
         $resp = new \Malmanger\Mpmq\Util\ResponseHandler();
         return $resp->setStatus(204, 'Queue deleted')->getResponse($response);
     }
 }
Beispiel #3
0
}
// Create a log object
$log = \Malmanger\Mpmq\Util\Log::getInstance(\Malmanger\Mpmq\Util\Log::DEBUG);
$log->debug("Logging started");
// initialize database
$data = $settings["settings"]["db"];
$misc = $settings["settings"]["misc"];
$app = new \Slim\App($settings);
// disable error handler
unset($app->getContainer()['errorHandler']);
// Create or connect to DB
try {
    // TODO: do test on settings to confirm correct database type
    $database = new \Malmanger\Mpmq\Db\SQLite($data);
} catch (\Exception $e) {
    $err = new \Malmanger\Mpmq\Util\ErrorHandler();
    $resp = new Slim\Http\Response();
    $log->debug("Error connecting to database:" . $e->getMessage());
    $log->error("Unable to create or connect to database!, exiting");
    $app->respond($err->addFatal($e->getMessage())->getErrorResponse($resp));
    exit;
}
// ___________________
//               _____
// Set up routes _____
// ___________________
// $app->get('/', function($request, $response, $args) {
//     echo 'Hello, world!';
// });
// Register the routes
require 'app/src/routes.php';