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
241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/tina4/router.rb', line 241 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
274 275 276 |
# File 'lib/tina4/router.rb', line 274 def any(path, middleware: [], swagger_meta: {}, template: nil, &block) add_route("ANY", path, block, middleware: middleware, swagger_meta: , template: template) end |
.clear! ⇒ Object
315 316 317 318 319 |
# File 'lib/tina4/router.rb', line 315 def clear! @routes = [] @method_index = Hash.new { |h, k| h[k] = [] } @ws_routes = [] end |
.delete(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
270 271 272 |
# File 'lib/tina4/router.rb', line 270 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
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/tina4/router.rb', line 278 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.
224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/tina4/router.rb', line 224 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
254 255 256 |
# File 'lib/tina4/router.rb', line 254 def get(path, middleware: [], swagger_meta: {}, template: nil, &block) add_route("GET", path, block, middleware: middleware, swagger_meta: , template: template) end |
.get_routes ⇒ Object
197 198 199 |
# File 'lib/tina4/router.rb', line 197 def get_routes routes end |
.group(prefix, auth_handler: nil, middleware: [], &block) ⇒ Object
321 322 323 |
# File 'lib/tina4/router.rb', line 321 def group(prefix, auth_handler: nil, middleware: [], &block) GroupContext.new(prefix, auth_handler, middleware).instance_eval(&block) end |
.list_routes ⇒ Object
201 202 203 |
# File 'lib/tina4/router.rb', line 201 def list_routes routes end |
.load_routes(directory) ⇒ Object
Load route files from a directory (file-based route discovery)
326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/tina4/router.rb', line 326 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().
295 296 297 |
# File 'lib/tina4/router.rb', line 295 def match(path, method) find_route(path, method) end |
.method_index ⇒ Object
Routes indexed by HTTP method for O(1) method lookup
237 238 239 |
# File 'lib/tina4/router.rb', line 237 def method_index @method_index ||= Hash.new { |h, k| h[k] = [] } end |
.patch(path, middleware: [], swagger_meta: {}, template: nil, &block) ⇒ Object
266 267 268 |
# File 'lib/tina4/router.rb', line 266 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
258 259 260 |
# File 'lib/tina4/router.rb', line 258 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
262 263 264 |
# File 'lib/tina4/router.rb', line 262 def put(path, middleware: [], swagger_meta: {}, template: nil, &block) add_route("PUT", path, block, middleware: middleware, swagger_meta: , template: template) end |
.routes ⇒ Object
193 194 195 |
# File 'lib/tina4/router.rb', line 193 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)
311 312 313 |
# File 'lib/tina4/router.rb', line 311 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
215 216 217 218 219 220 |
# File 'lib/tina4/router.rb', line 215 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
206 207 208 |
# File 'lib/tina4/router.rb', line 206 def ws_routes @ws_routes ||= [] end |