Pārlūkot izejas kodu

:hammer: add php rpc server demo

Jeremy Zheng 2 gadi atpakaļ
vecāks
revīzija
5d87d574da

+ 29 - 0
clients/README.md

@@ -0,0 +1,29 @@
+# USAGE
+
+## Setup
+
+- for PHP
+
+  ```bash
+  sudo apt install php-grpc-all-dev
+  ```
+
+## Server
+
+- Node server demo
+
+  ![node-server](documents/node-server.png)
+
+- PHP Server demo
+
+  ![php-server](documents/php-server.png)
+
+## Client
+
+- PHP
+
+  ```bash
+  php -d extension=grpc.so -d max_execution_time=300 morus-demo.php
+  ```
+
+  ![client](documents/client.png)

+ 0 - 0
clients/php/documents/client.png → clients/documents/client.png


+ 0 - 0
clients/php/documents/server.png → clients/documents/node-server.png


BIN
clients/documents/php-server.png


+ 1 - 1
clients/java/com/github/iapt_platform/mint/plugins/morus/v1/MarkdownGrpc.java

@@ -8,7 +8,7 @@ import static io.grpc.MethodDescriptor.generateFullMethodName;
  * </pre>
  */
 @javax.annotation.Generated(
-    value = "by gRPC proto compiler (version 1.57.2)",
+    value = "by gRPC proto compiler (version 1.58.0)",
     comments = "Source: morus.proto")
 @io.grpc.stub.annotations.GrpcGenerated
 public final class MarkdownGrpc {

+ 42 - 0
clients/php/Mint/Morus/V1/MarkdownStub.php

@@ -0,0 +1,42 @@
+<?php
+// GENERATED CODE -- DO NOT EDIT!
+
+namespace Mint\Morus\V1;
+
+/**
+ * ----------------------------------------------------------------------------
+ */
+class MarkdownStub {
+
+    /**
+     * @param \Mint\Morus\V1\MarkdownToHtmlRequest $request client request
+     * @param \Grpc\ServerContext $context server request context
+     * @return \Mint\Morus\V1\MarkdownToHtmlResponse for response data, null if if error occured
+     *     initial metadata (if any) and status (if not ok) should be set to $context
+     */
+    public function ToHtml(
+        \Mint\Morus\V1\MarkdownToHtmlRequest $request,
+        \Grpc\ServerContext $context
+    ): ?\Mint\Morus\V1\MarkdownToHtmlResponse {
+        $context->setStatus(\Grpc\Status::unimplemented());
+        return null;
+    }
+
+    /**
+     * Get the method descriptors of the service for server registration
+     *
+     * @return array of \Grpc\MethodDescriptor for the service methods
+     */
+    public final function getMethodDescriptors(): array
+    {
+        return [
+            '/mint.morus.v1.Markdown/ToHtml' => new \Grpc\MethodDescriptor(
+                $this,
+                'ToHtml',
+                '\Mint\Morus\V1\MarkdownToHtmlRequest',
+                \Grpc\MethodDescriptor::UNARY_CALL
+            ),
+        ];
+    }
+
+}

+ 0 - 10
clients/php/README.md

@@ -1,10 +0,0 @@
-# USAGE
-
-![server](documents/server.png)
-
-```bash
-sudo apt install php-grpc-all-dev
-php -d extension=grpc.so -d max_execution_time=300 morus-demo.php
-```
-
-![client](documents/client.png)

+ 24 - 0
clients/php/morus-server.php

@@ -0,0 +1,24 @@
+<?php
+
+require dirname(__FILE__) . '/vendor/autoload.php';
+
+class Greeter extends \Mint\Morus\V1\MarkdownStub
+{
+    public function ToHtml(
+        \Mint\Morus\V1\MarkdownToHtmlRequest $request,
+        \Grpc\ServerContext $context
+    ): ?\Mint\Morus\V1\MarkdownToHtmlResponse {
+        $text = $request->getPayload();
+        echo 'Received request: ' . $text . PHP_EOL;
+        $response = new \Mint\Morus\V1\MarkdownToHtmlResponse();
+        $response->setPayload("Hello <h1>" . $text . "</h1>");
+        return $response;
+    }
+}
+
+$port = 9999;
+$server = new \Grpc\RpcServer();
+$server->addHttp2Port('0.0.0.0:' . $port);
+$server->handle(new Greeter());
+echo 'Listening on port :' . $port . PHP_EOL;
+$server->run();

+ 26 - 2
clients/schema.sh

@@ -22,13 +22,36 @@ function generate_grpc_by_lang() {
         $WORKSPACE/../protocols/*.proto
 }
 
+function generate_grpc_for_php() {
+    local target=$WORKSPACE/php
+    echo "generate code for php"
+
+    local -a folders=(
+        "GPBMetadata"
+        "Mint"
+    )
+
+    for f in "${folders[@]}"
+    do
+        if [ -d $target/$f ]
+        then
+            rm -r $target/$f
+        fi
+    done
+    mkdir -p $target
+    $PROTOBUF_ROOT/bin/protoc -I $WORKSPACE/../protocols \
+        -I $PROTOBUF_ROOT/include/google/protobuf \
+        --php_out=$target --grpc_out=generate_server:$target \
+        --plugin=protoc-gen-grpc=$PROTOBUF_ROOT/bin/grpc_php_plugin \
+        $WORKSPACE/../protocols/*.proto
+}
+
 # -----------------------------------------------------------------------------
 
 declare -a languages=(
-    "php"
+    "cpp"
     "python"
     "ruby"
-    "cpp"
     "csharp"
     "java"
 )
@@ -38,6 +61,7 @@ do
     generate_grpc_by_lang $l
 done
 
+generate_grpc_for_php
 # -----------------------------------------------------------------------------
 
 echo 'done.'