public static function startHttp($options) { $tcpOptions = ranch_utils::config($options, 'tcp', []); $httpOptions = ranch_utils::config($options, 'http', []); cowboy_router::compile($options['routes']); cowboy::startHttp($tcpOptions, $httpOptions); }
function init(cowboy_request $req, array $opts) { $handler = ranch_utils::config($opts, 'handler'); if (!$handler) { $req->reply(500); } else { return new $handler(); } }
public static function compile($routes) { $compiled = []; foreach ($routes as $path => $params) { if (count($params) < 2) { ranch_utils::halt("Route params count should be >= 3"); } $path = cowboy_utils::addTrailingSlash($path); $route['handler'] = $params[0]; $route['opts'] = $params[1]; $route['path'] = $path; $pattern = preg_replace("/(\\:[^\\/]+)\\//", "([^\\/]+)\\/", str_replace('/', '\\/', $path)); $route['pattern'] = '/^' . $pattern . '$/'; preg_match_all("/(\\:[^\\/]+)/", "{$path}", $m); $route['bindings'] = array_map(function ($x) { return ltrim($x, ":"); }, array_filter($m[0])); $route['match'] = (bool) $route['bindings']; self::$compiled[$path] = $route; } }
public function __construct(array $opts, $protocol, $protoOpts) { $this->opts = ['port' => ranch_utils::config($opts, 'port', 3000)]; $this->protocol = new $protocol($protoOpts); }
public function init(cowboy_request $req, array $opts) { $module = ranch_utils::config($opts, 'module'); $function = ranch_utils::config($opts, 'function'); return $module::$function($req); }