Posted to tcl by sebres at Thu May 28 12:14:27 GMT 2020view raw

  1. # uri's of rest-api:
  2.  
  3. namespace eval rest {proc uri2cmd {req} {
  4. set p [dict get $req uri]; set m [dict get $req method]
  5. # set p [::cgi::path validate $p]
  6. list [string map {/ ::} $p]::=$m {*}[dict get $req args]
  7. }}
  8.  
  9. # uri's of simplest html-page's:
  10.  
  11. namespace eval page {proc uri2cmd {req} {
  12. set p [dict get $req uri]; set m [dict get $req method]
  13. # set p [::cgi::path validate $p];
  14. regsub {/([^/]*)$} $p {/=\1} p
  15. list ::page[string map {/ ::} $p] $m {*}[dict get $req args]
  16. }}
  17.  
  18. # examples:
  19.  
  20. set req {api rest uri "/some/thing" method put args {name X surname Y}}
  21. puts [namespace inscope [dict get $req api] uri2cmd $req]
  22. # {*}[namespace inscope [dict get $req api] uri2cmd $req]
  23.  
  24. set req {api page uri "/some/page" method get args {id 123}}
  25. puts [namespace inscope [dict get $req api] uri2cmd $req]
  26. # {*}[namespace inscope [dict get $req api] uri2cmd $req]
  27.  
  28. set req {api page uri "/" method get args {}}
  29. puts [namespace inscope [dict get $req api] uri2cmd $req]
  30. # {*}[namespace inscope [dict get $req api] uri2cmd $req]
  31.