<?php

define('CRAZY_SWOOLE', 1);

include_once(__DIR__.'/api/gateway.php');
include_once(__DIR__.'/api/login.php');
$http = new Swoole\Http\Server('0.0.0.0', 9501);

$http->set([
    'daemonize'          => true,
    'worker_num'         => 8,
    //'reactor_num'     => 8,
    'open_tcp_keepalive' => true, //单位秒，连接在 n 秒内没有数据请求，将开始对此连接进行探测。
    'tcp_keepidle'       => 3, //3s探测一次
    'tcp_keepcount'      => 5, //探测的次数，超过5次后还没回包close此连接
    'log_level'          => SWOOLE_LOG_TRACE,
    'log_date_format'    => '%Y-%m-%d %H:%M:%S',
]);

$http->on('request', function ($request, $response) {
    if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') {
        $response->end();
        return;
    }
    $response->header("Content-Type", "text/html; charset=utf-8");
    $ret = '[]';
    $requestUri = ltrim($request->server['request_uri'], '/');
    if(!in_array($requestUri, ['api/gateway.php', 'api/login.php'])){
        $response->end($ret);
        return;
    }
    if($requestUri == 'api/login.php'){
        $response->header("Access-Control-Allow-Origin:*");
        $response->header("Access-Control-Allow-Method:*");
        $response->header("Access-Control-Allow-Headers:Content-Type");
        $ret = (new Login($request->post, $request->get))->start();
    }else{
        if(IS_DEBUF){
            $response->header("Access-Control-Allow-Origin:*");//允许跨域
        }else{
            $response->header("Access-Control-Allow-Origin:*");//允许跨域
        }
        $ret = (new gateway($request->post, $request->get))->start();
    }
    $response->end($ret);
});

$http->start();
