HttpServer.php 1.37 KB
<?php

require_once __DIR__.'/Sender.php';

$serv = new swoole_http_server("0.0.0.0", Config::http_port);
$serv->set(array(
    'worker_num' => Config::worker_num,
    'daemonize' => Config::daemonize, //是否作为守护进程
    'log_file' => Config::log_file,
));

$serv->on('start', function (swoole_server $serv){
    echo "Client:Start.\n";
});

$serv->on('Request', function(swoole_http_request $request, swoole_http_response $response) 
{
    $response->end();
    
    $ip = $request->server['remote_addr'];
    $get = !empty($request->get) ? $request->get : array();
    $post = !empty($request->post) ? $request->post : array();
    
    $ouput = "HTTP Request--\n";
    $ouput .= sprintf("Receive request fd: %d, ip: %s, method: %s\n", $request->fd, $ip, $request->server['request_method']);
    $ouput .= date('Y-m-d H:i:s')."\n";
    $ouput .= "Request query : \n";
    $ouput .= $get ? json_encode($get)."\n" : '';
    $ouput .= $post ? json_encode($post)."\n" : '';
    $ouput .= "\r\n\r\n";
    Sender::log($ouput);
    
    $data = $get + $post;
    
    $tags = array(
        'ip'=>$ip,
        'hostname'=>!empty($data['hostname']) ? $data['hostname'] : gethostbyaddr($ip),
    );
    $type = !empty($data['type']) ? $data['type'] : '';
    Sender::add($type,$data,$tags);
});

$serv->on('close', function (swoole_server $serv, $fd) {
    echo "Client: Close.\n";
});

$serv->start();