Action
according to a path
nitcorn :: Route :: defaultinit
nitcorn :: Route :: parse_params
Extract parameter values fromuri
.
nitcorn :: Route :: resolve_path
Replaceself.path
parameters with concrete values from the request
URI.
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
nitcorn :: Route :: defaultinit
core :: Object :: defaultinit
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: output_class_name
Display class name on stdout (debug only).nitcorn :: Route :: parse_params
Extract parameter values fromuri
.
nitcorn :: Route :: resolve_path
Replaceself.path
parameters with concrete values from the request
URI.
# A route to an `Action` according to a `path`
class Route
# Path to this action present in the URI
var path: nullable String
init
do
var path = path
if path != null then self.path = "/" / path
end
# `Action` to activate when this route is traveled
var handler: Action
end
lib/nitcorn/server_config.nit:59,1--72,3
# A route to an `Action` according to a `path`
redef class Route
redef init do
super
parse_pattern(path)
end
# Replace `self.path` parameters with concrete values from the `request` URI.
fun resolve_path(request: HttpRequest): nullable String do
if pattern_parts.is_empty then return self.path
var path = "/"
for part in pattern_parts do
if part isa UriString then
path /= part.string
else if part isa UriParam then
path /= request.param(part.name) or else part.name
end
end
return path
end
# Cut `path` into `UriParts`.
private fun parse_pattern(path: nullable String) do
if path == null then return
path = standardize_path(path)
var parts = path.split("/")
for part in parts do
if not part.is_empty and part.first == ':' then
# is an uri param
var name = part.substring(1, part.length)
var param = new UriParam(name)
pattern_parts.add param
else
# is a standard string
pattern_parts.add new UriString(part)
end
end
end
# `UriPart` forming `self` pattern.
private var pattern_parts = new Array[UriPart]
# Does `self` matches `uri`?
fun match(uri: nullable String): Bool do
if pattern_parts.is_empty then return true
if uri == null then return false
uri = standardize_path(uri)
var parts = uri.split("/")
for i in [0 .. pattern_parts.length[ do
if i >= parts.length then return false
var ppart = pattern_parts[i]
var part = parts[i]
if not ppart.match(part) then return false
end
return true
end
# Extract parameter values from `uri`.
fun parse_params(uri: nullable String): Map[String, String] do
var res = new HashMap[String, String]
if pattern_parts.is_empty then return res
if uri == null then return res
uri = standardize_path(uri)
var parts = uri.split("/")
for i in [0 .. pattern_parts.length[ do
if i >= parts.length then return res
var ppart = pattern_parts[i]
var part = parts[i]
if not ppart.match(part) then return res
if ppart isa UriParam then
res[ppart.name] = part
end
end
return res
end
# Remove first occurence of `/`.
private fun standardize_path(path: String): String do
if not path.is_empty and path.first == '/' then
return path.substring(1, path.length)
end
return path
end
end
lib/nitcorn/vararg_routes.nit:126,1--210,3