PromucFlow_constructor/app/client/start-https.sh

339 lines
10 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2020-05-05 12:16:51 +00:00
set -o errexit
set -o nounset
set -o pipefail
if [[ -n ${TRACE-} ]]; then
set -o xtrace
fi
cd "$(dirname "$0")"
if [[ ${1-} =~ ^-*h(elp)?$ ]]; then
echo 'Run '"$0"' [option...]
--with-docker: Start NGINX with Docker. Fail if Docker is not available.
--without-docker: Start NGINX directly. Fail if NGINX is not installed.
If neither of the above are set, we check if NGINX is installed, and use that if yes, or Docker otherwise.
--https: Require start with https.
--http: Require start with http.
If neither of the above ar set, then we check if mkcert is available, and use https if yes, or http otherwise.
--env-file: Specify an alternate env file. Defaults to '.env' at the root of the project.
A single positional argument can be given to set the backend server proxy address. Example:
2020-05-05 12:16:51 +00:00
'"$0"' https://localhost:8080
'"$0"' https://host.docker.internal:8080
'"$0"' https://release.app.appsmith.com:8080
' >&2
exit
2020-05-05 12:16:51 +00:00
fi
while [[ $# -gt 0 ]]; do
case $1 in
--with-docker)
run_as=docker
shift
;;
--without-docker)
run_as=nginx
shift
;;
--https)
use_https=1
shift
;;
--http)
use_https=0
shift
;;
--env-file)
env_file=$2
shift
shift
;;
-*|--*)
echo "Unknown option $1" >&2
exit 1
;;
*)
backend="$1"
shift
;;
esac
done
if [[ -z ${run_as-} ]]; then
if type nginx; then
run_as=nginx
elif type docker; then
run_as=docker
else
echo 'Could not find nginx or docker. Do a "brew install nginx" and try again.'
exit
fi
Use injected configuration from Nginx at runtime instead of build time (#30) * Use envsubst and nginx templates to generate nginx configs which can substitute environment variables and inject into the index.html file * Fix path in dockerfile. Add .gitignore and .env.example files. Fix nginx-linux template. * Add all environment variables. Add prefix to all environment variables. Update scripts to attempt to substitute all environment variables with the prefix * Setup dockerfile to execute a bash script. use env.example for fetching environment variables in development * Toggle features based on injected configs. Fix nginx template substitution script. * Update env.example file * Remove debug code from start-nginx.sh * Fix nginx config templates by adding quotes by default. Fix sed regex to include numerals. Toggle social login buttons on Login page based on the config. * Update rapid api environment variable name. Toggle oauth buttons based on config in SignUp page. Update .env.example to be a union of server and client environment variables * Adding a Map disabled message on Map widget * Adding links to Privacy policy and TNC * Use REACT_APP_ env variables with higher priority over injected config variables for toggling features * Update netlify.toml by commenting out the build environment variables * Remove env variables not required by the client * Remove start-storybook entry from package.json * Fix netlify.toml. Fallback algolia configs * Add contexts to netlify.toml for successful deploys. Swith to using APPSMITH_MARKETPLACE_URL as the toggle for RapidAPI feature on the client. Remove comments in nginx config templates. Fix template used in dockerfile. Co-authored-by: Satbir Singh <apple@apples-MacBook-Pro.local> Co-authored-by: Satbir Singh <satbir121@gmail.com>
2020-07-07 10:22:17 +00:00
fi
if [[ $run_as == docker ]]; then
echo 'Running with Docker. You may "brew install nginx" to run this without Docker.'
fi
Use injected configuration from Nginx at runtime instead of build time (#30) * Use envsubst and nginx templates to generate nginx configs which can substitute environment variables and inject into the index.html file * Fix path in dockerfile. Add .gitignore and .env.example files. Fix nginx-linux template. * Add all environment variables. Add prefix to all environment variables. Update scripts to attempt to substitute all environment variables with the prefix * Setup dockerfile to execute a bash script. use env.example for fetching environment variables in development * Toggle features based on injected configs. Fix nginx template substitution script. * Update env.example file * Remove debug code from start-nginx.sh * Fix nginx config templates by adding quotes by default. Fix sed regex to include numerals. Toggle social login buttons on Login page based on the config. * Update rapid api environment variable name. Toggle oauth buttons based on config in SignUp page. Update .env.example to be a union of server and client environment variables * Adding a Map disabled message on Map widget * Adding links to Privacy policy and TNC * Use REACT_APP_ env variables with higher priority over injected config variables for toggling features * Update netlify.toml by commenting out the build environment variables * Remove env variables not required by the client * Remove start-storybook entry from package.json * Fix netlify.toml. Fallback algolia configs * Add contexts to netlify.toml for successful deploys. Swith to using APPSMITH_MARKETPLACE_URL as the toggle for RapidAPI feature on the client. Remove comments in nginx config templates. Fix template used in dockerfile. Co-authored-by: Satbir Singh <apple@apples-MacBook-Pro.local> Co-authored-by: Satbir Singh <satbir121@gmail.com>
2020-07-07 10:22:17 +00:00
working_dir="$PWD/nginx"
mkdir -pv "$working_dir"
2020-05-05 12:16:51 +00:00
domain=dev.appsmith.com
key_file="$working_dir/$domain-key.pem"
cert_file="$working_dir/$domain.pem"
if [[ -z ${use_https-} ]]; then
if type mkcert; then
use_https=1
else
echo 'SSL cert isn'\''t there, and "mkcert" is not installed either. Starting with http.' >&2
echo 'Please "brew install mkcert" and re-run this script to start with https.' >&2
use_https=0
fi
fi
if [[ $use_https == 1 ]]; then
if ! [[ -f $key_file && -f $cert_file ]]; then
if type mkcert; then
pushd "$working_dir"
mkcert -install
mkcert "$domain"
popd
else
echo 'I got "--use-https", but "mkcert" is not available. Please "brew install mkcert" and try again.' >&2
exit 1
fi
fi
fi
upstream_host=localhost
if [[ $run_as == docker ]]; then
upstream_host=host.docker.internal
2020-05-05 12:16:51 +00:00
fi
frontend_host=${frontend_host-$upstream_host}
backend_host=${backend_host-$upstream_host}
rts_host=${rts_host-$upstream_host}
frontend_port=${frontend_port-3000}
backend_port=${backend_port-8080}
rts_port=${rts_port-8091}
backend="${backend-http://$backend_host:$backend_port}"
frontend="http://$frontend_host:$frontend_port"
rts="http://$rts_host:$rts_port"
if [[ -n ${env_file-} && ! -f $env_file ]]; then
echo "I got --env-file as '$env_file', but I cannot access it." >&2
exit 1
elif [[ -n ${env_file-} || -f ../../.env ]]; then
set -o allexport
# shellcheck disable=SC1090
source "${env_file-../../.env}"
set +o allexport
else
Use injected configuration from Nginx at runtime instead of build time (#30) * Use envsubst and nginx templates to generate nginx configs which can substitute environment variables and inject into the index.html file * Fix path in dockerfile. Add .gitignore and .env.example files. Fix nginx-linux template. * Add all environment variables. Add prefix to all environment variables. Update scripts to attempt to substitute all environment variables with the prefix * Setup dockerfile to execute a bash script. use env.example for fetching environment variables in development * Toggle features based on injected configs. Fix nginx template substitution script. * Update env.example file * Remove debug code from start-nginx.sh * Fix nginx config templates by adding quotes by default. Fix sed regex to include numerals. Toggle social login buttons on Login page based on the config. * Update rapid api environment variable name. Toggle oauth buttons based on config in SignUp page. Update .env.example to be a union of server and client environment variables * Adding a Map disabled message on Map widget * Adding links to Privacy policy and TNC * Use REACT_APP_ env variables with higher priority over injected config variables for toggling features * Update netlify.toml by commenting out the build environment variables * Remove env variables not required by the client * Remove start-storybook entry from package.json * Fix netlify.toml. Fallback algolia configs * Add contexts to netlify.toml for successful deploys. Swith to using APPSMITH_MARKETPLACE_URL as the toggle for RapidAPI feature on the client. Remove comments in nginx config templates. Fix template used in dockerfile. Co-authored-by: Satbir Singh <apple@apples-MacBook-Pro.local> Co-authored-by: Satbir Singh <satbir121@gmail.com>
2020-07-07 10:22:17 +00:00
echo "
Please populate the .env at the root of the project and run again
Or add the environment variables defined in .env.example to the environment
-- to enable features
" >&2
fi
if [[ -f /etc/nginx/mime.types || $run_as == docker ]]; then
mime_types=/etc/nginx/mime.types
elif type nginx >/dev/null; then
mime_types="$(dirname "$(nginx -t 2>&1 | head -1 | cut -d' ' -f5)")/mime.types"
if [[ ! -f $mime_types ]]; then
mime_types=
fi
fi
if [[ -z ${mime_types-} ]]; then
echo "No mime.types file found. Can't start NGINX." >&2
exit 1
Use injected configuration from Nginx at runtime instead of build time (#30) * Use envsubst and nginx templates to generate nginx configs which can substitute environment variables and inject into the index.html file * Fix path in dockerfile. Add .gitignore and .env.example files. Fix nginx-linux template. * Add all environment variables. Add prefix to all environment variables. Update scripts to attempt to substitute all environment variables with the prefix * Setup dockerfile to execute a bash script. use env.example for fetching environment variables in development * Toggle features based on injected configs. Fix nginx template substitution script. * Update env.example file * Remove debug code from start-nginx.sh * Fix nginx config templates by adding quotes by default. Fix sed regex to include numerals. Toggle social login buttons on Login page based on the config. * Update rapid api environment variable name. Toggle oauth buttons based on config in SignUp page. Update .env.example to be a union of server and client environment variables * Adding a Map disabled message on Map widget * Adding links to Privacy policy and TNC * Use REACT_APP_ env variables with higher priority over injected config variables for toggling features * Update netlify.toml by commenting out the build environment variables * Remove env variables not required by the client * Remove start-storybook entry from package.json * Fix netlify.toml. Fallback algolia configs * Add contexts to netlify.toml for successful deploys. Swith to using APPSMITH_MARKETPLACE_URL as the toggle for RapidAPI feature on the client. Remove comments in nginx config templates. Fix template used in dockerfile. Co-authored-by: Satbir Singh <apple@apples-MacBook-Pro.local> Co-authored-by: Satbir Singh <satbir121@gmail.com>
2020-07-07 10:22:17 +00:00
fi
nginx_pid="$working_dir/wildcard-nginx.pid"
nginx_access_log="$working_dir/access.log"
nginx_error_log="$working_dir/error.log"
rm -f "$nginx_access_log" "$nginx_error_log"
nginx_dev_conf="$working_dir/nginx.dev.conf"
# Rare case, if this file doesn't exist, and the `docker run` command
# (from further below) the script runs, then it'll auto-create a _directory_
# at this path, breaking this script after that.
rm -rf "$nginx_dev_conf"
worker_connections=1024
echo "
worker_processes 1;
error_log $nginx_error_log info;
$(if [[ $run_as == nginx ]]; then echo "pid $nginx_pid;"; fi)
events {
worker_connections $worker_connections;
}
http {
map \$http_x_forwarded_proto \$origin_scheme {
default \$http_x_forwarded_proto;
'' \$scheme;
}
include $mime_types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
access_log $nginx_access_log;
gzip on;
gzip_types *;
$(if [[ $use_https == 1 ]]; then echo "
server {
listen 80 default_server;
server_name $domain;
return 301 https://\$host\$request_uri;
}
"; fi)
server {
$(if [[ $use_https == 1 ]]; then echo "
listen 443 ssl http2 default_server;
server_name $domain;
ssl_certificate '$cert_file';
ssl_certificate_key '$key_file';
"; else echo "
listen 80 default_server;
server_name _;
"; fi)
client_max_body_size 100m;
gzip on;
proxy_ssl_server_name on;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header X-Forwarded-Proto \$origin_scheme;
proxy_set_header X-Forwarded-Host \$host;
proxy_set_header Accept-Encoding '';
sub_filter_once off;
location / {
proxy_pass $frontend;
sub_filter __APPSMITH_SENTRY_DSN__ '${APPSMITH_SENTRY_DSN-}';
sub_filter __APPSMITH_SMART_LOOK_ID__ '${APPSMITH_SMART_LOOK_ID-}';
sub_filter __APPSMITH_OAUTH2_GOOGLE_CLIENT_ID__ '${APPSMITH_OAUTH2_GOOGLE_CLIENT_ID-}';
sub_filter __APPSMITH_OAUTH2_GITHUB_CLIENT_ID__ '${APPSMITH_OAUTH2_GITHUB_CLIENT_ID-}';
sub_filter __APPSMITH_MARKETPLACE_ENABLED__ '${APPSMITH_MARKETPLACE_ENABLED-}';
sub_filter __APPSMITH_SEGMENT_KEY__ '${APPSMITH_SEGMENT_KEY-}';
sub_filter __APPSMITH_ALGOLIA_API_ID__ '${APPSMITH_ALGOLIA_API_ID-}';
sub_filter __APPSMITH_ALGOLIA_SEARCH_INDEX_NAME__ '${APPSMITH_ALGOLIA_SEARCH_INDEX_NAME-}';
sub_filter __APPSMITH_ALGOLIA_API_KEY__ '${APPSMITH_ALGOLIA_API_KEY-}';
sub_filter __APPSMITH_CLIENT_LOG_LEVEL__ '${APPSMITH_CLIENT_LOG_LEVEL-}';
sub_filter __APPSMITH_GOOGLE_MAPS_API_KEY__ '${APPSMITH_GOOGLE_MAPS_API_KEY-}';
sub_filter __APPSMITH_TNC_PP__ '${APPSMITH_TNC_PP-}';
sub_filter __APPSMITH_SENTRY_RELEASE__ '${APPSMITH_SENTRY_RELEASE-}';
sub_filter __APPSMITH_SENTRY_ENVIRONMENT__ '${APPSMITH_SENTRY_ENVIRONMENT-}';
sub_filter __APPSMITH_VERSION_ID__ '${APPSMITH_VERSION_ID-}';
sub_filter __APPSMITH_VERSION_RELEASE_DATE__ '${APPSMITH_VERSION_RELEASE_DATE-}';
sub_filter __APPSMITH_INTERCOM_APP_ID__ '${APPSMITH_INTERCOM_APP_ID-}';
sub_filter __APPSMITH_MAIL_ENABLED__ '${APPSMITH_MAIL_ENABLED-}';
sub_filter __APPSMITH_DISABLE_TELEMETRY__ '${APPSMITH_DISABLE_TELEMETRY-}';
sub_filter __APPSMITH_CLOUD_SERVICES_BASE_URL__ '${APPSMITH_CLOUD_SERVICES_BASE_URL-}';
sub_filter __APPSMITH_RECAPTCHA_SITE_KEY__ '${APPSMITH_RECAPTCHA_SITE_KEY-}';
sub_filter __APPSMITH_DISABLE_INTERCOM__ '${APPSMITH_DISABLE_INTERCOM-}';
sub_filter __APPSMITH_FORM_LOGIN_DISABLED__ '${APPSMITH_FORM_LOGIN_DISABLED-}';
sub_filter __APPSMITH_SIGNUP_DISABLED__ '${APPSMITH_SIGNUP_DISABLED-}';
sub_filter __APPSMITH_ZIPY_SDK_KEY__ '${APPSMITH_ZIPY_SDK_KEY-}';
sub_filter __APPSMITH_HIDE_WATERMARK__ '${APPSMITH_HIDE_WATERMARK-}';
sub_filter __APPSMITH_DISABLE_IFRAME_WIDGET_SANDBOX__ '${APPSMITH_DISABLE_IFRAME_WIDGET_SANDBOX-}';
}
location /api {
proxy_pass $backend;
}
location /oauth2 {
proxy_pass $backend;
}
location /login {
proxy_pass $backend;
}
location /rts {
proxy_pass $rts;
proxy_http_version 1.1;
proxy_set_header Host \$host;
proxy_set_header Connection upgrade;
proxy_set_header Upgrade \$http_upgrade;
}
}
}
" > "$nginx_dev_conf"
if type docker &>/dev/null; then
docker rm --force wildcard-nginx >/dev/null 2>&1 || true
fi
if [[ -f $nginx_pid ]]; then
# We never run `nginx` without the `-c` argument, since that can load the default system configuration, which is
# different for different systems. It introduces too many unknowns, with little value.
# So we build a temp config, just to have a predictable value for the `pid` directive.
temp_nginx_conf="$working_dir/temp.nginx.conf"
echo "pid $nginx_pid; events { worker_connections $worker_connections; }" > "$temp_nginx_conf"
if ! nginx -c "$temp_nginx_conf" -s quit; then
echo "Failed to stop nginx. This is _probably_ okay, and things should work fine." >&2
echo " If not, try running 'lsof -iTCP:80 -sTCP:LISTEN -nPt | xargs kill', and then re-run this script." >&2
fi
# The above stop command will delete the pid file, but we still do this to ensure it really is gone.
rm -f "$nginx_pid" "$temp_nginx_conf"
unset temp_nginx_conf
fi
if [[ $run_as == nginx ]]; then
nginx -c "$nginx_dev_conf"
stop_cmd="nginx -c '$nginx_dev_conf' -s quit"
elif [[ $run_as == docker ]]; then
docker run \
--name wildcard-nginx \
--detach \
--publish 80:80 \
--publish 443:443 \
--add-host=host.docker.internal:host-gateway \
--volume "$nginx_dev_conf:/etc/nginx/nginx.conf:ro" \
--volume "$working_dir:$working_dir" \
nginx:alpine \
>/dev/null
stop_cmd='docker rm --force wildcard-nginx'
else
echo "I don't know how to start NGINX with '$run_as'."
fi
echo '✅ Started NGINX'
echo " Stop with: $stop_cmd"
echo "🎉 $(if [[ $use_https == 1 ]]; then echo "https://$domain"; else echo "http://localhost"; fi)"