You've already forked Php-Template
176 lines
5.4 KiB
PHP
176 lines
5.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Siteworxpro\App;
|
|
|
|
use Illuminate\Container\Container;
|
|
use Illuminate\Support\Facades\Facade;
|
|
use League\Route\Http\Exception\MethodNotAllowedException;
|
|
use League\Route\Http\Exception\NotFoundException;
|
|
use League\Route\Router;
|
|
use Nyholm\Psr7\Factory\Psr17Factory;
|
|
use Siteworxpro\App\Facades\Config;
|
|
use Siteworxpro\App\Facades\Logger;
|
|
use Siteworxpro\App\Http\JsonResponseFactory;
|
|
use Siteworxpro\App\Http\Middleware\CorsMiddleware;
|
|
use Spiral\RoadRunner\Http\PSR7Worker;
|
|
use Spiral\RoadRunner\Worker;
|
|
|
|
/**
|
|
* Class Server
|
|
*
|
|
* This class represents the main server application.
|
|
* It handles incoming HTTP requests, routes them to the appropriate handlers,
|
|
* and manages the server lifecycle.
|
|
*
|
|
* @package Siteworxpro\App
|
|
*/
|
|
class Server
|
|
{
|
|
/**
|
|
* @var Router The router instance for handling routes.
|
|
*/
|
|
protected Router $router;
|
|
|
|
/**
|
|
* @var PSR7Worker The PSR-7 worker instance for handling HTTP requests.
|
|
*/
|
|
protected PSR7Worker $worker;
|
|
|
|
|
|
/**
|
|
* Server constructor.
|
|
*
|
|
* Initializes the server by booting the PSR-7 worker and router.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->boot();
|
|
}
|
|
|
|
/**
|
|
* Bootstraps the server by initializing the PSR-7 worker and router.
|
|
*
|
|
* This method sets up the PSR-7 worker and router instances, and registers
|
|
* the routes for the server. It should be called in the constructor of
|
|
* subclasses to ensure proper initialization.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function boot(): void
|
|
{
|
|
$container = new Container();
|
|
Facade::setFacadeApplication($container);
|
|
|
|
$this->worker = new PSR7Worker(
|
|
Worker::create(),
|
|
new Psr17Factory(),
|
|
new Psr17Factory(),
|
|
new Psr17Factory()
|
|
);
|
|
|
|
$this->router = new Router();
|
|
|
|
$this->registerRoutes();
|
|
$this->bootModelCapsule();
|
|
}
|
|
|
|
/**
|
|
* Bootstraps the model capsule for database connections.
|
|
*
|
|
* This method sets up the database connection using the Eloquent ORM.
|
|
* It retrieves the database configuration from the Config facade and
|
|
* initializes the Eloquent capsule manager.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function bootModelCapsule(): void
|
|
{
|
|
$capsule = new \Illuminate\Database\Capsule\Manager();
|
|
$capsule->addConnection([
|
|
'driver' => Config::get('db.driver'),
|
|
'host' => Config::get('db.host'),
|
|
'database' => Config::get('db.database'),
|
|
'username' => Config::get('db.username'),
|
|
'password' => Config::get('db.password'),
|
|
'charset' => 'utf8',
|
|
'collation' => 'utf8_unicode_ci',
|
|
'prefix' => '',
|
|
]);
|
|
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
}
|
|
|
|
/**
|
|
* Registers the routes for the server.
|
|
*
|
|
* This method is responsible for defining the routes that the server will handle.
|
|
* It should be implemented in subclasses to provide specific route definitions.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function registerRoutes(): void
|
|
{
|
|
$this->router->get('/', function () {
|
|
return JsonResponseFactory::createJsonResponse(['status_code' => 200, 'message' => 'Server is running']);
|
|
});
|
|
|
|
$this->router->middleware(new CorsMiddleware());
|
|
}
|
|
|
|
/**
|
|
* Starts the server and handles incoming requests.
|
|
*
|
|
* This method enters an infinite loop to continuously handle incoming HTTP requests.
|
|
* It decodes the request body, routes the request, and sends the response. It also handles
|
|
* exceptions and ensures proper cleanup after each request.
|
|
*
|
|
* @throws \JsonException If there is an error decoding the JSON request body.
|
|
*/
|
|
public function startServer(): void
|
|
{
|
|
Logger::info(sprintf('Server started: %s', microtime(true)));
|
|
Logger::info(sprintf('Server PID: %s', getmypid()));
|
|
Logger::info(sprintf('Server Listening on: 0.0.0.0:%s', Config::get('server.port')));
|
|
|
|
while (true) {
|
|
try {
|
|
$request = $this->worker->waitRequest();
|
|
|
|
if ($request === null) {
|
|
break;
|
|
}
|
|
|
|
$request = $request->withParsedBody(json_decode($request->getBody()->getContents(), true));
|
|
|
|
$response = $this->router->handle($request);
|
|
$this->worker->respond($response);
|
|
} catch (MethodNotAllowedException | NotFoundException) {
|
|
$this->worker->respond(
|
|
JsonResponseFactory::createJsonResponse(
|
|
['status_code' => 404, 'reason_phrase' => 'Not Found'],
|
|
404
|
|
)
|
|
);
|
|
} catch (\Throwable $e) {
|
|
Logger::error($e->getMessage());
|
|
Logger::error($e->getTraceAsString());
|
|
|
|
$json = ['status_code' => 500, 'reason_phrase' => 'Server Error'];
|
|
if (Config::get("server.dev_mode")) {
|
|
$json = [
|
|
'status_code' => 500,
|
|
'reason_phrase' => 'Server Error',
|
|
'message' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
];
|
|
}
|
|
|
|
$this->worker->respond(JsonResponseFactory::createJsonResponse($json, 500));
|
|
}
|
|
}
|
|
}
|
|
}
|