Module: Tina4::Router
- Defined in:
- lib/tina4/router.rb
Defined Under Namespace
Classes: GroupContext
Class Method Summary collapse
- .add_route(method, path, handler, auth_handler: nil, swagger_meta: {}, middleware: [], template: nil) ⇒ Object
- .any(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
- .clear! ⇒ Object
- .delete(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
- .find_route(path, method) ⇒ Object
-
.find_ws_route(path) ⇒ Object
Find a matching WebSocket route for a given path.
-
.get(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
Convenience registration methods matching tina4-python pattern.
- .get_routes ⇒ Object
- .group(prefix, auth_handler: nil, middleware: [], &block) ⇒ Object
- .list_routes ⇒ Object
-
.load_routes(directory) ⇒ Object
Load route files from a directory (file-based route discovery).
-
.match(path, method) ⇒ Object
Alias for find_route().
-
.method_index ⇒ Object
Routes indexed by HTTP method for O(1) method lookup.
- .patch(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
- .post(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
- .put(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
- .routes ⇒ Object
-
.use(klass) ⇒ Object
Register a class-based middleware globally.
-
.websocket(path, &block) ⇒ Object
Register a WebSocket route.
-
.ws_routes ⇒ Object
Registered WebSocket routes.
Class Method Details
.add_route(method, path, handler, auth_handler: nil, swagger_meta: {}, middleware: [], template: nil) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/tina4/router.rb', line 243 def add_route(method, path, handler, auth_handler: nil, swagger_meta: {}, middleware: [], template: nil) route = Route.new(method, path, handler, auth_handler: auth_handler, swagger_meta: , middleware: middleware, template: template) routes << route method_index[route.method] << route Tina4::Log.debug("Route registered: #{method.upcase} #{path}") route end |
.any(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
276 277 278 |
# File 'lib/tina4/router.rb', line 276 def any(path, middleware: [], swagger_meta: {}, template: nil, &block) add_route("ANY", path, block, middleware: middleware, swagger_meta: , template: template) end |
.clear! ⇒ Object
317 318 319 320 321 |
# File 'lib/tina4/router.rb', line 317 def clear! @routes = [] @method_index = Hash.new { |h, k| h[k] = [] } @ws_routes = [] end |
.delete(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
272 273 274 |
# File 'lib/tina4/router.rb', line 272 def delete(path, middleware: [], swagger_meta: {}, template: nil, &block) add_route("DELETE", path, block, middleware: middleware, swagger_meta: , template: template) end |
.find_route(path, method) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/tina4/router.rb', line 280 def find_route(path, method) normalized_method = method.upcase # Normalize path once (not per-route) normalized_path = path.gsub("\\", "/") normalized_path = "/#{normalized_path}" unless normalized_path.start_with?("/") normalized_path = normalized_path.chomp("/") unless normalized_path == "/" # Check ANY routes first, then method-specific routes candidates = (method_index["ANY"] || []) + (method_index[normalized_method] || []) candidates.each do |route| params = route.match_path(normalized_path) return [route, params] if params end nil end |
.find_ws_route(path) ⇒ Object
Find a matching WebSocket route for a given path. Returns [ws_route, params] or nil.
226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/tina4/router.rb', line 226 def find_ws_route(path) normalized = path.gsub("\\", "/") normalized = "/#{normalized}" unless normalized.start_with?("/") normalized = normalized.chomp("/") unless normalized == "/" ws_routes.each do |ws_route| params = ws_route.match?(normalized) return [ws_route, params] if params end nil end |
.get(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
Convenience registration methods matching tina4-python pattern
256 257 258 |
# File 'lib/tina4/router.rb', line 256 def get(path, middleware: [], swagger_meta: {}, template: nil, &block) add_route("GET", path, block, middleware: middleware, swagger_meta: , template: template) end |
.get_routes ⇒ Object
199 200 201 |
# File 'lib/tina4/router.rb', line 199 def get_routes routes end |
.group(prefix, auth_handler: nil, middleware: [], &block) ⇒ Object
323 324 325 |
# File 'lib/tina4/router.rb', line 323 def group(prefix, auth_handler: nil, middleware: [], &block) GroupContext.new(prefix, auth_handler, middleware).instance_eval(&block) end |
.list_routes ⇒ Object
203 204 205 |
# File 'lib/tina4/router.rb', line 203 def list_routes routes end |
.load_routes(directory) ⇒ Object
Load route files from a directory (file-based route discovery)
328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/tina4/router.rb', line 328 def load_routes(directory) return unless Dir.exist?(directory) Dir.glob(File.join(directory, "**/*.rb")).sort.each do |file| begin load file Tina4::Log.debug("Route loaded: #{file}") rescue => e Tina4::Log.error("Failed to load route #{file}: #{e.}") end end end |
.match(path, method) ⇒ Object
Alias for find_route().
297 298 299 |
# File 'lib/tina4/router.rb', line 297 def match(path, method) find_route(path, method) end |
.method_index ⇒ Object
Routes indexed by HTTP method for O(1) method lookup
239 240 241 |
# File 'lib/tina4/router.rb', line 239 def method_index @method_index ||= Hash.new { |h, k| h[k] = [] } end |
.patch(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
268 269 270 |
# File 'lib/tina4/router.rb', line 268 def patch(path, middleware: [], swagger_meta: {}, template: nil, &block) add_route("PATCH", path, block, middleware: middleware, swagger_meta: , template: template) end |
.post(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
260 261 262 |
# File 'lib/tina4/router.rb', line 260 def post(path, middleware: [], swagger_meta: {}, template: nil, &block) add_route("POST", path, block, middleware: middleware, swagger_meta: , template: template) end |
.put(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
264 265 266 |
# File 'lib/tina4/router.rb', line 264 def put(path, middleware: [], swagger_meta: {}, template: nil, &block) add_route("PUT", path, block, middleware: middleware, swagger_meta: , template: template) end |
.routes ⇒ Object
195 196 197 |
# File 'lib/tina4/router.rb', line 195 def routes @routes ||= [] end |
.use(klass) ⇒ Object
Register a class-based middleware globally. The class should define static before_* and/or after_* methods. Example:
class AuthMiddleware
def self.before_auth(request, response)
unless request.headers["authorization"]
return [request, response.json({ error: "Unauthorized" }, 401)]
end
[request, response]
end
end
Tina4::Router.use(AuthMiddleware)
313 314 315 |
# File 'lib/tina4/router.rb', line 313 def use(klass) Tina4::Middleware.use(klass) end |
.websocket(path, &block) ⇒ Object
Register a WebSocket route. The handler block receives (connection, event, data) where:
connection — WebSocketConnection with #send, #broadcast, #close, #params
event — :open, :message, or :close
data — String payload for :message, nil for :open/:close
217 218 219 220 221 222 |
# File 'lib/tina4/router.rb', line 217 def websocket(path, &block) ws_route = WebSocketRoute.new(path, block) ws_routes << ws_route Tina4::Log.debug("WebSocket route registered: #{path}") ws_route end |
.ws_routes ⇒ Object
Registered WebSocket routes
208 209 210 |
# File 'lib/tina4/router.rb', line 208 def ws_routes @ws_routes ||= [] end |