99use Swoole \Coroutine \Http \Server as CoroutineServer ;
1010use Swoole \Http \Request ;
1111use Swoole \Http \Response ;
12+ use Utopia \Console ;
1213use Utopia \Proxy \Adapter ;
1314use Utopia \Proxy \Protocol ;
1415use Utopia \Proxy \Resolver ;
@@ -84,7 +85,7 @@ protected function configure(): void
8485 protected function initAdapter (): void
8586 {
8687 $ this ->adapter = new Adapter ($ this ->resolver , name: 'HTTP ' , protocol: Protocol::HTTP );
87- $ this ->adapter ->setCacheTtl ($ this ->config ->cacheTTL );
88+ $ this ->adapter ->setCacheTTL ($ this ->config ->cacheTTL );
8889
8990 if ($ this ->config ->skipValidation ) {
9091 $ this ->adapter ->setSkipValidation (true );
@@ -93,21 +94,37 @@ protected function initAdapter(): void
9394
9495 public function onStart (): void
9596 {
96- echo "HTTP Proxy Server started at http:// {$ this ->config ->host }: {$ this ->config ->port }\n" ;
97- echo "Workers: {$ this ->config ->workers }\n" ;
98- echo "Max connections: {$ this ->config ->maxConnections }\n" ;
97+ Console:: success ( "HTTP Proxy Server started at http:// {$ this ->config ->host }: {$ this ->config ->port }" ) ;
98+ Console:: log ( "Workers: {$ this ->config ->workers }" ) ;
99+ Console:: log ( "Max connections: {$ this ->config ->maxConnections }" ) ;
99100 }
100101
101102 public function onWorkerStart (int $ workerId = 0 ): void
102103 {
103- echo "Worker # {$ workerId } started (Adapter: {$ this ->adapter ->getName ()}) \n" ;
104+ if ($ this ->config ->workerStart !== null ) {
105+ ($ this ->config ->workerStart )(null , $ workerId , $ this ->adapter );
106+ }
107+
108+ Console::log ("Worker # {$ workerId } started (Adapter: {$ this ->adapter ->getName ()}) " );
104109 }
105110
106111 /**
107112 * Main request handler
108113 */
109114 public function onRequest (Request $ request , Response $ response ): void
110115 {
116+ if ($ this ->config ->requestHandler !== null ) {
117+ try {
118+ ($ this ->config ->requestHandler )($ request , $ response , $ this ->adapter );
119+ } catch (\Throwable $ e ) {
120+ Console::error ("Request handler error: {$ e ->getMessage ()}" );
121+ $ response ->status (500 );
122+ $ response ->end ('Internal Server Error ' );
123+ }
124+
125+ return ;
126+ }
127+
111128 try {
112129 if ($ this ->config ->directResponse !== null ) {
113130 $ response ->status ($ this ->config ->directResponseStatus );
@@ -157,7 +174,7 @@ public function onRequest(Request $request, Response $response): void
157174 }
158175
159176 } catch (\Exception $ e ) {
160- error_log ("Proxy error: {$ e ->getMessage ()} in {$ e ->getFile ()}: {$ e ->getLine ()}" );
177+ Console:: error ("Proxy error: {$ e ->getMessage ()} in {$ e ->getFile ()}: {$ e ->getLine ()}" );
161178
162179 $ response ->status (503 );
163180 $ response ->header ('Content-Type ' , 'application/json ' );
@@ -173,8 +190,7 @@ public function onRequest(Request $request, Response $response): void
173190 */
174191 protected function forwardRequest (Request $ request , Response $ response , string $ endpoint , ?Telemetry $ telemetry = null ): void
175192 {
176- [$ host , $ port ] = explode (': ' , $ endpoint . ':80 ' );
177- $ port = (int ) $ port ;
193+ [$ host , $ port ] = Adapter::parseEndpoint ($ endpoint , 80 );
178194
179195 $ poolKey = "{$ host }: {$ port }" ;
180196 if (!isset ($ this ->pools [$ poolKey ])) {
@@ -304,8 +320,7 @@ protected function forwardRawRequest(Request $request, Response $response, strin
304320 return ;
305321 }
306322
307- [$ host , $ port ] = explode (': ' , $ endpoint . ':80 ' );
308- $ port = (int ) $ port ;
323+ [$ host , $ port ] = Adapter::parseEndpoint ($ endpoint , 80 );
309324
310325 $ poolKey = "raw: {$ host }: {$ port }" ;
311326 if (!isset ($ this ->pools [$ poolKey ])) {
0 commit comments