blob: 4104a69fcfbd7a8633ba556fdd50aeb60d418285 [file] [log] [blame]
#!/usr/bin/env bash
### Usage:
### publish <app_name> <container_ip>
### --help
###
### Publish HTTP traffic from the container port:/ out to the
### desired app_name by creating an nginx location
# source colors
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${dir}/colorizer-lib/colorizer.sh"
function publish(){
local help=$(grep "^### " "$0" | cut -c 5-)
if [[ ( -z "$1" ) || ( ${1} == "help" ) ]]; then
echo $help
else
local app_name=$1
local container_ip=$2
local nginx_config=/etc/nginx
if [[ -z "${app_name}" ]]; then
colorize "<red>You must give a container ip.</red>"
exit 1
fi
if [[ -z "${container_ip}" ]]; then
colorize "<red>You must give a app_name.</red>"
exit 1
fi
local publish_config="${nginx_config}/apps-enabled/${app_name}.conf"
# create the directory if it does not exist
test ! -d "${nginx_config}/apps-enabled" && mkdir -p "${nginx_config}/apps-enabled"
# special redirect bc ghost routes
if [[ $app_name == "blog" ]]; then
local redirect="/blog"
else
local redirect=""
fi
# create the file
cat << EOF > "${publish_config}"
location /$app_name/ {
# Path rewriting to hide mount prefix
rewrite /$app_name/(.*) $redirect/\$1 break;
proxy_pass http://${container_ip};
# web sockets
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "Upgrade";
# headers
proxy_set_header Host \$http_host;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_set_header X-Forwarded-For \$remote_addr;
proxy_set_header X-Forwarded-Port \$server_port;
proxy_set_header X-Request-Start \$msec;
}
EOF
# restart nginx
nginx -s reload
colorize "<white>published http://${container_ip} to http://localhost/${app_name}/</white>"
fi
}
publish $@