Swoole 实现路由功能的Http服务器

 1 <?php
 2 include_once 'error.php';//文件代码在下方
 3 $http=new \swoole\http\server('0.0.0.0',9502);
 4 $http->set([
 5     'package_max_length'=>1024*1024*10,
 6     'upload_tmp_dir'=>__DIR__.'/upload'
 7 ]);
 8 //监听http协议
 9 $http->on('request',function ($request,$response){
10     $response->header('Content-Type','text/html');
11     $response->header('Charset','utf-8');
12     $server=$request->server;
13     $path_info=$server['path_info'];
14     if($path_info=='/'){
15         $path_info='/';
16     }else{
17         $path_info=explode('/',$path_info);
18     }
19     if(!is_array($path_info)) {
20         $response->status(404);
21         $response->end('<meta charset="UTF-8">请求路径无效');
22     }
23     //模块
24     $model=(isset($path_info[1]) && !empty($path_info[1]))?$path_info[1]:'Swooleman';
25     //控制器
26     $controller=(isset($path_info[2]) && !empty($path_info[2]))?$path_info[2]:'error';
27     //方法
28     $method=(isset($path_info[3]) && !empty($path_info[3]))?$path_info[3]:'index';
29     //结合错误处理
30     $class_name="\\{$model}\\$controller";
31     if($class_name == '\favicon.ico\Index'||$class_name == '\favicon.ico\error' ){
32            return ;
33     }
34     //判断控制器类是否存在
35    if(file_exists(__DIR__.'/../'.str_replace('\\', '/', $class_name).'.php')){
36        require_once __DIR__.'/../'.str_replace('\\', '/', $class_name).'.php';
37        $obj= new $class_name;
38        //判断控制器方法是否存在
39        if(!method_exists($obj,$method)){
40            $response->status(404);
41            $response->end("<meta charset='UTF-8'>兄Dei,方法不存在,别瞎几把访问好吗");
42        }else{
43            //如果存在此方法,返回结果,return 无效
44            $response->end($obj->$method());
45        }
46    }else{
47        $response->status(404);
48        $response->end("<meta charset='UTF-8'>兄Dei,类不存在,别瞎几把访问好吗");
49    }
50 });
51 //启动http服务器
52 $http->start();
 1 <?php
 2 
 3 register_shutdown_function('handleFatal');
 4 //进程关闭时触发
 5 
 6 //发送错误信息到某个设备\发送邮件
 7 
 8 
 9 function handleFatal()
10 {
11     $error = error_get_last(); //获取最后的错误
12     if (isset($error['type']))
13     {
14         switch ($error['type'])
15         {
16             case E_ERROR :
17             case E_PARSE :
18             case E_CORE_ERROR :
19             case E_COMPILE_ERROR :
20                 $message = $error['message'];
21                 $file = $error['file'];
22                 $line = $error['line'];
23                 $log = "$message ($file:$line)\nStack trace:\n";
24                 $trace = debug_backtrace();
25                 foreach ($trace as $i => $t)
26                 {
27                     if (!isset($t['file']))
28                     {
29                         $t['file'] = 'unknown';
30                     }
31                     if (!isset($t['line']))
32                     {
33                         $t['line'] = 0;
34                     }
35                     if (!isset($t['function']))
36                     {
37                         $t['function'] = 'unknown';
38                     }
39                     $log .= "#$i {$t['file']}({$t['line']}): ";
40                     if (isset($t['object']) and is_object($t['object']))
41                     {
42                         $log .= get_class($t['object']) . '->';
43                     }
44                     $log .= "{$t['function']}()\n";
45                 }
46                 if (isset($_SERVER['REQUEST_URI']))
47                 {
48                     $log .= '[QUERY] ' . $_SERVER['REQUEST_URI'];
49                 }
50                 file_put_contents(__DIR__.'/log.log',$log);//错误日志的输出
51                 return $log;
52                 //echo $log;
53             default:
54                 break;
55         }
56     }
57 }

 

posted @ 2018-09-14 14:07  阿鸠  阅读(1471)  评论(0编辑  收藏  举报