Class: Above::MiddleWare::Redirect

Inherits:
Object
  • Object
show all
Defined in:
lib/above/middleware/redirect.rb

Overview

Middleware for redirects

Instance Method Summary collapse

Constructor Details

#initialize(app:) ⇒ Redirect

Returns a new instance of Redirect.



7
8
9
# File 'lib/above/middleware/redirect.rb', line 7

def initialize(app:)
  @app = app # who you gonna call
end

Instance Method Details

#call(env:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/above/middleware/redirect.rb', line 11

def call(env:)
  # env is {socket:, request:, config:, valid:}
  # redirects from local paths to local or remote ones
  # config format is eg:
  # "Above::MiddleWare::Redirect":
  #   redirect_permanent:
  #     "/bad.txt": "/good.txt"
  #     "/sad": "gemini://other.site/" etc

  # Can't redirect from an invalid url
  return @app&.call(env:) unless env[:valid]

  request = env[:request].path
  config = env[:config]["Above::MiddleWare::Redirect"]
  permanent_redirects = config["redirect_permanent"]
  temporary_redirects = config["redirect_temporary"]

  if permanent_redirects.key?(request)
    return [30, permanent_redirects[request], []]
  elsif temporary_redirects.key?(request)
    return [30, temporary_redirects[request], []]
  end

  # Otherise pass it on
  status, header, body_arr = @app.call(env:)
  [status, header, body_arr]
end