You've already forked Php-Template
TODO: Replace stubs
This commit is contained in:
170
src/Server.php
Normal file
170
src/Server.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Abstract class Server
|
||||
*
|
||||
* This abstract class serves as a base for creating server instances.
|
||||
* It initializes the PSR-7 worker and router, and provides an abstract method
|
||||
* for registering routes. It also includes a method to start the server and handle
|
||||
* incoming requests.
|
||||
*/
|
||||
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
|
||||
{
|
||||
$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(Config::DB_DRIVER),
|
||||
'host' => Config::get(Config::DB_HOST),
|
||||
'database' => Config::get(Config::DB_DATABASE),
|
||||
'username' => Config::get(Config::DB_USER),
|
||||
'password' => Config::get(Config::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: %s:%s', Config::get('HTTP_HOST'), Config::get('HTTP_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("DEV_MODE", 'bool')) {
|
||||
$json = [
|
||||
'status_code' => 500,
|
||||
'reason_phrase' => 'Server Error',
|
||||
'message' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
];
|
||||
}
|
||||
|
||||
$this->worker->respond(JsonResponseFactory::createJsonResponse($json, 500));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user