Instantly share code, notes, and snippets.

Embed
What would you like to do?
A sample to test some more complex routing options of haxe.web.Dispatch. Run with `haxe -x DispatchTest.hx` This is the sample code for this tutorial / blog post: http://jasononeil.com.au/2013/05/29/creating-complex-url-routing-schemes-with-haxe-web-dispatch/
import haxe.ds.StringMap;
import haxe.web.Dispatch;
import haxe.web.Dispatch.DispatchError;
#if sys
import Sys.println;
import Sys.print;
#else
import haxe.Log.trace in println;
import haxe.Log.trace in print;
#end
import haxe.web.Dispatch;
class DispatchTest {
static var tests = [
// Basic tests - default, simple matches, slashes
"/" => "Welcome",
"about" => "About Us",
"about/" => "About Us",
"aboutus/" => "About Us",
// Test basic variable capture
"help/" => "Please choose a topic",
"help/haxe" => "Info about haxe",
"help/ufront/" => "Info about ufront",
// Test sub-dispatching
// Note: it's clever enough not to think "bytag" and "popular" are project names for doDefault()
"projects/" => "Projects Index",
"projects/ufront" => "ufront project page",
"projects/popular" => "Popular Projects",
"projects/bytag/" => "Search By Tag",
"projects/bytag/nme/" => 'Projects tagged "nme"',
// Test capturing before sub-dispatching
"users/" => "List of users",
"users/jason/" => "jason's projects",
"users/jason/contact/" => "Contact jason",
"users/jason/detox/" => "jason's detox project",
// Test getting the "rest" of the URL
"download" => "Please select a download",
"download/" => "Please select a download",
"download/file.zip" => "Download file.zip",
"download/archive/2.0/old.zip" => "Download archive/2.0/old.zip",
// Test top level usernames
"/jason/" => "jason's projects",
"jason/detox" => "jason\'s detox project",
"jason/contact" => "Contact jason",
// Sign Up
"signup/" => "Sign Up",
"signin/" => "Sign In",
"signout/" => "Sign Out",
];
static var methodTest = [
"/" => ""
];
static function main() {
// Trace the URL, expected and actual result for each test
for ( test in tests.keys() )
{
var expected = tests.get( test );
println(test);
println(' Expected: $expected');
print (' Actual: ');
try {
Dispatch.run( test, new StringMap(), new Routes() );
}
catch (e:DispatchError) {
println("ERROR: " + e);
}
}
}
}
import haxe.web.Dispatch;
#if sys
import Sys.println;
import Sys.print;
#else
import haxe.Log.trace in println;
import haxe.Log.trace in print;
#end
class Routes {
public function new() {}
function doDefault( d:Dispatch, username:String ) {
if ( username == "" ) {
println("Welcome");
}
else {
d.dispatch( new UserController(username) );
}
}
function doAbout() {
println("About Us");
}
function doHelp( topic:String ) {
if ( topic == "")
println("Please choose a topic");
else
println('Info about $topic');
}
function doProjects( d:Dispatch ) {
d.dispatch( new ProjectController() );
}
function doUsers( d:Dispatch, username:String ) {
if ( username == "" ) {
println("List of users");
}
else {
d.dispatch( new UserController(username) );
}
}
function doDownload( d:Dispatch ) {
if ( d.parts[d.parts.length-1] == "" ) d.parts.pop();
if ( d.parts.length == 0 ) {
println("Please select a download");
}
else {
println("Download " + d.parts.join("/"));
}
}
inline function doSignup() (new AuthController()).doSignUp();
inline function doSignin() (new AuthController()).doSignIn();
inline function doSignout() (new AuthController()).doSignOut();
inline function doAboutus() doAbout();
}
class ProjectController {
public function new() {}
function doDefault( name:String ) {
if ( name == "") {
println("Projects Index");
}
else {
println('$name project page');
}
}
function doPopular() {
println("Popular Projects");
}
// PLEASE NOTE: case sensitive, so doByTag will only match /byTag/, not /bytag/
function doBytag( tag:String ) {
if ( tag == "" ) {
println("Search By Tag");
}
else {
println('Projects tagged "$tag"');
}
}
}
class UserController {
var username:String;
public function new( username:String ) {
this.username = username;
}
function doDefault( project:String ) {
if ( project == "") {
println('$username\'s projects');
}
else {
println('$username\'s $project project');
}
}
function doContact() {
println('Contact $username');
}
}
class AuthController {
public function new() {}
public function doSignUp() {
println("Sign Up");
}
public function doSignIn() {
println("Sign In");
}
public function doSignOut() {
println("Sign Out");
}
}
@jasononeil

This comment has been minimized.

Owner

jasononeil commented Jun 9, 2013

I added two examples "aboutus/" - which is an alias to "about/", and also "download/*", which lets you look at the remaining path as an array of strings, basically a rest:Array parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment