siler icon indicating copy to clipboard operation
siler copied to clipboard

API returns 'Not Found' after first request on v1.7.9

Open leyume opened this issue 4 years ago • 2 comments

Hello @leocavalcante Thank you for Siler.

There's this strange issue with v1.7.9 API returns 'Not Found' after first request with Siler Swoole

This worked fine up to version 1.7.8


Simple code to recreate

index.php

<?php 
declare(strict_types=1);
require_once 'vendor/autoload.php';

use Siler\Swoole;
use Siler\Route;

$server = function () {
    Swoole\cors( '*', 'Authorization, Content-Type', 'GET,POST,PUT' );

    try {
        Route\files('api');
    } catch (Exception $e) {
        Resource::error($e->errorInfo[2]);
    }
    
    Swoole\emit('Not found', 404);
};

Swoole\http($server)->start();

Then, api/get.php

<?php
use Siler\Swoole;

return function () {
    Swoole\json( ['greet'=>'Hello', 'name'=>'Leo'] );
};

Request API more than once. You would see the "Not Found"

leyume avatar Feb 06 '21 04:02 leyume

I encountered this behavior while following the official documentation.

I added a second emit to verify it.

The first visit returns the route properly, at the second attempt it bypasses all other Routes and triggers the first Swoole\emit it encounters so in my case it returns 'Hello World' rather than 'Not Found'.

index.php:

<?php declare(strict_types=1);

require_once 'vendor/autoload.php';

use Siler\Swoole;
use Siler\Route;
use Siler\Twig;

Twig\init('pages');

$handler = function ($req) {
  Route\get('/', 'pages/home.php');
  Route\get('/todos', 'api/todos.php');
  Swoole\emit('Hello World');
  Swoole\emit('Not found', 404);
};

Swoole\http($handler)->start();

pages/home.php

<?php declare(strict_types=1);

use Siler\Swoole;
use Siler\Twig;

return fn() => Swoole\emit(Twig\render('home.twig'));

api/todos.php

<?php declare(strict_types=1);

use Siler\Swoole;

return function () {
    Swoole\cors();
    Swoole\json([
      ['id' => 1, 'text' => 'foo'],
      ['id' => 2, 'text' => 'bar'],
      ['id' => 3, 'text' => 'baz'],
  ]);
};

ogfremo avatar Apr 16 '21 14:04 ogfremo

Exactly same behavior for me. Using fresh installed swoole with pecl and php 7.4, siler from composer.

jrborbars avatar Jun 03 '21 02:06 jrborbars