/** * 输出静态文件 * * @param $uri * @param \Swoole\Http\Response $response */ protected function assets($uri, $response) { $uri = str_replace(['\\', '../'], ['/', '/'], $uri); $rPos = strrpos($uri, '.'); if (false === $rPos) { # 没有任何后缀 $response->status(404); $response->end('assets not found'); return; } $type = strtolower(substr($uri, $rPos + 1)); $header = ['js' => 'application/x-javascript', 'css' => 'text/css', 'png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'json' => 'application/json', 'svg' => 'image/svg+xml', 'woff' => 'application/font-woff', 'woff2' => 'application/font-woff2', 'ttf' => 'application/x-font-ttf', 'eot' => 'application/vnd.ms-fontobject', 'html' => 'text/html']; if (isset($header[$type])) { $response->header('Content-Type', $header[$type]); } $file = __DIR__ . '/../../../../assets/' . $uri; if (is_file($file)) { # 设置缓存头信息 $time = 86400; $response->header('Cache-Control', 'max-age=' . $time); $response->header('Pragma', 'cache'); $response->header('Last-Modified', date('D, d M Y H:i:s \\G\\M\\T', filemtime($file))); $response->header('Expires', date('D, d M Y H:i:s \\G\\M\\T', time() + $time)); # 直接发送文件 $response->sendfile($file); } else { $response->status(404); $response->end('assets not found'); } }
/** * HTTP 接口请求处理的方法 * * @param \Swoole\Http\Request $request * @param \Swoole\Http\Response $response */ public function onRequest($request, $response) { $this->request = $request; $this->response = $response; $response->header('Content-Type', 'application/json'); $response->end('{"status":"error","code":0,"msg":"verify fail."}'); try { if (!$this->verify($request)) { $response->status(401); } $uri = $this->uri(); $file = __DIR__ . '/../../../../api/' . $uri . (substr($uri, -1) === '/' ? 'index' : '') . '.php'; $this->debug("request api: {$file}"); if (!is_file($file)) { throw new \Exception('can not found api', 1); } $rs = (include $file); if (!$rs) { throw new \Exception('api result empty', 2); } elseif (is_string($rs)) { $rs = ['data' => $rs, 'status' => 'success']; } elseif (!is_array($rs)) { $rs = (array) $rs; } if (!isset($rs['status'])) { $rs['status'] = 'success'; } $response->end(json_encode($rs, JSON_UNESCAPED_UNICODE)); } catch (\Exception $e) { $response->status(500); $response->end(json_encode(['status' => 'error', 'code' => -$e->getCode(), 'msg' => $e->getMessage()], JSON_UNESCAPED_UNICODE)); } $this->request = null; $this->response = null; }
/** * HTTP 接口请求处理的方法 * * @param \Swoole\Http\Request $request * @param \Swoole\Http\Response $response */ public function onRequest($request, $response) { # 发送一个头信息 $response->header('Server', self::$config['server']['name'] ?: 'MQSRV'); if (isset(self::$workers['API'])) { /** * @var WorkerAPI $worker */ $worker = self::$workers['API']; if ($worker->isApi($request)) { $worker->onRequest($request, $response); return; } } if (isset(self::$workers['Manager'])) { /** * @var WorkerManager $worker */ $worker = self::$workers['Manager']; if ($worker->isManager($request)) { $worker->onRequest($request, $response); return; } } self::$worker->onRequest($request, $response); }