Skip to content

Instantly share code, notes, and snippets.

@mogsie
Last active December 23, 2015 13:39
Show Gist options
  • Save mogsie/6643280 to your computer and use it in GitHub Desktop.
Save mogsie/6643280 to your computer and use it in GitHub Desktop.
My 5-in-5 presentation for REST Fest 2013

Erik Mogensen

Chief Architect, Vizrt

An alternative to template based routing?

Traditional routing

GET /user/:id
GET /user/:id/statuses
GET /status/:id

user 123 → SELECT * FROM user WHERE id = '123'
user 123 → SELECT * FROM statuses WHERE userid = '123'
status 456 → SELECT * FROM statuses WHERE id = '456'

Change the URL space

The business side wants the statuses to be a top level API: /statuses/:id

GET /user/:id
GET /statuses/:id
GET /status/:id

user 123 → SELECT * FROM user WHERE id = '123'
user 123 → SELECT * FROM statuses WHERE userid = '123'
status 456 → SELECT * FROM statuses WHERE id = '456'

Alternative routing, think like a file system

/user/123 → /var/www/user/123
/user/123/statuses → /var/www/user/123/statuses
/status/456 → /var/www/status/456

Alternative routing, from a database perspective

/user/123 → SELECT * FROM resource WHERE path = '/user/123'
/user/123/statuses → SELECT * FROM resource WHERE path = '/user/123/statuses'
/status/456 → SELECT * FROM resource WHERE path = '/status/456'

change URL space, let's add a new user, user id 567:

/user/123 → SELECT * FROM resource WHERE path = '/user/123'
/user/123/statuses → SELECT * FROM resource WHERE path = '/user/123/statuses'

/user/567 → SELECT * FROM resource WHERE path = '/user/123'
/statuses/567 → SELECT * FROM resource WHERE path = '/statuses/123'

Allow the old URL space to coexist with the new;

Old resources are still known by their old URL

Change URL space again:

/user/123 → SELECT * FROM resource WHERE path = '/user/123'
/user/123/statuses → SELECT * FROM resource WHERE path = '/user/123/statuses'
/status/456 → SELECT * FROM resource WHERE path = '/status/456'
/statuses/123 → SELECT * FROM resource WHERE path = '/statuses/123'
/user/bobby → SELECT * FROM resource WHERE path = '/user/bobby'
/user/bobby/statuses → SELECT * FROM resource WHERE path = '/user/bobby/statuses'
/user/bobby/status/20130920T191211Z → SELECT * FROM resource WHERE path = '/user/bobby/status/20130920T191211Z'

Have some fun

/%E0%AE%95%E0%AE%B2% → SELECT * FROM resource WHERE path = '/%E0%AE%95%E0%AE%B2%'

Thank you!

Presentation notes:

To present this on a big screen, I used this little HTML snippet:

<html>
<head>
<style>
h1 {
  padding-top: 5cm;
}
.gist-file, 
.gist-data {
  background-color: white !important;
  border-width: 0 !important;
}
</style>
<script src="https://gist.github.com/mogsie/6643280.js"></script>
</head>
<body>
</body>
</html>

Have fun with it!

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