From 7afa027b9576f012bb27d65c7fa1028bc3ffa250 Mon Sep 17 00:00:00 2001 From: syeopite <70992037+syeopite@users.noreply.github.com> Date: Wed, 11 Aug 2021 03:36:25 -0700 Subject: [PATCH] Switch routing logic to use modules (#2298) * Switch routing logic to use modules * Add more macros for adding routes of different HTTP methods --- src/invidious/routes/base_route.cr | 2 -- src/invidious/routes/channels.cr | 18 +++++++++--------- src/invidious/routes/embed.cr | 6 +++--- src/invidious/routes/login.cr | 8 ++++---- src/invidious/routes/misc.cr | 10 +++++----- src/invidious/routes/playlists.cr | 26 +++++++++++++------------- src/invidious/routes/preferences.cr | 8 ++++---- src/invidious/routes/search.cr | 8 ++++---- src/invidious/routes/watch.cr | 6 +++--- src/invidious/routing.cr | 18 +++++++----------- 10 files changed, 52 insertions(+), 58 deletions(-) delete mode 100644 src/invidious/routes/base_route.cr diff --git a/src/invidious/routes/base_route.cr b/src/invidious/routes/base_route.cr deleted file mode 100644 index 07c6f15b..00000000 --- a/src/invidious/routes/base_route.cr +++ /dev/null @@ -1,2 +0,0 @@ -abstract class Invidious::Routes::BaseRoute -end diff --git a/src/invidious/routes/channels.cr b/src/invidious/routes/channels.cr index 9876936f..efa9056d 100644 --- a/src/invidious/routes/channels.cr +++ b/src/invidious/routes/channels.cr @@ -1,9 +1,9 @@ -class Invidious::Routes::Channels < Invidious::Routes::BaseRoute - def home(env) +module Invidious::Routes::Channels + def self.home(env) self.videos(env) end - def videos(env) + def self.videos(env) data = self.fetch_basic_information(env) if !data.is_a?(Tuple) return data @@ -40,7 +40,7 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute templated "channel" end - def playlists(env) + def self.playlists(env) data = self.fetch_basic_information(env) if !data.is_a?(Tuple) return data @@ -62,7 +62,7 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute templated "playlists" end - def community(env) + def self.community(env) data = self.fetch_basic_information(env) if !data.is_a?(Tuple) return data @@ -91,7 +91,7 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute templated "community" end - def about(env) + def self.about(env) data = self.fetch_basic_information(env) if !data.is_a?(Tuple) return data @@ -102,7 +102,7 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute end # Redirects brand url channels to a normal /channel/:ucid route - def brand_redirect(env) + def self.brand_redirect(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? # /attribution_link endpoint needs both the `a` and `u` parameter @@ -131,7 +131,7 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute end # Handles redirects for the /profile endpoint - def profile(env) + def self.profile(env) # The /profile endpoint is special. If passed into the resolve_url # endpoint YouTube would return a sign in page instead of an /channel/:ucid # thus we'll add an edge case and handle it here. @@ -146,7 +146,7 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute end end - private def fetch_basic_information(env) + private def self.fetch_basic_information(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" diff --git a/src/invidious/routes/embed.cr b/src/invidious/routes/embed.cr index 5e1e9431..5fc8a61f 100644 --- a/src/invidious/routes/embed.cr +++ b/src/invidious/routes/embed.cr @@ -1,5 +1,5 @@ -class Invidious::Routes::Embed < Invidious::Routes::BaseRoute - def redirect(env) +module Invidious::Routes::Embed + def self.redirect(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? if plid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "") @@ -23,7 +23,7 @@ class Invidious::Routes::Embed < Invidious::Routes::BaseRoute env.redirect url end - def show(env) + def self.show(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? id = env.params.url["id"] diff --git a/src/invidious/routes/login.cr b/src/invidious/routes/login.cr index f9e6ea6c..21d3fafd 100644 --- a/src/invidious/routes/login.cr +++ b/src/invidious/routes/login.cr @@ -1,5 +1,5 @@ -class Invidious::Routes::Login < Invidious::Routes::BaseRoute - def login_page(env) +module Invidious::Routes::Login + def self.login_page(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" @@ -28,7 +28,7 @@ class Invidious::Routes::Login < Invidious::Routes::BaseRoute templated "login" end - def login(env) + def self.login(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? referer = get_referer(env, "/feed/subscriptions") @@ -475,7 +475,7 @@ class Invidious::Routes::Login < Invidious::Routes::BaseRoute end end - def signout(env) + def self.signout(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" diff --git a/src/invidious/routes/misc.cr b/src/invidious/routes/misc.cr index 336f7e33..fa548f53 100644 --- a/src/invidious/routes/misc.cr +++ b/src/invidious/routes/misc.cr @@ -1,5 +1,5 @@ -class Invidious::Routes::Misc < Invidious::Routes::BaseRoute - def home(env) +module Invidious::Routes::Misc + def self.home(env) preferences = env.get("preferences").as(Preferences) locale = LOCALES[preferences.locale]? user = env.get? "user" @@ -26,17 +26,17 @@ class Invidious::Routes::Misc < Invidious::Routes::BaseRoute end end - def privacy(env) + def self.privacy(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? templated "privacy" end - def licenses(env) + def self.licenses(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? rendered "licenses" end - def cross_instance_redirect(env) + def self.cross_instance_redirect(env) referer = get_referer(env) if !env.get("preferences").as(Preferences).automatic_instance_redirect diff --git a/src/invidious/routes/playlists.cr b/src/invidious/routes/playlists.cr index 1f7fa27d..a2166bdd 100644 --- a/src/invidious/routes/playlists.cr +++ b/src/invidious/routes/playlists.cr @@ -1,5 +1,5 @@ -class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute - def index(env) +module Invidious::Routes::Playlists + def self.index(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" @@ -24,7 +24,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute templated "view_all_playlists" end - def new(env) + def self.new(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" @@ -40,7 +40,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute templated "create_playlist" end - def create(env) + def self.create(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" @@ -78,7 +78,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute env.redirect "/playlist?list=#{playlist.id}" end - def subscribe(env) + def self.subscribe(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" @@ -95,7 +95,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute env.redirect "/playlist?list=#{playlist.id}" end - def delete_page(env) + def self.delete_page(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" @@ -118,7 +118,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute templated "delete_playlist" end - def delete(env) + def self.delete(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" @@ -151,7 +151,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute env.redirect "/view_all_playlists" end - def edit(env) + def self.edit(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" @@ -191,7 +191,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute templated "edit_playlist" end - def update(env) + def self.update(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" @@ -235,7 +235,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute env.redirect "/playlist?list=#{plid}" end - def add_playlist_items_page(env) + def self.add_playlist_items_page(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" @@ -282,7 +282,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute templated "add_playlist_items" end - def playlist_ajax(env) + def self.playlist_ajax(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get? "user" @@ -409,7 +409,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute end end - def show(env) + def self.show(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? user = env.get?("user").try &.as(User) @@ -457,7 +457,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute templated "playlist" end - def mix(env) + def self.mix(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? rdid = env.params.query["list"]? diff --git a/src/invidious/routes/preferences.cr b/src/invidious/routes/preferences.cr index 21d79218..0f26ec15 100644 --- a/src/invidious/routes/preferences.cr +++ b/src/invidious/routes/preferences.cr @@ -1,5 +1,5 @@ -class Invidious::Routes::PreferencesRoute < Invidious::Routes::BaseRoute - def show(env) +module Invidious::Routes::PreferencesRoute + def self.show(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? referer = get_referer(env) @@ -9,7 +9,7 @@ class Invidious::Routes::PreferencesRoute < Invidious::Routes::BaseRoute templated "preferences" end - def update(env) + def self.update(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? referer = get_referer(env) @@ -219,7 +219,7 @@ class Invidious::Routes::PreferencesRoute < Invidious::Routes::BaseRoute env.redirect referer end - def toggle_theme(env) + def self.toggle_theme(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? referer = get_referer(env, unroll: false) diff --git a/src/invidious/routes/search.cr b/src/invidious/routes/search.cr index 513904b8..610d5031 100644 --- a/src/invidious/routes/search.cr +++ b/src/invidious/routes/search.cr @@ -1,5 +1,5 @@ -class Invidious::Routes::Search < Invidious::Routes::BaseRoute - def opensearch(env) +module Invidious::Routes::Search + def self.opensearch(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? env.response.content_type = "application/opensearchdescription+xml" @@ -15,7 +15,7 @@ class Invidious::Routes::Search < Invidious::Routes::BaseRoute end end - def results(env) + def self.results(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? query = env.params.query["search_query"]? @@ -34,7 +34,7 @@ class Invidious::Routes::Search < Invidious::Routes::BaseRoute end end - def search(env) + def self.search(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? region = env.params.query["region"]? diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index cd8ef910..0faba76e 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -1,5 +1,5 @@ -class Invidious::Routes::Watch < Invidious::Routes::BaseRoute - def handle(env) +module Invidious::Routes::Watch + def self.handle(env) locale = LOCALES[env.get("preferences").as(Preferences).locale]? region = env.params.query["region"]? @@ -190,7 +190,7 @@ class Invidious::Routes::Watch < Invidious::Routes::BaseRoute templated "watch" end - def redirect(env) + def self.redirect(env) url = "/watch?v=#{env.params.url["id"]}" if env.params.query.size > 0 url += "&#{env.params.query}" diff --git a/src/invidious/routing.cr b/src/invidious/routing.cr index 82d0028b..1fd3477d 100644 --- a/src/invidious/routing.cr +++ b/src/invidious/routing.cr @@ -1,15 +1,11 @@ module Invidious::Routing - macro get(path, controller, method = :handle) - get {{ path }} do |env| - controller_instance = {{ controller }}.new - controller_instance.{{ method.id }}(env) - end - end + {% for http_method in {"get", "post", "delete", "options", "patch", "put", "head"} %} - macro post(path, controller, method = :handle) - post {{ path }} do |env| - controller_instance = {{ controller }}.new - controller_instance.{{ method.id }}(env) + macro {{http_method.id}}(path, controller, method = :handle) + {{http_method.id}} \{{ path }} do |env| + \{{ controller }}.\{{ method.id }}(env) + end end - end + + {% end %} end