Skip to content

Commit 131cf73

Browse files
committed
Refactor JSON subdomain redirector logic
Reworks the JsonSubdomainRedirector to explicitly redirect API calls to the API domain and GUI calls to the GUI domain. Introduces clearer separation of redirect logic and improves method naming for better readability and maintainability.
1 parent 253f4a2 commit 131cf73

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

app/middleware/json_subdomain_redirector.rb

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
require 'uri'
22

3+
##
4+
# Make sure that the API calls are channeled through the API domain and the
5+
# GUI calls are channeled through the GUI domain
36
class JsonSubdomainRedirector
47
def initialize(app)
8+
@gui_domain = 'registry.seqco.de'
59
@api_domain = 'api.seqco.de'
610
@api_version = 'v1'
711
@app = app
@@ -10,11 +14,14 @@ def initialize(app)
1014
def call(env)
1115
request = Rack::Request.new(env)
1216

13-
return @app.call(env) if
14-
!is_json?(request) ||
15-
is_core_json?(request) ||
16-
already_on_api_subdomain?(request)
17+
if on_api_subdomain?(request)
18+
redirect_to_gui(request) unless is_api_call?(request)
19+
else
20+
redirect_to_api(request) if is_api_call?(request)
21+
end
22+
end
1723

24+
def redirect_to_api(request)
1825
is_versioned = request.path.match?(/^\/v\d+\//)
1926
new_path = is_versioned ? request.path : "/#{@api_version}#{request.path}"
2027

@@ -30,6 +37,21 @@ def call(env)
3037
]
3138
end
3239

40+
def redirect_to_gui(request)
41+
new_path = request.path.gsub(/^\/v\d+\//, '/')
42+
43+
# Reconstruct URL
44+
uri = URI.parse(request.url)
45+
uri.host = @gui_domain
46+
uri.path = new_path
47+
48+
return [
49+
301,
50+
{ 'Location' => uri.to_s, 'Content-Type' => 'text/html' },
51+
['Moved Permanently']
52+
]
53+
end
54+
3355
def is_json?(request)
3456
request.get_header('HTTP_ACCEPT')&.include?('application/json') ||
3557
request.path.end_with?('.json')
@@ -42,7 +64,11 @@ def is_core_json?(request)
4264
path.match?(/check\/\d+\.json$/)
4365
end
4466

45-
def already_on_api_subdomain?(request)
67+
def is_api_call?(request)
68+
is_json?(request) && !is_core_json?(request)
69+
end
70+
71+
def on_api_subdomain?(request)
4672
request.host == @api_domain
4773
end
4874
end

0 commit comments

Comments
 (0)