forked from code-golf/code-golf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We must wrap the affected routes in a Group with the middleware applied to that group, moving the middleware higher doesn't work. See go-chi/chi#892 for details. This lays the groundwork for future hole/lang renames like Lisp → Common Lisp. Updates code-golf#1318
- Loading branch information
Showing
5 changed files
with
113 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// RedirHolesLangs redirects old values for {hole} and {lang}. | ||
func RedirHolesLangs(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
newPath := r.URL.Path | ||
|
||
// FIXME Consider using chi.RouteContext(r.Context()).RoutePattern() | ||
// and filling in hole/lang, more robust than strings.Replace(). | ||
|
||
switch r.PathValue("hole") { | ||
case "billiard": | ||
newPath = strings.Replace(newPath, "/billiard", "/billiards", 1) | ||
case "eight-queens": | ||
newPath = strings.Replace(newPath, "/eight-queens", "/n-queens", 1) | ||
case "factorial-factorisation-ascii": | ||
newPath = strings.Replace(newPath, "/factorial-factorisation-ascii", | ||
"/factorial-factorisation", 1) | ||
case "grid-packing": | ||
newPath = strings.Replace(newPath, "/grid-packing", "/css-grid", 1) | ||
} | ||
|
||
if r.PathValue("lang") == "perl6" { | ||
newPath = strings.Replace(newPath, "/perl6", "/raku", 1) | ||
} | ||
|
||
if newPath == r.URL.Path { | ||
next.ServeHTTP(w, r) | ||
} else { | ||
if r.URL.RawQuery != "" { | ||
newPath += "?" + r.URL.RawQuery | ||
} | ||
|
||
http.Redirect(w, r, newPath, http.StatusPermanentRedirect) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,27 @@ | ||
use t; | ||
use HTTP::Tiny; | ||
use Test; | ||
|
||
constant $path = '/foo?bar=baz'; | ||
my constant $query = '?foo=bar'; | ||
|
||
# TODO | ||
# is $ua->get( $_ . PATH )->{url}, 'https://code.golf' . PATH, $_ | ||
# for <http{,s}://{,www.}code{-golf.io,.golf}>; | ||
for < | ||
GET /api/holes/billiard /api/holes/billiards | ||
GET /api/langs/perl6 /api/langs/raku | ||
DELETE /api/notes/billiard/perl6 /api/notes/billiards/raku | ||
GET /api/notes/billiard/perl6 /api/notes/billiards/raku | ||
PUT /api/notes/billiard/perl6 /api/notes/billiards/raku | ||
GET /billiard /billiards | ||
GET /rankings/recent-holes/perl6/bytes /rankings/recent-holes/raku/bytes | ||
GET /recent/perl6 /recent/raku | ||
GET /recent/solutions/billiard/perl6/bytes /recent/solutions/billiards/raku/bytes | ||
GET /scores/billiard/perl6 /scores/billiards/raku | ||
> -> $method, $start, $end { | ||
state $ua = HTTP::Tiny.new :!max-redirect; | ||
|
||
my $location = $ua.request( | ||
$method, "https://app:443$start$query", | ||
)<headers><location>; | ||
|
||
is $location, $end ~ $query, "$method $start → $end"; | ||
} | ||
|
||
done-testing; |