Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Posts Tagged ‘REST

Joljet mobile client

leave a comment »

A few years ago, I built a music portal to experience ExtJS and of cause to create my own music listening experience. It was fun and I have learnt a lot from this exercise from hand craft the app, built script and caching optimization. As always with great power come great responsibility. Due to the size of ExtJS core, the page load is not satisfy by most. However, the site hosted on cheap server and not using any CDN therefore I can’t push it too far.

Moving on, I have done with ExtJS and I know the site works perfectly on desktop platform. However, on mobile it is kind of suck. But well, as a pet project I’m not too worry about that until I’m need to listen to my favorite song on the go then mobile client is needed. So, I decided to built one for myself AGAIN! :D. Yeah, again. Why not after all I’m a craftman I love builting stuffs.

Now here is the plan

Image

I’m going to built mobile client which reuse all functionality from the portal and at the same time extending the functionalities to support offline, favorite & playlist customization or by the way I don’t want to be responsible for username & password so I am going to offload that task to Facebook.

Sound interesting right ;) to make it more interesting I shall say that I have no clue how to built a mobile app at this point. But, let see how long it will take for me to built one :D

 

Advertisements

Written by Chorn Sokun

May 12, 2014 at 8:07 am

REST, Routing & the beauty of MR Controller

with one comment

What is REST?

REST stands for Representational State Trasfer an important concept in REST is the existence of resources (sources of specific information), each of which is referenced with a global identifier (e.g., a URI in HTTP).

How does it work?

REST take advantage of HTTP methods such as POST, GET, PUT and DELETE and translate the request as CREATE, READ, UPDATE, DELETE (CRUD) operation associated with database technologies.

HTTP CRUD
POST Create, Update, Delete
GET Read
PUT Create, Overwrite/Replace
DELETE Delete

REST application might define resources URI such as:

http://example.com/albums
http://example.com/albums/id
http://example.com/albums/id/slideshow

and client code would look something like this:

Resource userResource = new Resource('http://example.com/users/001');
userResource.delete();

It is simple enough, right?

RESTful Castle

REST needed URI to begin with; now if you are assigned to design a server side code for that what are you gonna do? Well, you simple need an URL Rewrite tools to save day, don’t you? So what does URL Rewrite tools do? Simply it would intercept incoming HTTP request and route to relevant destination (.php, .cgi, .aspx, .asmx scripts etc). It is pretty trivial to do it on apache web server:

  1. Make sure mod_urlrewrite is enabled
  2. define a few rules in .htaccess (hmm, regex not so funny)
And that all you need. But I am not talking about Apache I am now going to talk about .NET and the IIS to be exact I am talking about Castle.MonoRail. Castle.MonoRail is a MVC framework (MVC stands for Model-View-Controller) and I am going to dive a bit into Controller. According to the document a Controller logically grouped in an Area; and it contains Actions. For instance to produce url like this /admin/users/index.rails we simple define a controller class as:
[ControllerDetails(Area="admin")]
public class UsersController : Controller
{
   public void Index()
   {
   }
}

Notice the use of ControllerDetails attribute which usually allow you to tell MonoRail how a controller can be found. The important part of ControllerDetails above is Area = “admin” which is very flexible and that mean you can burried a controller down as deep as you want it to be.

Is that mean I can have a controller defined and it will generate URI like this?

http://example.com/albums
http://example.com/albums/id
http://example.com/albums/id/slideshow

With controller alone? you basically can’t do, because controller generated URI in the form /area/controller/action.rails?id={id} to do a nice RESTful like URI you need another tool similar to URL Rewrite lucky enough MR got a build-in tool for that it located here Castle.MonoRail.Framework.Routing … going to talk more in the future.

Limitation & Workaround

How are you going to define your controller class to fullfill the required like this?

* Public Access
http://example.com/albums
http://example.com/albums/id
http://example.com/albums/id/slideshow

* Authorization Access
http://example.com/users/albums
http://example.com/users/albums/id
http://example.com/users/albums/id/slideshow

Look at the screenshot bellow:

and you say; hell YEAH that neat right; now compile and run the test site. Navigate to /album/index.rails work unfortunately navigate to /users/albums/index.rails will cause MR to throw an exception say it can’t find Area & Controller specified.

What the hell ??? the truth is MR register a controller using class name as a key I believe it run from the top namespace first which mean Basic.Web.Controllers.AlbumController got register first so when it walk into Basic.Web.Controllers.Users.AlbumController it just skip that controller.

To work around this problem against thank to ControllerDetailsAttribute which allow us to name a controller free from Controller class. How are you going to do that? well rename /users/AlbumController.cs to /users/WhatTheF_CKAlbumController.cs and boom it done.


[ControllerDetails("album", Area="users")]

public class WhatTheF_CKAlbumController{

    ... same code old code

}

Get lost? probably :) but don’t worry I am going to give a slow version in my C# lecturing session and if you are interested to walkin and listen up just subscribe to my google calendar.

Until then have a good weekend.

Written by Chorn Sokun

September 20, 2008 at 6:01 am

Posted in MonoRail, OSS

Tagged with , ,