Skip to content

Commit

Permalink
Merge pull request #17 from callahad/custom-code
Browse files Browse the repository at this point in the history
Implement support for custom HTTP redirect codes
  • Loading branch information
josegonzalez authored Mar 24, 2017
2 parents ad03fee + 711ea4c commit 23c9170
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 22 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,29 @@ $ dokku plugin:install https://github.com/dokku/dokku-redirect.git

```
$ dokku help
redirect <app> Display the redirects set on app
redirect:set <app> <source> <destination> Set a redirect from <source> domain to <destination> domain
redirect:unset <app> <source> Unset a redirect from <source>
redirect <app> Display the redirects set on app
redirect:set <app> <src> <dest> [<code>] Set a redirect from <src> domain to <dest> domain
redirect:unset <app> <src> Unset a redirect from <source>
```

## Redirect Codes

| Code | Name | Behavior |
| ---- | ------------------ | -------------------------------------------------- |
| 301 | Moved Permanently | __(Default)__ Permanent, preserves method |
| 302 | Found | Temporary, may change method to GET |
| 303 | See Other | (HTTP/1.1) Temporary, changes method to GET |
| 307 | Temporary Redirect | (HTTP/1.1) Temporary, preserves method |


## Usage

Check redirects on my-app
```shell
$ dokku redirect my-app

SOURCE DESTINATION
ma.dokku.me my-app.dokku.me
SOURCE DESTINATION CODE
ma.dokku.me my-app.dokku.me 301
```

Set a new redirect on my-app
Expand Down
12 changes: 6 additions & 6 deletions commands
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ fi
case "$1" in
redirect:set)
[[ -z $3 || -z $4 ]] && echo "Please provide a source domain and a destination" && exit 1
SOURCE="$3"; DEST="$4"
SOURCE="$3"; DEST="$4"; CODE="${5:-301}"
touch "$REDIRECT_FILE"
grep -q "^$SOURCE:" "$REDIRECT_FILE" && dokku_log_fail "Source domain is already redirected"
dokku_log_info1 "Setting redirect for $APP..."
echo "$SOURCE:$DEST" >> "$REDIRECT_FILE"
echo "$SOURCE:$DEST:$CODE" >> "$REDIRECT_FILE"
is_deployed "$APP" && dokku nginx:build-config "$APP"
dokku_log_verbose "done"
;;
Expand All @@ -38,14 +38,14 @@ case "$1" in

redirect)
[[ ! -s $REDIRECT_FILE ]] && dokku_log_warn "There are no redirects for $APP" && exit 0
(echo "SOURCE:DESTINATION"; cat "$DOKKU_ROOT/$APP/REDIRECTS") | column -s: -t
(echo "SOURCE:DESTINATION:CODE"; awk -F ':' 'BEGIN { OFS=":" } { if ($3 == "") { $3 = "301" }; print }' "$DOKKU_ROOT/$APP/REDIRECTS") | column -s: -t
;;

help)
HELP=$(cat<<EOF
redirect <app>, Display the redirects set on app
redirect:set <app> <source> <destination>, Set a redirect from <source> domain to <destination> domain
redirect:unset <app> <source>, Unset a redirect from <source>
redirect:set <app> <src> <dest> [<code>], Set a redirect from <src> domain to <dest> domain
redirect:unset <app> <src>, Unset a redirect from <src>
EOF
)
if [[ -n $DOKKU_API_VERSION ]]; then
Expand All @@ -56,7 +56,7 @@ EOF
;;

*)
exit $DOKKU_NOT_IMPLEMENTED_EXIT
exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
;;

esac
12 changes: 7 additions & 5 deletions nginx-pre-reload
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ source "$PLUGIN_AVAILABLE_PATH/config/functions"
source "$PLUGIN_AVAILABLE_PATH/proxy/functions"
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions"

# shellcheck disable=SC2155
redirect_nginx_pre_load_trigger() {
# shellcheck disable=SC2034
declare desc="add nginx redirect servers"
Expand All @@ -33,15 +34,16 @@ redirect_nginx_pre_load_trigger() {
# shellcheck disable=SC2064
trap "rm -f \"$NGINX_CONF_PREPEND\"" EXIT

while read line; do
while read -r line; do
[[ -z "$line" ]] && continue
local DOMAIN=$(echo "$line" | cut -d: -f1)
local DEST_DOMAIN=${line//[^:]*:/}
dokku_log_info1 "Configuring redirect for $DOMAIN to $DEST..."
local DOMAIN=$(echo "$line" | awk -F ':' '{ print $1 }')
local DEST_DOMAIN=$(echo "$line" | awk -F ':' '{ print $2 }')
local REDIR_CODE=$(echo "$line" | awk -F ':' '{ print $3 ? $3 : "301" }')
dokku_log_info1 "Configuring redirect for $DOMAIN to $DEST via HTTP $REDIR_CODE..."
local SIGIL_PARAMS=(-f $NGINX_TEMPLATE APP="$APP" DOKKU_ROOT="$DOKKU_ROOT"
SPDY_SUPPORTED="$SPDY_SUPPORTED"
APP_SSL_PATH="$APP_SSL_PATH"
DOMAIN="$DOMAIN" DEST_DOMAIN="$DEST_DOMAIN"
DOMAIN="$DOMAIN" DEST_DOMAIN="$DEST_DOMAIN" REDIR_CODE="$REDIR_CODE"
PROXY_PORT_MAP="$PROXY_PORT_MAP")
sigil "${SIGIL_PARAMS[@]}" | cat -s >> "$NGINX_CONF_PREPEND"
done <<< "$(< "$REDIRECT_FILE")"
Expand Down
2 changes: 1 addition & 1 deletion plugin.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[plugin]
description = "Simple redirects for apps"
version = "0.4.0"
version = "0.5.0"
[plugin.config]
4 changes: 2 additions & 2 deletions templates/nginx.conf.sigil
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ server {
listen {{ $listen_port }};
server_name {{ $.DOMAIN }};
access_log off;
return 301 $scheme://{{ $.DEST_DOMAIN }}$request_uri;
return {{ $.REDIR_CODE }} $scheme://{{ $.DEST_DOMAIN }}$request_uri;
}
{{ else if eq $scheme "https"}}
server {
Expand All @@ -21,6 +21,6 @@ server {
ssl_certificate {{ $.APP_SSL_PATH }}/server.crt;
ssl_certificate_key {{ $.APP_SSL_PATH }}/server.key;

return 301 $scheme://{{ $.DEST_DOMAIN }}$request_uri;
return {{ $.REDIR_CODE }} $scheme://{{ $.DEST_DOMAIN }}$request_uri;
}
{{ end }}{{ end }}
2 changes: 1 addition & 1 deletion tests/redirect.bats
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ teardown() {
@test "(redirect) list redirects when present" {
dokku redirect:set my_app ma.dokku.me my-app.dokku.me
run dokku redirect my_app
assert_contains "${lines[*]}" "ma.dokku.me my-app.dokku.me"
assert_contains "${lines[*]}" "ma.dokku.me my-app.dokku.me 301"
}
11 changes: 9 additions & 2 deletions tests/redirect_set.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ teardown() {
assert_contains "${lines[*]}" "Please provide a source domain and a destination"
}

@test "(redirect:set) success" {
@test "(redirect:set) success with default http code" {
run dokku redirect:set my_app ma.dokku.me my-app.dokku.me
redirect=$(< "$DOKKU_ROOT/my_app/REDIRECTS")
assert_contains "${lines[*]}" "Setting redirect for my_app..."
assert_equal "$redirect" "ma.dokku.me:my-app.dokku.me"
assert_equal "$redirect" "ma.dokku.me:my-app.dokku.me:301"
}

@test "(redirect:set) success with custom http code" {
run dokku redirect:set my_app ma.dokku.me my-app.dokku.me 307
redirect=$(< "$DOKKU_ROOT/my_app/REDIRECTS")
assert_contains "${lines[*]}" "Setting redirect for my_app..."
assert_equal "$redirect" "ma.dokku.me:my-app.dokku.me:307"
}

0 comments on commit 23c9170

Please sign in to comment.