/** * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $port = 8900; $server = new Server(); $server->get('/', function (Request $request, Response $response, $next) { $response->writeJson(['ok' => true, 'self' => ['id' => 'U01234567', 'name' => 'testbot'], 'url' => 'wss://localhost:8901/']); $response->end(); }); $runner = new Runner($server); $runner->listen($port); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use CapMousse\ReactRestify\Server; use CapMousse\ReactRestify\Runner; $server = new Server("ReactAPI", "0.0.0.1"); $server->any('/products', 'App\\Controllers\\ProductController')->where('id', '[0-9]?'); $server->on('NotFound', function ($request, $response, $next) { $response->write('You fail, 404'); $response->setStatus(404); $next(); }); $runner = new Runner($server); $runner->listen(1337);
public function buildServer() { $server = new Server("SlackBot", "0.1"); $server->post('/playbook/run/', function (Request $request, Response $response, $next) { $rawData = $request->getData(); $postParser = Registry::get('container')['post_parser']; $parsedData = $postParser->parse($rawData); $playbook = urldecode($parsedData['playbook']); $yamlParser = Registry::get('container')['yaml_parser']; $playbook = $yamlParser->parse($playbook); /** @var SlackApi $slackApi */ $slackApi = Registry::get('container')['slack_api']; $playbookToken = Ar::get($playbook, 'auth.token'); $oldToken = null; if (null !== $playbookToken) { $oldToken = $slackApi->getToken(); $slackApi->setToken($playbookToken); } $executor = new PlaybookExecutor(Registry::get('container')['core_processor']); Variables::clear(); $executor->execute($playbook); $response->write('Playbook executed successfully'); $response->end(); if (null !== $playbookToken) { $slackApi->setToken($oldToken); } $fileName = basename($parsedData['filename']); echo '[INFO] Executing playbook file ' . $fileName . "\n"; $next(); }); $server->post('/command/run/', function (Request $request, Response $response, $next) { $rawData = $request->getData(); $postParser = Registry::get('container')['post_parser']; $parsedData = $postParser->parse($rawData); $command = urldecode($parsedData['command']); $coreProcessor = Registry::get('container')['core_processor']; $dto = new RequestDto(); $dto->setData(['type' => 'message', 'channel' => 'cron', 'user' => 'cron', 'text' => $command, 'ts' => time()]); $coreProcessor->processCommand($dto); $response->write('Playbook executed successfully'); $response->end(); echo '[INFO] Executing command ' . $command . "\n"; $next(); }); $server->post('/process/message/', function (Request $request, Response $response, $next) { $response->sendHeaders(); $response->writeJson(['ok' => true]); $response->end(); $rawData = $request->getData(); $postParser = Registry::get('container')['post_parser']; $parsedData = $postParser->parse($rawData); /** @var CoreProcessor $coreProcessor */ $coreProcessor = Registry::get('container')['core_processor']; $dto = new RequestDto(); $dto->setSource('rtm'); $dto->setData(json_decode(Ar::get($parsedData, 'message'), true)); $coreProcessor->process($dto); $next(); }); $server->get('/info/cron/', function (Request $request, Response $response, $next) { $response->writeJson(Registry::get('container')['config']->getSection('cron')); $response->end(); $next(); }); return $server; }