From 7a4bfe4180155e1388d9805e3e1e3b50fec284a1 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Fri, 9 Sep 2022 15:42:20 +1200 Subject: [PATCH] Redirect /wikiname to its homepage --- breezewiki.rkt | 2 ++ src/application-globals.rkt | 26 +++++++++++++++++++++++--- src/dispatcher-tree.rkt | 1 + src/page-home.rkt | 1 + src/page-redirect-wiki-home.rkt | 15 +++++++++++++++ src/page-subdomain.rkt | 18 ++---------------- src/xexpr-utils.rkt | 5 +++-- 7 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 src/page-redirect-wiki-home.rkt diff --git a/breezewiki.rkt b/breezewiki.rkt index dbca428..aeaaa89 100644 --- a/breezewiki.rkt +++ b/breezewiki.rkt @@ -13,6 +13,7 @@ (require-reloadable "src/page-home.rkt" page-home) (require-reloadable "src/page-not-found.rkt" page-not-found) (require-reloadable "src/page-proxy.rkt" page-proxy) +(require-reloadable "src/page-redirect-wiki-home.rkt" redirect-wiki-home) (require-reloadable "src/page-search.rkt" page-search) (require-reloadable "src/page-static.rkt" static-dispatcher) (require-reloadable "src/page-subdomain.rkt" subdomain-dispatcher) @@ -35,6 +36,7 @@ page-proxy page-search page-wiki + redirect-wiki-home static-dispatcher subdomain-dispatcher)))) (define server-t (thread start)) diff --git a/src/application-globals.rkt b/src/application-globals.rkt index 11d610a..b47ed30 100644 --- a/src/application-globals.rkt +++ b/src/application-globals.rkt @@ -1,6 +1,8 @@ #lang racket/base (require racket/string - net/http-easy + (prefix-in easy: net/http-easy) + html-writing + web-server/http "config.rkt" "xexpr-utils.rkt" "url-utils.rkt") @@ -11,13 +13,15 @@ ; generates a consistent footer application-footer ; generates a consistent template for wiki page content to sit in - generate-wiki-page) + generate-wiki-page + ; generates a minimal but complete redirect to another page + generate-redirect) (module+ test (require rackunit html-writing)) -(define timeouts (make-timeout-config #:lease 5 #:connect 5)) +(define timeouts (easy:make-timeout-config #:lease 5 #:connect 5)) (define (application-footer source-url) `(footer (@ (class "custom-footer")) @@ -102,3 +106,19 @@ (λ (t a c) (eq? t 'link)) page)))) "/proxy?dest=https%3A%2F%2Ftest.fandom.com"))) + +(define (generate-redirect dest) + (define dest-bytes (string->bytes/utf-8 dest)) + (response/output + #:code 302 + #:headers (list (header #"Location" dest-bytes)) + (λ (out) + (write-html + `(html + (head + (title "Redirecting...")) + (body + "Redirecting to " + (a (@ (href ,dest)) ,dest) + "...")) + out)))) diff --git a/src/dispatcher-tree.rkt b/src/dispatcher-tree.rkt index 49306d8..58e8eae 100644 --- a/src/dispatcher-tree.rkt +++ b/src/dispatcher-tree.rkt @@ -67,5 +67,6 @@ (filter:make #rx"^/[a-z-]+/wiki/Category:.+$" (lift:make (hash-ref ds 'page-category))) (filter:make #rx"^/[a-z-]+/wiki/.+$" (lift:make (hash-ref ds 'page-wiki))) (filter:make #rx"^/[a-z-]+/search$" (lift:make (hash-ref ds 'page-search))) + (filter:make #rx"^/[a-z-]+(/(wiki(/)?)?)?$" (lift:make (hash-ref ds 'redirect-wiki-home))) (hash-ref ds 'static-dispatcher) (lift:make (hash-ref ds 'page-not-found))))))) diff --git a/src/page-home.rkt b/src/page-home.rkt index 5374e0a..7e4184c 100644 --- a/src/page-home.rkt +++ b/src/page-home.rkt @@ -37,6 +37,7 @@ ,(apply format "~a: ~a" x)))) examples)) (h2 "Testimonials") + (p (@ (class "testimonial")) ">So glad to never have to touch fandom's garbage platform directly ever again —RNL") (p (@ (class "testimonial")) ">you are so right that fandom still sucks even with adblock somehow. even zapping all the stupid padding it still sucks —Minimus") (p (@ (class "testimonial")) ">attempting to go to a wiki's forum page with breezewiki doesn't work, which is based honestly —Tom Skeleton") (p (@ (class "testimonial")) ">Fandom pages crashing and closing, taking forever to load and locking up as they load the ads on the site... they are causing the site to crash because they are trying to load video ads both at the top and bottom of the site as well as two or three banner ads, then a massive top of site ad and eventually my anti-virus shuts the whole site down because it's literally pulling more resources than WoW in ultra settings... —Anonymous") diff --git a/src/page-redirect-wiki-home.rkt b/src/page-redirect-wiki-home.rkt new file mode 100644 index 0000000..a3740b5 --- /dev/null +++ b/src/page-redirect-wiki-home.rkt @@ -0,0 +1,15 @@ +#lang racket/base +(require net/url + web-server/http + "application-globals.rkt" + "url-utils.rkt" + "xexpr-utils.rkt") + +(provide + redirect-wiki-home) + +(define (redirect-wiki-home req) + (response-handler + (define wikiname (path/param-path (car (url-path (request-uri req))))) + (define dest (format "~a/wiki/Main_Page" wikiname)) + (generate-redirect dest))) diff --git a/src/page-subdomain.rkt b/src/page-subdomain.rkt index 4028a78..dededfb 100644 --- a/src/page-subdomain.rkt +++ b/src/page-subdomain.rkt @@ -3,9 +3,8 @@ racket/string net/url web-server/http - web-server/servlet-dispatch - html-writing (prefix-in lift: web-server/dispatchers/dispatch-lift) + "application-globals.rkt" "config.rkt" "xexpr-utils.rkt") @@ -20,17 +19,4 @@ (define path (url-path uri)) (define path-string (string-join (map (λ (p) (path/param-path p)) path) "/")) (define dest (format "~a/~a/~a" (config-get 'canonical_origin) subdomain path-string)) - (define dest-bytes (string->bytes/utf-8 dest)) - (response/output - #:code 302 - #:headers (list (header #"Location" dest-bytes)) - (λ (out) - (write-html - `(html - (head - (title "Redirecting...")) - (body - "Redirecting to " - (a (@ (href ,dest)) ,dest) - "...")) - out))))))) + (generate-redirect dest))))) diff --git a/src/xexpr-utils.rkt b/src/xexpr-utils.rkt index 5b87d34..cd7f651 100644 --- a/src/xexpr-utils.rkt +++ b/src/xexpr-utils.rkt @@ -197,6 +197,7 @@ (λ (out) (for ([port (list (current-error-port) out)]) (parameterize ([current-error-port port]) - (displayln "Exception raised in Racket code at response generation time:" (current-error-port)) - ((error-display-handler) (exn-message e) e))))))]) + (with-handlers ([exn:fail? (λ (e) (void))]) + (displayln "Exception raised in Racket code at response generation time:" (current-error-port)) + ((error-display-handler) (exn-message e) e)))))))]) body ...))