subdispatch
[catagits/Web-Simple.git] / examples / dispatchex / dispatchex.cgi
diff --git a/examples/dispatchex/dispatchex.cgi b/examples/dispatchex/dispatchex.cgi
new file mode 100644 (file)
index 0000000..9c357a5
--- /dev/null
@@ -0,0 +1,29 @@
+use Web::Simple 'DispatchEx';
+
+package DispatchEx;
+
+dispatch [
+  filter_response {
+    [ 200, [ 'Content-type' => 'text/plain' ], $_[1] ];
+  },
+  subdispatch sub (.html) {
+    [
+      filter_response { [ @{$_[1]}, '.html' ] },
+      sub (/foo) { [ '/foo' ] },
+    ]
+  },
+  subdispatch sub (/domain/*/...) {
+    return unless (my $domain_id = $_[1]) =~ /^\d+$/;
+    [
+      sub (/) {
+        [ "Domain ${domain_id}" ]
+      },
+      sub (/user/*) {
+        return unless (my $user_id = $_[1]) =~ /^\d+$/;
+        [ "Domain ${domain_id} user ${user_id}" ]
+      }
+    ]
+  }
+];
+
+DispatchEx->run_if_script;