############################################################
# Dockerfile to build Traffic Ops Url Rewriter container images
# Based on NGINX 1.9
############################################################
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Example Build and Run:
# docker build --file Url_Rewriter_Dockerfile --rm --tag traffic_ops_url_rewriter:0.1 .
# docker run --add-host="localhost:10.0.2.2" -p 9008:9008 --name my-tour --hostname my-tour --detach traffic_ops_url_rewriter:0.1

FROM nginx:1.9
MAINTAINER dev@trafficcontrol.apache.org

RUN printf '\
server {\n\
	listen       9008;\n\
	server_name  localhost;\n\
\n\
	location /successful-return-prefix {\n\
		return 200 '\''{"result":'\'';\n\
		add_header Content-Type text/plain;\n\
	}\n\
\n\
	location /successful-return-postfix {\n\
		return 200 '\''}'\'';\n\
		add_header Content-Type text/plain;\n\
	}\n\
\n\
	location ~* ^/api/4.0/profile/(.*)$ {\n\
		add_before_body /successful-return-prefix;\n\
		add_after_body /successful-return-postfix;\n\
		addition_types *;\n\
		rewrite ^/api/4.0/profile/(.*)$ "/profile_parameter?profile=eq.$1&select=parameter{*}" break;\n\
		proxy_pass http://localhost:42421;\n\
		proxy_set_header Accept-Encoding "";\n\
		proxy_intercept_errors on;\n\
	}\n\
}\n\
' > /etc/nginx/conf.d/traffic_ops.conf

EXPOSE 9008
