Class: Kirei::Routing::Router

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Singleton
Defined in:
lib/kirei/routing/router.rb

Overview

Usage:

Router.add_routes([

Route.new(
  verb: Verb::GET,
  path: "/livez",
  controller: Controllers::HealthController,
  action: "livez",
),

])

Constant Summary collapse

RoutesHash =
T.type_alias do
  T::Hash[String, Route]
end
ResolveResult =
T.type_alias do
  T.nilable([Route, T::Hash[String, String]])
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



38
39
40
41
# File 'lib/kirei/routing/router.rb', line 38

def initialize
  @routes = T.let({}, RoutesHash)
  @dynamic_routes = T.let([], T::Array[Route])
end

Instance Attribute Details

#current_envObject

Returns the value of attribute current_env.



35
36
37
# File 'lib/kirei/routing/router.rb', line 35

def current_env
  @current_env
end

#dynamic_routesObject (readonly)

Returns the value of attribute dynamic_routes.



47
48
49
# File 'lib/kirei/routing/router.rb', line 47

def dynamic_routes
  @dynamic_routes
end

#routesObject (readonly)

Returns the value of attribute routes.



44
45
46
# File 'lib/kirei/routing/router.rb', line 44

def routes
  @routes
end

Class Method Details

.add_health_routes!Object



90
91
92
93
94
95
96
97
98
# File 'lib/kirei/routing/router.rb', line 90

def self.add_health_routes!
  add_routes(
    [
      Route.new(verb: Verb::GET, path: "/livez", controller: Kirei::Controllers::Health, action: "livez"),
      Route.new(verb: Verb::GET, path: "/readyz", controller: Kirei::Controllers::Health, action: "readyz"),
      Route.new(verb: Verb::GET, path: "/healthz", controller: Kirei::Controllers::Health, action: "healthz"),
    ],
  )
end

.add_routes(routes) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/kirei/routing/router.rb', line 77

def self.add_routes(routes)
  routes.each do |route|
    if route.dynamic?
      instance.dynamic_routes << route
    else
      key = "#{route.verb.serialize} #{route.path}"
      instance.routes[key] = route
    end
  end
end

Instance Method Details

#get(verb, path) ⇒ Object



56
57
58
59
# File 'lib/kirei/routing/router.rb', line 56

def get(verb, path)
  key = "#{verb.serialize} #{path}"
  routes[key]
end

#resolve(verb, path) ⇒ Object



69
70
71
72
73
74
# File 'lib/kirei/routing/router.rb', line 69

def resolve(verb, path)
  static_route = get(verb, path)
  return [static_route, {}] unless static_route.nil?

  match_dynamic(verb, path)
end