server.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Workerman HTTP server startup file
  4. * Based on Workerman to implement an HTTP server, integrated with the ThinkPHP framework
  5. */
  6. use Workerman\Worker;
  7. use Workerman\Connection\TcpConnection;
  8. use Workerman\Protocols\Http\Request as WorkermanRequest;
  9. use Cyber\Response;
  10. // Load Composer autoload file
  11. require __DIR__ . '/vendor/autoload.php';
  12. // Create a Worker listening on port 8099, using HTTP protocol for communication
  13. $http_worker = new Worker("http://0.0.0.0:8080");
  14. // Set the number of processes (set according to the number of CPU cores, recommended to be 1-4 times the number of CPU cores)
  15. $http_worker->count = 4;
  16. // Initialize ThinkPHP application
  17. $app = \Cyber\App::bootstrap(__DIR__.'/bootstrap.php');
  18. /**
  19. * Callback function to handle HTTP requests
  20. * @param TcpConnection $connection Client connection object
  21. * @param WorkermanRequest $request HTTP request object
  22. */
  23. $http_worker->onMessage = function(TcpConnection $connection, WorkermanRequest $request) use ($app) {
  24. // Initialize request object
  25. $_GET = $request->get(); // Get GET parameters
  26. $_POST = $request->post(); // Get POST parameters
  27. $_FILES = $request->file(); // Get file uploads
  28. $_COOKIE = $request->cookie(); // Get COOKIE
  29. // Merge server variables
  30. $_SERVER = array_merge($_SERVER, [
  31. 'RAW_BODY' => $request->rawBody(), // Raw request body
  32. 'REQUEST_METHOD' => $request->method(), // Request method
  33. 'REQUEST_URI' => $request->uri(), // Request URI
  34. 'QUERY_STRING' => $request->queryString(), // Query string
  35. 'REMOTE_ADDR' => $connection->getRemoteIp(), // Client IP
  36. 'REMOTE_PORT' => $connection->getRemotePort(), // Client port
  37. 'SERVER_PROTOCOL' => 'HTTP/'.$request->protocolVersion(), // Protocol version
  38. ]);
  39. // Handle request headers
  40. foreach ($request->header() as $key => $value) {
  41. $_SERVER['HTTP_' . strtoupper(str_replace('-', '_', $key))] = $value;
  42. }
  43. try {
  44. ob_start(); // Start output buffering
  45. $response = $app->run(); // Run ThinkPHP application
  46. // Handle response
  47. if(!$response instanceof Response){
  48. // If not a Response object, directly output content
  49. echo $response;
  50. $content = ob_get_clean();
  51. $connection->send($content);
  52. }else{
  53. // If it is a Response object, send HTTP response
  54. echo $response->send();
  55. $content = ob_get_clean();
  56. $connection->send(new Workerman\Protocols\Http\Response(
  57. $response->getStatusCode(), // Status code
  58. $response->getHeaders(), // Response headers
  59. $content // Response content
  60. ));
  61. }
  62. } catch (Exception $e) {
  63. // Catch exceptions and render error page
  64. $connection->send(renderExceptionPage($e));
  65. } catch (Throwable $e) {
  66. // Catch all errors
  67. $connection->send(renderExceptionPage($e));
  68. }
  69. };
  70. /**
  71. * Run all Worker instances
  72. * This method will block the current process until all Workers stop
  73. */
  74. Worker::runAll();