http { server { listen 80; access_log /proc/self/fd/1; error_log /proc/self/fd/2;
location /api/v1/tokens { default_type text/html; content_by_lua ' local cjson = require "cjson" local redis = require "resty.redis" local red = redis:new() local args = ngx.req.get_uri_args() local ok, err = red:connect(os.getenv("REDIS_PORT_6379_TCP_ADDR"), tonumber(os.getenv("REDIS_PORT_6379_TCP_PORT"))) if not ok then ngx.say("failed to connect: ", err) return end local res, err = red:get("users:" .. args.uuid) if not res then ngx.say("failed to get token: ", err) return end ngx.header.content_type = "application/json; charset=utf-8" ngx.say(cjson.encode({token = res})) red:close() '; } } }
Docker Composeからopenrestyサービスをupします。
$ docker-compose up openresty docker-compose up openresty Recreating openrestyapps_redis_1... Recreating openrestyapps_openresty_1... Attaching to openrestyapps_openresty_1
依存関係にあるredisサービスも起動しますがrediscliサービスは起動しません。
$ docker-compose ps Name Command State Ports ------------------------------------------------------------------------- openrestyapps_op nginx -c /etc/ng Up 0.0.0.0:80->80/t enresty_1 inx/nginx.conf cp openrestyapps_re /entrypoint.sh Up 6379/tcp dis_1 redis-server
redis-cliでレコード作成
rediscliサービスでテスト用のレコードをRedisに作成します。
$ docker-compose run --rm rediscli bash -c 'redis-cli -h $REDIS_PORT_6379_TCP_ADDR set users:123 8Nyo7D9a' OK Removing openrestyapps_rediscli_run_1...
getからレコードの作成を確認します。
$ docker-compose run --rm rediscli bash -c 'redis-cli -h $REDIS_PORT_6379_TCP_ADDR get users:123' "8Nyo7D9a" Removing openrestyapps_rediscli_run_1...