1. 程式人生 > 其它 >使用shell做http web介面,可以傳遞引數

使用shell做http web介面,可以傳遞引數

映象:

docker pull ipyker/fcgiwrap-nginx-shell

啟動:

docker run -d --name nginx-fcgiwrap -p 80:80 ipyker/fcgiwrap-nginx-shell

使用:

curl http://127.0.0.1/v1/api/demo   

curl "http://127.0.0.1/v1/api/demo?abc&efg"

Dockerfile

FROM nginx:1.17.9

RUN apt-get update && apt-get install -y spawn-fcgi fcgiwrap \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/*

EXPOSE 80

STOPSIGNAL SIGTERM

COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]

docker-entrypoint.sh

#!/bin/bash
# ----------------------------------------------------------------
# Filename:       docker-entrypoint.sh
# Revision:       1.1
# Date:           2021-08-26
# Author:         pyker.zhang
# Email:          [email protected]
# website:        www.ipyker.com
# Description:    使用shell寫http web介面
# ----------------------------------------------------------------

# nginx支援fcgiwrap配置
cat > /etc/nginx/conf.d/default.conf <<EOF
server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ ^/v1/api/(.*)$ {
        gzip off;
        default_type  text/plain;
        root   /data/shell;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
        include fastcgi_params;
    }
}
EOF

# 建立shell指令碼目錄
mkdir -p /data/shell/v1/api

# 建立一個demo指令碼
cat > /data/shell/v1/api/demo <<EOF
#!/bin/sh
echo "Content-Type:text/html;charset=utf-8"
echo ""
# 自動重新整理
#echo '<script>window.setInterval(function(){
#    window.location.reload();
#},1000);</script>'
#echo '<meta http-equiv="refresh" content="60">'
# html頁面css樣式
#echo '<style>
#body{color:#cecece;}
#.title{color: #FF9800;border-left: 4px solid;padding: 4px;}
#pre{font-size:14px;border-left: 4px solid #4CAF50;padding: 5px;}
#</style>'
for i in a b c; do
	echo \$i
done
# Passing parameters
echo "\$QUERY_STRING" | awk -F '&' '{print \$1}'
echo "\$QUERY_STRING" | awk -F '&' '{print \$2}'
EOF

chmod +x /data/shell/v1/api/demo
/etc/init.d/fcgiwrap start
chmod 766 /var/run/fcgiwrap.socket
nginx -g "daemon off;"

進一步使用

本機建立掛載目錄,然後啟動容器的時候指定該路徑進行掛載

mkdir -p /data/shell
docker run -d --name nginx-fcgiwrap -p 80:80 -v /data/shell:/data/shell/ ipyker/fcgiwrap-nginx-shell

此時在本機的/data/shell/v1/api路徑下寫shell指令碼,然後訪問請求,就能執行該shell指令碼了

注意:在該目錄下存放的shell指令碼需要有x可執行許可權,否則會報403錯誤。

優化,使用現成的linux-shell模板