Skip to content

Commit 323fd10

Browse files
committed
feat: expose some request info to controllers
1 parent df88993 commit 323fd10

File tree

6 files changed

+94
-10
lines changed

6 files changed

+94
-10
lines changed

lib/kirei/controller.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,38 @@ def self.after(&block)
4040
@after_hooks ||= T.let(Set.new, Routing::NilableHooksType)
4141
@after_hooks.add(block) if block
4242
end
43+
44+
sig { returns(String) }
45+
def req_host
46+
env.fetch("HTTP_HOST")
47+
end
48+
49+
sig { returns(String) }
50+
def req_domain
51+
T.must(req_host.split(":").first).split(".").last(2).join(".")
52+
end
53+
54+
sig { returns(T.nilable(String)) }
55+
def req_subdomain
56+
parts = T.must(req_host.split(":").first).split(".")
57+
return if parts.size <= 2
58+
59+
T.must(parts[0..-3]).join(".")
60+
end
61+
62+
sig { returns(Integer) }
63+
def req_port
64+
env.fetch("SERVER_PORT")&.to_i
65+
end
66+
67+
sig { returns(T::Boolean) }
68+
def req_ssl?
69+
env.fetch("HTTPS", env.fetch("rack.url_scheme", "http")) == "https"
70+
end
71+
72+
sig { returns(T::Hash[String, T.untyped]) }
73+
private def env
74+
T.cast(@router.current_env, T::Hash[String, T.untyped])
75+
end
4376
end
4477
end

lib/kirei/routing/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def call(env)
3838
route = router.get(http_verb, req_path)
3939
return NOT_FOUND if route.nil?
4040

41+
router.current_env = env # expose the env to the controller
42+
4143
params = case route.verb
4244
when Verb::GET
4345
query = T.cast(env.fetch("QUERY_STRING"), String)

lib/kirei/routing/router.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class Router
2525
T::Hash[String, Route]
2626
end
2727

28+
sig { returns(T.nilable(T::Hash[String, T.untyped])) }
29+
attr_accessor :current_env
30+
2831
sig { void }
2932
def initialize
3033
@routes = T.let({}, RoutesHash)

spec/test_app/Gemfile.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ PATH
22
remote: ../..
33
specs:
44
kirei (0.6.3)
5+
logger (~> 1.5)
56
oj (~> 3.0)
67
pg (~> 1.0)
78
rack (~> 3.0)

spec/test_app/app/controllers/health.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@ class Health < Base
77
def livez
88
TestApp.config.logger.info("Health check")
99
TestApp.config.logger.info(params.inspect)
10-
render(TestApp.version, status: 200)
10+
11+
info = {
12+
"version" => TestApp.version,
13+
"req_host" => req_host,
14+
"req_domain" => req_domain,
15+
"req_subdomain" => req_subdomain,
16+
"req_port" => req_port,
17+
"req_ssl" => req_ssl?,
18+
}
19+
20+
render(Oj.dump(info), status: 200)
1121
end
1222
end
1323
end

spec/test_app/sorbet/rbi/gems/kirei@0.6.3.rbi

Lines changed: 44 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)