Posted to tcl by sebres at Thu May 28 12:14:27 GMT 2020view pretty
# uri's of rest-api: namespace eval rest {proc uri2cmd {req} { set p [dict get $req uri]; set m [dict get $req method] # set p [::cgi::path validate $p] list [string map {/ ::} $p]::=$m {*}[dict get $req args] }} # uri's of simplest html-page's: namespace eval page {proc uri2cmd {req} { set p [dict get $req uri]; set m [dict get $req method] # set p [::cgi::path validate $p]; regsub {/([^/]*)$} $p {/=\1} p list ::page[string map {/ ::} $p] $m {*}[dict get $req args] }} # examples: set req {api rest uri "/some/thing" method put args {name X surname Y}} puts [namespace inscope [dict get $req api] uri2cmd $req] # {*}[namespace inscope [dict get $req api] uri2cmd $req] set req {api page uri "/some/page" method get args {id 123}} puts [namespace inscope [dict get $req api] uri2cmd $req] # {*}[namespace inscope [dict get $req api] uri2cmd $req] set req {api page uri "/" method get args {}} puts [namespace inscope [dict get $req api] uri2cmd $req] # {*}[namespace inscope [dict get $req api] uri2cmd $req]