Browse Source

Update ngx_php fortunes test (#5026)

* Update ngx_php version

* Update the fortune of ngx_php7

* Update php-ngx.dockerfile

* Try to use ENV like lua for now

* Combine ENV and nginx

* Update nginx_php conf

* Update ngx_php to v0.19

* Fixed parse host tfb-database failed.

* Fixed Spelling mistake, should be DB_HOST.
Joan Miquel 6 years ago
parent
commit
bb0410f7e0
2 changed files with 40 additions and 8 deletions
  1. 37 7
      frameworks/PHP/php/deploy/nginx_php.conf
  2. 3 1
      frameworks/PHP/php/php-ngx.dockerfile

+ 37 - 7
frameworks/PHP/php/deploy/nginx_php.conf

@@ -24,14 +24,24 @@ http {
 
     php_ini_path /deploy/conf/php.ini;
 
+    init_worker_by_php '
+        define("DB_HOST", gethostbyname("tfb-database"));
+        define("DB_PORT", "3306");
+        define("DB_USER", "benchmarkdbuser");
+        define("DB_PASS", "benchmarkdbpass");
+        define("DB_NAME", "hello_world");
+    ';
+
     server {
         listen       8080 default_server reuseport;
 
         root /;
         index  index.html;
 
+        php_keepalive 100;
+
         location = /hello {
-	        add_header Content-Type text/plain;
+            add_header Content-Type text/plain;
             content_by_php '
                 echo "Hello, World!";
             ';
@@ -45,22 +55,42 @@ http {
         }
         
         location = /fortune {
-            add_header Content-Type "text/html; charset=UTF-8";
-	        content_by_php '
-                include "fortune.php";
+            content_by_php '
+                require_once("/ngx_php7/t/lib/mysql.php");
+                ngx_header_set("Content-Type", "text/html; charset=UTF-8");
+
+                $my = new php\\ngx\mysql();
+                yield from $my->connect(DB_HOST, DB_PORT, DB_USER, DB_PASS, DB_NAME);
+                $ret = yield from $my->query("SELECT id, message FROM Fortune");
+                
+                $arr = [];
+                foreach ($ret as $row) {
+                        $arr[$row["id"]] = $row["message"];
+                }
+                $arr[0] = "Additional fortune added at request time.";
+                asort($arr);
+                
+                $html = "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>\n";
+                foreach ( $arr as $id => $fortune ) {
+                        $html.="<tr><td>{$id}</td><td>".htmlspecialchars($fortune, ENT_QUOTES, "UTF-8")."</td></tr>\n";
+                }
+                $html .= "</table></body></html>\n";
+                echo $html;
+
+                yield from $my->close();
             ';
         }
 
         location = /dbraw {
             add_header Content-Type application/json;
-	        content_by_php '
+            content_by_php '
                 include "dbraw.php";
             ';
         }
 
         location /dbquery {
             add_header Content-Type application/json;
-	        content_by_php '
+            content_by_php '
                 $_GET = ngx::query_args();
                 include "dbquery.php";
             ';
@@ -68,7 +98,7 @@ http {
 
         location /update {
             add_header Content-Type application/json;
-	        content_by_php '
+            content_by_php '
                 $_GET = ngx::query_args();
                 include "updateraw.php";
             ';

+ 3 - 1
frameworks/PHP/php/php-ngx.dockerfile

@@ -26,4 +26,6 @@ RUN wget -q http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \
             --add-module=/ngx_php7 > /dev/null && \
     make > /dev/null && make install > /dev/null
 
-CMD /nginx/sbin/nginx -c /deploy/nginx_php.conf
+CMD export DBIP=`getent hosts tfb-database | awk '{ print $1 }'` && \
+         /nginx/sbin/nginx -c /deploy/nginx_php.conf
+