Skip to content

Commit

Permalink
Switch routing logic to use modules (#2298)
Browse files Browse the repository at this point in the history
* Switch routing logic to use modules
* Add more macros for adding routes of different HTTP methods
  • Loading branch information
syeopite authored Aug 11, 2021
1 parent 637a5cc commit 7afa027
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 58 deletions.
2 changes: 0 additions & 2 deletions src/invidious/routes/base_route.cr

This file was deleted.

18 changes: 9 additions & 9 deletions src/invidious/routes/channels.cr
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/invidious/routes/embed.cr
Original file line number Diff line number Diff line change
@@ -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_-]/, "")
Expand All @@ -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"]

Expand Down
8 changes: 4 additions & 4 deletions src/invidious/routes/login.cr
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions src/invidious/routes/misc.cr
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
26 changes: 13 additions & 13 deletions src/invidious/routes/playlists.cr
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"]?
Expand Down
8 changes: 4 additions & 4 deletions src/invidious/routes/preferences.cr
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions src/invidious/routes/search.cr
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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"]?
Expand All @@ -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"]?

Expand Down
6 changes: 3 additions & 3 deletions src/invidious/routes/watch.cr
Original file line number Diff line number Diff line change
@@ -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"]?

Expand Down Expand Up @@ -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}"
Expand Down
18 changes: 7 additions & 11 deletions src/invidious/routing.cr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7afa027

Please sign in to comment.