public function index() { log::prn_log(DEBUG, json_encode($this->content)); log::prn_log(DEBUG, json_encode($this->param)); $db = $this->mysql; $result = $db->gearman_queue->insert(['unique_key' => 'd847233c-1ef2-11e5-9130-2c44fd7aee72', 'function_name' => 'test', 'priority' => 1, 'data' => 'test', 'when_to_run' => 0]); if ($result === false) { return 'error'; } $result = $db->select_one("select * from gearman_queue where unique_key='d847233c-1ef2-11e5-9130-2c44fd7aee72'"); if ($result === false) { return 'error'; } var_dump($result); $result = $db->gearman_queue->update(['function_name' => 'testtest', 'priority' => 100, 'data' => 'testtesttesttest', 'when_to_run' => 100], ['unique_key' => 'd847233c-1ef2-11e5-9130-2c44fd7aee72']); if ($result === false) { return 'error'; } var_dump($db->select_one("select * from gearman_queue where unique_key='d847233c-1ef2-11e5-9130-2c44fd7aee72'")); $result = $db->gearman_queue->delete(['unique_key' => 'd847233c-1ef2-11e5-9130-2c44fd7aee72']); if ($result === false) { return 'error'; } $result = $db->select_more("select * from gearman_queue limit 3"); if ($result === false) { return 'error'; } var_dump($result); return 'ok'; }
public function __construct($config) { $this->config = $config; //var_dump($this->config); $this->dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) { foreach ($this->config['auths'] as $id => $route) { log::prn_log(DEBUG, 'addRoute: ' . json_encode($route)); $r->addRoute($route[0], $route[1], 'func_' . $id); $this->config['auths'][$id]['users'] = []; $users = explode(',', $route[2]); foreach ($users as $user) { if (substr($user, 0, 1) != '@') { if (!in_array($user, $this->config['auths'][$id]['users'])) { $this->config['auths'][$id]['users'][] = $user; } } else { $users2 = explode(',', $this->config['groups'][substr($user, 1)]); foreach ($users2 as $user) { if (!in_array($user, $this->config['auths'][$id]['users'])) { $this->config['auths'][$id]['users'][] = $user; } } } } if (in_array('*', $this->config['auths'][$id]['users'])) { unset($this->config['auths'][$id]['users']); $this->config['auths'][$id]['users'][0] = '*'; } } }); }
public function handel_route($method, $uri) { $route_info = $this->dispatcher->dispatch($method, $uri); log::prn_log(DEBUG, json_encode($route_info)); switch ($route_info[0]) { case FastRoute\Dispatcher::NOT_FOUND: return 404; break; case FastRoute\Dispatcher::METHOD_NOT_ALLOWED: $allow_methods = $route_info[1]; return 405; break; case FastRoute\Dispatcher::FOUND: $handler = $route_info[1][1]; if (substr($handler, 0, 9) === '_handler.') { return call_user_func(array($this, substr($handler, 9)), $route_info); } else { list($ret['class'], $ret['fun']) = explode('.', $handler); $ret['param'] = $route_info[2]; return $ret; } break; } }
<?php /** * @author jiaofuyou@qq.com * @date 2015-10-25 */ require_once __DIR__ . '/swoole.php'; require_once __DIR__ . '/log.php'; require_once __DIR__ . '/mysql.php'; require_once __DIR__ . '/controller.php'; require_once __DIR__ . '/protocol.php'; require_once __DIR__ . '/route.php'; require_once __DIR__ . '/config.php'; require_once __DIR__ . '/fast-route/vendor/autoload.php'; spl_autoload_register(function ($className) { $file = BASE_PATH . '/controller' . "/{$className}.php"; if (file_exists($file)) { log::prn_log(DEBUG, 'require_once: ' . $file); require_once BASE_PATH . '/controller' . "/{$className}.php"; } $file = BASE_PATH . '/protocol' . "/{$className}.php"; if (file_exists($file)) { log::prn_log(DEBUG, 'require_once: ' . $file); require_once BASE_PATH . '/protocol' . "/{$className}.php"; } $file = BASE_PATH . '/config' . "/{$className}.php"; if (file_exists($file)) { log::prn_log(DEBUG, 'require_once: ' . $file); require_once BASE_PATH . '/config' . "/{$className}.php"; } });
public function start() { // 只能单例运行 if ($this->config['is_sington'] == true) { $this->checkPidfile(); } $this->createPidfile(); if (!isset($this->on_func['request'])) { log::prn_log(ERROR, "must set on_request callback function!"); exit; } $this->serv->start(); if (!$this->shutdown) { log::prn_log(ERROR, "swoole start error: " . swoole_errno() . ',' . swoole_strerror(swoole_errno())); } }
public function handle_request($serv, $request, $response) { global $worker_conf; global $route; //var_dump($request); $method = $request->server['request_method']; $uri = $request->server['request_uri']; $header = $request->header; $content = $request->rawContent(); Log::prn_log(INFO, "REQUEST {$method} {$uri} {$content}"); if (in_array($request->server['remote_addr'], $worker_conf['trust_ip'])) { log::prn_log(INFO, "trust ip: {$request->server['remote_addr']}"); return $this->http_pass($serv, $request, $response); } $route_info = $route->handel_route($method, $uri); if ($route_info === 405) { Log::prn_log(NOTICE, "REQUEST {$method} {$uri} {$content}"); return $this->response($response, 405, 'Method Not Allowed, ' . $request->server['request_method']); } if ($route_info === 404) { Log::prn_log(NOTICE, "REQUEST {$method} {$uri} {$content}"); return $this->response($response, 404, "{$uri} is not found!"); } $auth_users = $route_info; if (in_array('*', $auth_users)) { log::prn_log(INFO, "pass user: *"); return $this->http_pass($serv, $request, $response); } if (!isset($header['authorization'])) { Log::prn_log(NOTICE, "REQUEST {$method} {$uri} {$content}"); return $this->response($response, 401, 'Unauthorized', ['WWW-Authenticate' => 'Basic realm=Elasticsearch Auth']); } if (substr($header['authorization'], 0, 6) != 'Basic ') { Log::prn_log(NOTICE, "REQUEST {$method} {$uri} {$content}"); return $this->response($response, 401, 'Unauthorized, only support Basic auth'); } $authorization = base64_decode(substr($header['authorization'], 6)); list($user, $passwd) = explode(':', $authorization, 2); if (!in_array($user, $auth_users)) { Log::prn_log(NOTICE, "REQUEST {$method} {$uri} {$content}"); return $this->response($response, 401, "Unauthorized, invalid user"); } if (!isset($worker_conf['users'][$user])) { Log::prn_log(NOTICE, "REQUEST {$method} {$uri} {$content}"); return $this->response($response, 401, "Unauthorized, user is not found"); } if ($worker_conf['users'][$user] != $passwd) { Log::prn_log(NOTICE, "REQUEST {$method} {$uri} {$content}"); return $this->response($response, 401, "Unauthorized, user passwd error!"); } log::prn_log(INFO, "Basic auth pass"); return $this->http_pass($serv, $request, $response); }
public function start() { // 只能单例运行 if ($this->config['is_sington'] == true) { $this->checkPidfile(); } $this->createPidfile(); $this->serv->start(); if (!$this->shutdown) { log::prn_log(ERROR, "swoole start error: " . swoole_errno() . ',' . swoole_strerror(swoole_errno())); } }