Skip to content

Commit

Permalink
Update start-swow-http.php
Browse files Browse the repository at this point in the history
  • Loading branch information
getpinga authored Mar 14, 2023
1 parent 35872f1 commit 6cdfc85
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions start-swow-http.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Dotenv\Dotenv;

require_once __DIR__ . "/vendor/autoload.php";
$routeCollection = require __DIR__ . "/config/routes.php";
$app['route'] = require __DIR__ . "/config/routes.php";

// Load environment variables from .env file
$dotenv = Dotenv::createImmutable(__DIR__);
Expand All @@ -31,15 +31,9 @@
$server->bind($host, $port)->listen(Socket::DEFAULT_BACKLOG);
file_put_contents($_ENV['PIDFILE'], getmypid());

// Choose one
// Log to a file
//$logger = new PiLogger('/tmp/file.log');
// Log to stdout
$logger = new PiLogger(null, true);
// Log to both file and stdout
//$logger = new PiLogger($logFilePath, true);
$app['logger'] = new PiLogger(null, true);

$logger->info(
$app['logger']->info(
sprintf("Pinglet Swow running at http://%s:%s", $hostname, $port)
);

Expand All @@ -49,8 +43,7 @@
$connection = $server->acceptConnection();
Coroutine::run(static function () use (
$connection,
$routeCollection,
$logger
$app
): void {
try {
while (true) {
Expand Down Expand Up @@ -84,7 +77,7 @@
->withParsedBody($request->post ?? [])
->withUploadedFiles($request->files ?? []);

$routeInfo = $routeCollection->dispatch(
$routeInfo = $app['route']->dispatch(
$request_method,
$request_uri
);
Expand All @@ -108,13 +101,13 @@
"@",
$handler
);
$controller = new $controllerClass();
$controller = new $controllerClass($app);
$serverReponse = $controller->$method(
$serverRequest
);
} elseif (is_array($handler)) {
[$controllerClass, $method] = $handler;
$controller = new $controllerClass();
$controller = new $controllerClass($app);
$serverReponse = $controller->$method(
$serverRequest
);
Expand Down Expand Up @@ -153,7 +146,7 @@
break;
}
} catch (HttpProtocolException $exception) {
$logger->error($exception->getMessage());
$app['logger']->error($exception->getMessage());
$connection->error(
$exception->getCode(),
$exception->getMessage(),
Expand All @@ -166,7 +159,7 @@
}
}
} catch (Exception $exception) {
$logger->error($exception->getMessage());
$app['logger']->error($exception->getMessage());
} finally {
$connection->close();
}
Expand Down

0 comments on commit 6cdfc85

Please sign in to comment.